Ask Your Question

How to do proper thresholding?

asked 2013-12-03 22:30:09 -0500

zawpai gravatar image

updated 2013-12-05 21:41:56 -0500


I face some problems about thresholding. These are the images from Camera.Sample.bmp, Sample2.bmp and Sample3.bmp. According to these three images, how should I do the proper thresholding or which process I need to do before thresholding?

Please give me some suggestions.

Tested image Contours Image.bmp


edit retag flag offensive close merge delete


Which portion you want to segment in the image ?

Haris gravatar imageHaris ( 2013-12-04 01:37:46 -0500 )edit

Haris, I want to extract the black dot from center potion.

zawpai gravatar imagezawpai ( 2013-12-05 03:23:21 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-12-05 05:18:21 -0500

Haris gravatar image

updated 2013-12-05 23:28:36 -0500

It seems simple blur() with low kernel value works fine with your images. After blur just apply Threshold Binary Inverted with lower value.

  Mat src=imread("1.bmp",0);
  Mat dst,thr;
  blur( src, thr, Size( 5, 5 ), Point(-1,-1) );// blur with kernel(5X5)
  threshold(thr,thr,50,255,CV_THRESH_BINARY_INV);// Threshold binary inverted 
  src.copyTo(dst,thr); copy source image to destination by setting threshold as mask

For better result you may proceed with findcontour(),drawcontour(),minEnclosingCircle() on your threshold image to segment exactly circled image.


Using contour you can try below code

float radius=0;
Point2f center;  
vector< vector <Point> > contours; // Vector for storing contour
vector< Vec4i > hierarchy;
Mat mask(src.rows,src.cols,CV_8UC1,Scalar::all(0)); //create mask image

findContours( thr, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); 
minEnclosingCircle( contours[0], center, radius); //Here assumes  your image contains only one contour, if more than one contour use loop and consider largest contour
circle(mask,Point(center.x,center.y),radius,Scalar(255),-1,8,0);//Fill mask with region to be segment
//Copy using above mask

And here is the result i got for your image.

image description image description image description

edit flag offensive delete link more


Haris, Thanks. I will do a testing first. Then, I will get back you soon.

zawpai gravatar imagezawpai ( 2013-12-05 18:31:41 -0500 )edit

Hi Haris, I don't get the result and I also get some of the errors. After I use "contours->h_next" to loop and "drawContours", I can't call "minEnclosindCircle". System will prompt an error because "contours" is zero. I am not sure whether "minEnclosingCircle" is working properly or not if the image is not the circle shape. Please take look my imag in question.

zawpai gravatar imagezawpai ( 2013-12-05 21:41:11 -0500 )edit

@zawpai See my edit....

Haris gravatar imageHaris ( 2013-12-05 23:31:38 -0500 )edit

Hi Haris, Thanks. It is working now, but the area is not so accurate if we draw a circle on the original image. So, I need to pay the threshold to get the corrected size.

zawpai gravatar imagezawpai ( 2013-12-06 01:11:19 -0500 )edit

Just increase canny threshold value and try again....

Haris gravatar imageHaris ( 2013-12-06 01:38:50 -0500 )edit

Question Tools


Asked: 2013-12-03 22:30:09 -0500

Seen: 253 times

Last updated: Dec 05 '13