In: Computer Science
What is the difference between the inRange() and threshold() functions in OpenCV in Python? Thorough answer please!
What is the difference between the inRange() and threshold() function in OpenCV in Python?
Range and threshold functions can be used for image processing
and other applications in OpenCV.
The main difference between range() and threshold() is that the
range function is commonly used as range(int start, int end) where
start is inclusive too. This function checks the value passed to it
if it is within the specified range
Example:
Range(0,10)
For this if we pass a value 5 it accepts it.
If we pass a value of 255 it rejects it.
void my_function(..., const Range& r, ....)
{
if(r in range(0,5)) {
// process [r.start,
r.end)
}
else {
//process everything else
}
}
There are also other static member functions for range like all()
which returns a special variable that means "the whole sequence" or
"the whole range"
void my_function(..., const Range& r, ....)
{
if(r == Range::all()) {
// process all the data
}
else {
// process [r.start,
r.end)
}
}
Now, about thresholding
It is simple to explain threshold is like a barrier which your
values can either cross or not
If your value is greater than a threshold then it gets
accepted
Example:
If threshold is 128
And given value is 255, it will pass
If give value is 120, it wont pass
Now lets see in terms of image processing
First argument is the source image, which should be a grayscale
image. Second argument is the threshold value which is used to
classify the pixel values. Third argument is the maxVal which
represents the value to be given if pixel value is more than
(sometimes less than) the
threshold value.
There are also various other thresholding functions like binary,
binary inverse, to zero , adaptive thresholding etc.
To conclude thresholding and range can be used to achieve similar
results in image processing, but there are some cases where one
function is better used than the other.
Like when you want to convert a grayscale image into binary
( 0 and 1, pure white and pure black
) you can use cv2.THRESH_BINARY.
And when you want to see the brightest pixels of an image or the
darkest pixels in an image you can use either range or threshold
depending on your convenience, but remember that threshold only
checks for the maxVal or threshold border which range() checks if
it is in between the start and end values.
For better understanding the below explanation is given:
IN RANGE:
1.OpenCV's inRange()
function is
very similar to threshold(). It also takes a grayscale image and
converts it into a "binary" image: one where every pixel is either
all-white or all-black based on whether or not the pixel is within
the given range of of values.
OpenCV's inRange()
function is very similar to
threshold(). It also takes a grayscale image and converts it into a
"binary" image: one where every pixel is either all-white or
all-black based on whether or not the pixel is within the given
range of of values.
Where threshold()
selects for
pixels that are above a given value, inRange()
lets
you specify a maximum as well as a minimum value.
To see the difference, compare the same image filtered with
inRange(35, 50)
and with a simple
threshold(35)
:
Where threshold(35)
catches the bright near-white
of the wall as well as the light gray of my face,
inRange(35,50)
, leaves the wall as black, i.e.
un-selected, along with the highlight on the upper-right side of my
face.
Use with Color Images and the Hue Channel:
Like most of the other OpenCV filter functions,
inRange()
can be used on any single-channel image, not
just grayscale versions of color images. In particular,
inRange()
is especially useful when applied to the Hue
channel. For more about this technique see Color Tracking in HSV
Color Space.
Parameters:
inRange(int min, int max)
takes two arguments: the
first represents the lower bound of the range, the second
represents the upper bound. It will result in a binary OpenCV
output image with white where the pixels were in-range and black
where they were out-of-range.