How to Threshold image ?

97 views (last 30 days)
RuiQi
RuiQi on 6 Feb 2016
Commented: Image Analyst on 6 Feb 2016
Hi i am trying to write out the code for edge detection on my own. I dont know how to threshold to get the edges. There is no function that lets me threshold it like output = threshold(image,200) where 200 is the value to threshold. My code is below. help thanks
Image = imread('42049.jpg');
Result = conv2(double(Image),Gx,'valid');
Result = abs(Result);
minV = min(Result(:));
maxV = max(Result(:));
FinalResult = (Result-minV)*255/(maxV-minV);
BW = im2bw(FinalResult, 0.8);
imshow(BW);

Answers (1)

Walter Roberson
Walter Roberson on 6 Feb 2016
BW = FinalResult >= 200;
The error you have, by the way, is that you are leaving your final result as double precision when you scale it by 255. The thresholding of double precision images is done under the assumption that they are in the range 0 to 1. If you had not multiplied by 255, or if you had used uint8() on FinalResult then your thresholding would have worked.
  1 Comment
Image Analyst
Image Analyst on 6 Feb 2016
Plus, FinalResult is not needed, at least in the code we see so far. Doing a linear scaling on an image does not change what the final thresholded image will look like. It will just threshold at a different place, that's all. But the binary image will be the same.
If you are doing edge detection,like, say, with a Laplacian, you will have two values on either side of an edge - a positive one and a negative one. I don't think it's necessary to use abs - that will just make the edge thicker, and possibly with a zero between them so you'd have a double edge line.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!