how do I threshold pixels in an image and convert the background pixels to nothing instead of zero?

36 views (last 30 days)
I want to create a threshold for the pixels then set them to nothing.
here is what I have thus far:
%set a pixel threshold cutoff (60 is my threshold) - minimize background by choosing a
%value cutoff, such that every pixel less than that value is considered one class,
%while every pixel greater than that value is considered the other class.
I = imread('.tiff');
level = graythresh(I)
BW = imbinarize(I,level);
Display the original image next to the binary image.
imshowpair(I,BW,'montage')
end
Can this work for a RGB image?
Now I want to convert everything not of interest i.e. the background and pixels not of interest to NAN
%convert the intensity values of the background to "nothing"
% rather than to 0 and keeps the other values
After reading my image I, If i want to make all the black pixels [0 60] to NaN:
I(I>=0 & I<= 60) = NaN
Thank you so much, I am stuck at this stage and cannot move. Please help.

Accepted Answer

Image Analyst
Image Analyst on 22 Jul 2022
What you gave will work for a color image and an image of any dimensions. However, there is no need to do what you asked. It's possible to do anything you want with just the regular binary image.
  12 Comments

Sign in to comment.

More Answers (1)

Jenifer NG
Jenifer NG on 22 Jul 2022
Edited: Jenifer NG on 22 Jul 2022
This is my understand from your question. All value <60 will be convert to NaN
I =imread('corn.tif')
I = 415×312
105 105 39 88 27 42 38 99 61 99 75 88 39 105 69 52 52 69 105 39 39 21 85 85 85 69 52 118 118 118 35 88 50 71 42 38 99 100 86 100 42 50 88 88 39 39 39 39 39 21 21 71 85 63 85 39 69 52 31 31 71 71 71 38 99 99 100 100 110 124 100 42 27 88 88 88 88 39 88 21 71 24 99 63 14 39 69 52 52 52 42 42 38 99 99 100 100 110 86 16 100 22 38 42 71 27 71 71 71 71 38 99 63 100 99 71 39 69 69 69 99 99 99 100 100 100 100 100 86 100 44 86 22 99 42 42 42 38 85 38 63 99 100 113 25 71 39 39 69 69 38 38 99 38 38 42 99 22 99 22 100 100 86 100 100 100 99 99 100 63 100 61 61 110 113 99 81 39 39 41 42 42 42 50 50 71 42 71 42 42 99 99 100 86 86 100 44 100 100 100 86 16 110 124 124 99 17 71 41 41 71 50 50 88 88 50 88 88 71 71 71 38 99 34 86 16 22 22 104 44 61 86 86 110 110 34 99 17 71 71 50 50 78 35 35 78 88 88 27 71 71 71 99 99 34 86 22 22 22 38 95 89 100 8 61 100 99 14 17 17 88 35 19 35 35 39 88 107 50 88 71 71 42 99 100 100 22 97 75 42 24 24 42 24 38 63 100 99 99 17
VALUE = double((I<=60))
VALUE = 415×312
0 0 1 0 1 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 0 0 0 1 1 0 0 1 1 1 1 1 1 1 0 0 0 0 1 0 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 1 0 1 1 1 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0 0 0 0 1 0 1 1 0 0 1 1 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0 0 0 0 0 1 0 1 0 1 1 1 0 1 0 0 0 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 0 0 0 1 0 1 1 1 1 0 0 0 1 0 0 0 1 1 1 0 1 1 1 1 1 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1
VALUE(VALUE == 1) = NaN;
VALUE(VALUE == 0) = 1;
double(I).*VALUE
ans = 415×312
105 105 NaN 88 NaN NaN NaN 99 61 99 75 88 NaN 105 69 NaN NaN 69 105 NaN NaN NaN 85 85 85 69 NaN 118 118 118 NaN 88 NaN 71 NaN NaN 99 100 86 100 NaN NaN 88 88 NaN NaN NaN NaN NaN NaN NaN 71 85 63 85 NaN 69 NaN NaN NaN 71 71 71 NaN 99 99 100 100 110 124 100 NaN NaN 88 88 88 88 NaN 88 NaN 71 NaN 99 63 NaN NaN 69 NaN NaN NaN NaN NaN NaN 99 99 100 100 110 86 NaN 100 NaN NaN NaN 71 NaN 71 71 71 71 NaN 99 63 100 99 71 NaN 69 69 69 99 99 99 100 100 100 100 100 86 100 NaN 86 NaN 99 NaN NaN NaN NaN 85 NaN 63 99 100 113 NaN 71 NaN NaN 69 69 NaN NaN 99 NaN NaN NaN 99 NaN 99 NaN 100 100 86 100 100 100 99 99 100 63 100 61 61 110 113 99 81 NaN NaN NaN NaN NaN NaN NaN NaN 71 NaN 71 NaN NaN 99 99 100 86 86 100 NaN 100 100 100 86 NaN 110 124 124 99 NaN 71 NaN NaN 71 NaN NaN 88 88 NaN 88 88 71 71 71 NaN 99 NaN 86 NaN NaN NaN 104 NaN 61 86 86 110 110 NaN 99 NaN 71 71 NaN NaN 78 NaN NaN 78 88 88 NaN 71 71 71 99 99 NaN 86 NaN NaN NaN NaN 95 89 100 NaN 61 100 99 NaN NaN NaN 88 NaN NaN NaN NaN NaN 88 107 NaN 88 71 71 NaN 99 100 100 NaN 97 75 NaN NaN NaN NaN NaN NaN 63 100 99 99 NaN
  14 Comments
Image Analyst
Image Analyst on 29 Jul 2022
No. I don't have a visual representation of the number of pixels - that's just a number. How would you visualize 12?
The data are from the small random image I created. Apply to your image by adapting the demo code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!