Question

In: Computer Science

What is the difference between the inRange() and threshold() functions in OpenCV in Python? Thorough answer...

What is the difference between the inRange() and threshold() functions in OpenCV in Python? Thorough answer please!

Solutions

Expert Solution

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.


Related Solutions

describe the difference between the principles of transduction, absolute threshold and difference threshold
describe the difference between the principles of transduction, absolute threshold and difference threshold
What is the difference between local and global variable? Functions are also called ____________ In python,...
What is the difference between local and global variable? Functions are also called ____________ In python, to import functions from modules we use the keyword ‘import’, assume that you have a module named “mySelf” the mySelf module has “getMyName”, “getMyage” , and many more functions. 3 points. Write a statement that imports 3.a. Only getMyName function 3.b all other functions from the module How do you differentiate a variable from a function? Write a function named times_ten. The function should...
Describe the difference between threshold and non-threshold dose response curves. Give one example of a disease...
Describe the difference between threshold and non-threshold dose response curves. Give one example of a disease or condition that fall into each category. (2 points)
What do you know about programming in Python? What is the difference between Python and Java?
What do you know about programming in Python? What is the difference between Python and Java? What does the term Open Source mean? Name four examples of Open Source software. What is the IDEL programming environment? How does IDEL relate to Python? How do you spread a long statement over multiple lines in Python? How do you use the loop-index? How will knowing and understanding Python impact what you do in your profession and/or personal experiences?
Please provide a thorough explanation. thanks ...What is the difference between transactions and economic exposure? Which...
Please provide a thorough explanation. thanks ...What is the difference between transactions and economic exposure? Which can be hedge more easily?
Distinguish between absolute threshold and the just noticeable difference and provide an example of each.
Distinguish between absolute threshold and the just noticeable difference and provide an example of each.
What is the difference between “motives” and “functions,” and how are these two concepts related to...
What is the difference between “motives” and “functions,” and how are these two concepts related to "manifest functions" and "latent functions" according to Merton?
Answer these questions. 1.What is the difference between Minority status and Culture? What is the difference...
Answer these questions. 1.What is the difference between Minority status and Culture? What is the difference between Western and Eastern culture? What is the difference between a stigma vs misdiagnosing a mental illness? What are 3 norms of etiquette in across cultures? What is somatization? What are three specific types of cultural concepts? What are 3 ways immigrants face when arriving to a United States? What is the difference between acculturation and assimilation? What are some ways you feel like...
Answer the following in short answer. A. What is the difference between 'cultural humility' and 'cultural...
Answer the following in short answer. A. What is the difference between 'cultural humility' and 'cultural competence'? B. How is culture usually defined in health research? C. What problems have arisen from how 'cultural competency' trainings have emerged in health care?
the answer should be thorough and be about a page and a half. What determines the...
the answer should be thorough and be about a page and a half. What determines the desired amounts of national savings and investment? What relationship between desired savings and desired investment is required for goods market equilibrium, and how is this condition achieved? Please be specific in your response.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT