OpenCV function similar to matlab's “find”

后端 未结 2 794
长发绾君心
长发绾君心 2021-01-12 07:03

I am looking for a function in openCV to help me make masks of images.

for example in MATLAB:

B(A<1)=0;

or

B=zeros(size(A));

B(A==

相关标签:
2条回答
  • 2021-01-12 07:48

    OpenCV C++ supports the following syntax you might find convenient in creating masks:

    Mat B= A > 1;//B(A<1)=0
    

    or

    Mat B = A==10;
    B *= c;
    

    which should be equivalent to:

    B=zeros(size(A));
    B(A==10)=c;
    

    You can also use compare(). See the following OpenCV Documentation.

    0 讨论(0)
  • 2021-01-12 07:53

    Some functions allow you to pass mask arguments to them. To create masks the way you describe, I think you are after Cmp or CmpS which are comparison operators, allowing you to create masks, by comparison to another array or scalar. For example:

    im = cv.LoadImageM('tree.jpg', cv.CV_LOAD_IMAGE_GRAYSCALE)
    mask_im = cv.CreateImage((im.width, im.height), cv.IPL_DEPTH_8U, 1)
    #Here we create a mask by using `greater than 100` as our comparison
    cv.CmpS(im, 100, mask_im, cv.CV_CMP_GT)
    #We set all values in im to 255, apart from those masked, cv.Set can take a mask arg.
    cv.Set(im, 255, mask=mask_im)
    cv.ShowImage("masked", im)
    cv.WaitKey(0)
    

    Original im:

    enter image description here

    im after processing:

    enter image description here

    0 讨论(0)
提交回复
热议问题