How to properly Multithread in OpenCV in 2019?

First of all, thank you for the clarity of the question. Q: Is it okay to use (multi)threading on application level with OpenCV? A: Yes it is totally ok to use multithreading on application level with OpenCV unless and until you are using functions which can take advantage of multithreading such as blurring, colour space … Read more

“Reduce” function for Series

With itertools.chain() on the values This could be faster: from itertools import chain categories = list(chain.from_iterable(categories.values)) Performance from functools import reduce from itertools import chain categories = pd.Series([[‘a’, ‘b’], [‘c’, ‘d’, ‘e’]] * 1000) %timeit list(chain.from_iterable(categories.values)) 1000 loops, best of 3: 231 µs per loop %timeit list(chain(*categories.values.flat)) 1000 loops, best of 3: 237 µs per … Read more

Direction of two points

Answer 1: it is Vector(x2-x1,y2-y1) Answer 2: Normalizing means to scale the vector so that its length is 1. It is a useful operation in many computations, for example, normal vectors should be specified normalized for lighting calculations in computer graphics. The normalized vector of v(x,y) is vn(x/Length(v), y/length(v)). HTH

In what situation would the AVX2 gather instructions be faster than individually loading the data?

Newer microarchitectures have shifted the odds towards gather instructions. On an Intel Xeon Gold 6138 CPU @ 2.00 GHz with Skylake microarchitecture, we get for your benchmark: 9.383e+09 8.86e+08 2.777e+09 6.915e+09 7.793e+09 8.335e+09 5.386e+09 4.92e+08 6.649e+09 1.421e+09 2.362e+09 2.7e+07 8.69e+09 5.9e+07 7.763e+09 3.926e+09 5.4e+08 3.426e+09 9.172e+09 5.736e+09 9.383e+09 8.86e+08 2.777e+09 6.915e+09 7.793e+09 8.335e+09 5.386e+09 4.92e+08 … Read more

GCC: vectorization difference between two similar loops

In the first case: the code overwrites the same memory location a[i] in each iteration. This inherently sequentializes the loop as the loop iterations are not independent. (In reality, only the final iteration is actually needed. So the entire inner loop could be taken out.) In the second case: GCC recognizes the loop as a … Read more

Comparing BSXFUN and REPMAT

Introduction The debate on whether bsxfun is better than repmat or vice versa has been going on like forever. In this post, we would try to compare how the different built-ins that ship with MATLAB fight it out against repmat equivalents in terms of their runtime performances and hopefully draw some meaningful conclusions out of … Read more

Pandas: add timedelta column to datetime column (vectorized)

Full code solution: import pandas as pd from datetime import timedelta df = pd.DataFrame([[‘2016-01-10’,28],[‘2016-05-11’,28],[‘2016-02-23’,15],[‘2015-12-08’,30]], columns = [‘ship_string’,’days_supply’]) df[‘ship_date’] = pd.to_datetime(df[‘ship_string’]) df[‘time_added’] = pd.to_timedelta(df[‘days_supply’],’d’) df[‘supply_ended’] = df[‘ship_date’] + df[‘time_added’] print df ship_string days_supply ship_date time_added supply_ended 0 2016-01-10 28 2016-01-10 28 days 2016-02-07 1 2016-05-11 28 2016-05-11 28 days 2016-06-08 2 2016-02-23 15 2016-02-23 15 days … Read more

Numpy equivalent of if/else without loop

One IF-ELIF Approach #1 One approach – keep_mask = x==50 out = np.where(x>50,0,1) out[keep_mask] = 50 Approach #2 Alternatively, for in-situ edit – replace_mask = x!=50 x[replace_mask] = np.where(x>50,0,1)[replace_mask] # Or (x<=50).astype(int) in place of np.where(x>50,0,1) Code-golf? If you actually want to play code-golf/one-liner – (x<=50)+(x==50)*49 Multiple IF-ELIFs Approach #1 For a bit more generic … Read more

tech