changing values of pixels in an image pixel by pixel ( Thresholding )
Show older comments
Hi everyone, I'm trying to put a threshold on a grayscale image, and I'm doing it this way: that a "for" loop reads the image pixel by pixel, and if the value of the pixel is less than "0.5" sets its' value to "0", and if it is more than "3", sets it to "256". Here it is my code:
my_image =imread('picture.tif');
for R=1:num of Rows
for C=1:num of Columns
pixel=my_image(R,C);
if pixel<0.50000000 , pixel=0.000000000;, end
if pixel>3.00000000 , pixel=256;, end
thresh(R,C)=pixel;
end
end
im_thresh=mat2gray(thresh);
figure,imshow(im_thresh);
title('thresholding');
But it doesn't work properly. Sometimes it misses some values that ought to be changed, and sometimes changes the ones that shouldn't be changed, to arbitrary values…for example changes the value "0.8182" to "0.0032". It also doesn't have enough accuracy, for example instead of changing the value "0.425" to "0", changes it to "0.0002". Could you please tell me what's causing this problem, and help me to fix the code?! Thanks in advance…
4 Comments
Parth Batra
on 21 Apr 2014
Edited: Parth Batra
on 21 Apr 2014
I also need help with a similar probelem but i want to change the values of pixels between 135 and 165 to 0 please help
i tried the looping method but it's not providing satisfactory results.
Waleed Naeem
on 9 Nov 2016
Please let me know also if you find the solution to your problem
Image Analyst
on 9 Nov 2016
Waleed, since Mithra asked this 3 years ago, you might want to consider asking your own new question, and attach your image.
Pravin Ninawe
on 20 Feb 2018
thanks
Accepted Answer
More Answers (2)
Laurent
on 5 Sep 2013
Hi,
First I don't understand how your original image can have non-integer pixel values, because it seems your image is 8-bit from how you access the pixel-values. Is the value '0.8182' a pixel-value in your original image? Without the image itself it will be difficult to exactly determine what is going on.
Anyway, you can remove the for-loops and do it as follows:
thresh=my_image;
thresh(thresh<0.5)=0;
thresh(thresh>3)=255;
If your image is 8-bit, the values go from 0 to 255, not to 256.
3 Comments
Image Analyst
on 5 Sep 2013
Her image is floating point (supposedly, but I'm not convinced) so if she's right then 256 is possible.
Mithra
on 6 Sep 2013
Image Analyst
on 6 Sep 2013
OK. Floating point images (either single or double) can have a value of 256, whereas uint8 are limited to 255. But I see no reason at all why you would want that particular value. Can you explain why?
Image Analyst
on 5 Sep 2013
0 votes
Are you sure your tiff image has fractional values? I know it's a very flexible format and may allow floating point pixel values but it's not common. Anyway, you probably don't want a thresholded image with values of 0 and 256 and intact/unchanged values in the range 0.5 to 3.0. What are you going to do with the result anyway? Once I know that I can tell you how to proceed. You may want a binary image (the usual result of thresholding) to do what you need to do rather than another floating point image.
2 Comments
Mithra
on 6 Sep 2013
Image Analyst
on 6 Sep 2013
But why? Are you familiar with morphology? Do you know how grayscale morphology differs from binary morphology (my guess is no)? OK, let me ask...after you do this grayscale morphology that you plan on doing, what do you want the result to be? Please upload an image and tell me what you want to find/measure/enhance in it.
Categories
Find more on Morphological Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
