OpenCV threshold image for max and min range

I like to spawn an image within the max and min values ​​and fill in the remaining 255 for these pixel values ​​from this range. I cannot find such a threshold in an OpenCV document here . Thanks

+4
source share
2 answers

Main:

threshold(src, dst, threshold value, max value, threshold type);

Where

src_gray: Our input image
dst: Destination (output) image
threshold_value: The thresh value with respect to which the thresholding operation is made
max_BINARY_value: The value used with the Binary thresholding operations (to set the chosen pixels)
threshold_type: One of the 5 thresholding operations. 

for example,

threshold(image, fImage, 125, 255, cv::THRESH_BINARY);

means each value below 125, will be set to zero, and above 125 - 255.

If what you are looking for should have a certain range, for example, from 50 to 150, I would recommend that you make a for loop and also check and edit the pixels yourself. It is very simple. Take a look at this C ++ code:

for (int i=0; i< image.rows; i++)
    { 
        for (int j=0; j< image.cols; j++)
        {
            int editValue=image.at<uchar>(i,j);

            if((editValue>50)&&(editValue<150)) //check whether value is within range.
            {
                image.at<uchar>(i,j)=255;
            }
            else
            {
                image.at<uchar>(i,j)=0;
            }
        }
    }

, . Cheers (: , .

+5
inRange(src, lowerBound, upperBound, dst);
bitwise_not(src, src);
+12

Source: https://habr.com/ru/post/1530524/


All Articles