× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

opencv im2bw implementation

Ray Phan
Feb 02, 2015
<p>The reason why it isn't working is because you are improperly specifying the maximum threshold value (fourth parameter of the <code>cv::threshold</code> function). If you <a href="http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html?highlight=threshold#threshold" rel="nofollow">consult the documentation</a>, <code>cv::threshold</code> is called in the following way:</p> <pre><code>cv::threshold(src_gray, dst, threshold_value, max_BINARY_value,threshold_type); </code></pre> <p>Each of the input parameters are explained as follows:</p> <ul> <li><code>src_gray</code>: Input image</li> <li><code>dst</code>: Destination (output) image - same type as the input image</li> <li><code>threshold_value</code>: The threshold value with respect to which the thresholding operation is made</li> <li><code>max_BINARY_value</code>: The value used with the binary thresholding operations (to set the chosen pixels)</li> <li><code>threshold_type</code>: One of the 5 thresholding operations. They are listed in the documentation. Actually, a nice verbose description about each threshold can be found here: <a href="http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html" rel="nofollow">http://docs.opencv.org/doc/tutorials/imgproc/threshold/threshold.html</a>, but you chose <code>CV_THRESH_BINARY</code>, which sets each pixel past your threshold to be of the value <code>max_BINARY_value</code>.</li> </ul> <p>Now, look very closely at your <code>max_BINARY_value</code> parameter. You set this to 1. You actually should be setting this to 255. I'm going to assume that your input is an 8-bit grayscale image, which is why we set it to 255. If this is <strong>not</strong> the case, then you need to set it to whatever the maximum intensity is for your datatype. If it's 16-bit, then you'd need to set this to 65535, and so on.</p> <p>The way <code>CV_THRESH_BINARY</code> works is that any values that surpass your threshold, you will set this value to <code>max_BINARY_value</code>, which you have elected to be 1. Therefore, when you try and visualize it, 1 is such a small intensity that you don't see anything! Therefore, you simply need to change the fourth parameter to 255:</p> <pre><code>cv::Mat AutomaticMacbethDetection::im2bw(cv::Mat src, double grayThresh) { cv::Mat dst; cv::threshold(src,dst,grayThresh,255,CV_THRESH_BINARY); return dst; } </code></pre> <p>Be advised that MATLAB's <code>im2bw</code> function takes in thresholds between <code>[0-1]</code> where OpenCV takes in thresholds that are between <code>[0,maxValue]</code>, where <code>maxValue</code> is the maximum value associated with that datatype. If you are specifying values between <code>[0-1]</code>, you'd better multiply <code>grayThresh</code> in your function by 255, or 65535, or whatever the maximum value is for your data type first! If this is the case, then you'd need to multiply the threshold value by the maximum datatype inside the third parameter of <code>cv::threshold</code>.</p> <p>As such:</p> <pre><code>cv::Mat AutomaticMacbethDetection::im2bw(cv::Mat src, double grayThresh) { cv::Mat dst; cv::threshold(src,dst,255*grayThresh,255,CV_THRESH_BINARY); return dst; } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28003497/opencv%20im2bw%20implementation/28006321">Stack Overflow</a>.</p>
comments powered by Disqus