python - OpenCV multicolor thresholding -
I am trying to multicolor thresholding on an opencv cv2 image. The problem I am trying to solve is the following:
- Every "valid" list is in R, G, B
- If one pixel r, j For all valid values,
for example
- [221, make pixel (0,0,0) again, otherwise, make it (255) , 255, 255) 180, 50] is considered valid in R channel
- [23, 18, 2] is considered to be valid in live channel
- [84, 22, 48 ] B is considered to be valid in the channel
Then if the following value (RGB commands in a pixel
It will change in (0,0,0), otherwise (255255255)
Currently, I'm doing it with looped nested for loop:
in range for x (width): for y in range (height): imcv [y, x] = threshold [Y, x])
Where thresholds
function displays the arguments described above. Note that although I did this place, in-place transformation is not required.
The method I currently use, but it is still very slow I believe there should be a better way in OpenCV / Numpy. I am very new to both the structure and can not understand how
I researched OpenCV thresholding functions, it seems that they can only work on a channel gray scale image, it also series continuous range What do I need to do thresholding on all 3 channels at discrete values? I think there is a custom function to pass there, but I am unable to find the right API in my docs.
I also saw the numpy API that I could use, like ufunc
. It seems that what I want to achieve here to achieve it, or I did not see it.
Any help is appreciated.
Edit:
Thanks to both Abidaram's and care, both solutions achieved more than x1500 improvement in performance.
ncalls tottime percall cumtime percall filename: lineno (function) 1 1.576 1.576 1.576 1.576 test.py:48 (preprocess2_image) 1 0.000 0.000 0.001 0.00.py test.py:79 (preprocess2_image3) 1 0.000 0.000 0.001 0.001 test.py:66 (preprocess2_image2)
please Try:
z1 = np.dstack ([np.in1d (IMG [..., 0], b), NPNN1D (IMG [..., 1], G), NPN.N.DD (IMG [..., 2], R.]]). Reshape (img.shape) q = np.all (z1, axis = 2) out = np.uint8 (q) * 255
np.in1d (a, B)
gives you a similar level of boolean array with true
, if that element is in B, else incorrect
This is a vector equivalent of the in
method in Python or in short:
np.in1d (a, b) & lt; ==> [i if i in b other wrong] i
You do it for all channels, i.e. check the first channel for valid values in B , Second with G and second with R.
Then you place them in the z-direction stack np.dstack
. Why z-direction? Because we want in the format BGR-BGR-BGR ...
But remember, this is a 1D array, so we resize it into our original image size using the X.reshape (img.shape)
method.
So now you have a boolean mask where true is valid if it is valid and false.
This is in the first line of code.
Now you want to see a valid BGR combination. A combination is valid if all B, G, R components are true. Then you apply np.all () in the z-direction then you get a boolean mask quote
q will be a boolean mask with valid colors and others as correct.
Then you can change the integer data type, True -> 1 and False -> 0
Then you multiply with 255. If you want an inverted image, you can use np.bitwise_not
Comments
Post a Comment