how do I threshold pixels in an image and convert the background pixels to nothing instead of zero?
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Jenifer NG
el 22 de Jul. de 2022
Editada: Jenifer NG
el 22 de Jul. de 2022
This is my understand from your question. All value <60 will be convert to NaN
I =imread('corn.tif')
VALUE = double((I<=60))
VALUE(VALUE == 1) = NaN;
VALUE(VALUE == 0) = 1;
double(I).*VALUE
14 comentarios
Neo
el 22 de Jul. de 2022
Neo
el 22 de Jul. de 2022
Bear in mind that at this point, you have an improperly-scaled image full of NaNs. If you need the NaNs for a certain purpose, that's fine. If you try to feed the image to certain functions (e.g. imshow(), imwrite()), you'll get nonsense out.
If you want an image that's at least still scaled for its class:
inpict = imread('cameraman.tif');
mask = inpict<=60; % select dark pixels
outpict = im2double(inpict); % cast and scale
outpict(mask) = NaN; % use the mask itself
I should point out that corn.tif is an indexed image, so thresholding it by index alone doesn't really make any sense.
Jenifer NG
el 22 de Jul. de 2022
double(I).*VALUE this is normal multiple of array double just use to covert from logical to double
Jenifer NG
el 23 de Jul. de 2022
@DGM thanka you for advise
Image Analyst
el 23 de Jul. de 2022
Editada: Image Analyst
el 23 de Jul. de 2022
Again, I doubt having nans is necessary. What do you think you can't do if they remain as 0's?
I kind of agree with IA here. Unless you have a particular reason, it shouldn't be a common practice.
If you're thinking that you can use NaNs to make parts of an image appear transparent in a figure, that doesn't work. NaNs are simply rendered as opaque black, so it's visually equivalent to using zero.
A = imread('cameraman.tif');
B = imread('cman_01.png'); % color version of the above
mk = A<150; % select dark regions
A = im2double(A);
A(mk) = NaN;
imshow(B); hold on % show color image
imshow(A) % put image with NaNs on top
As you can see, this doesn't accomplish anything. If you wanted transparency, you could do it with the mask itself.
A = imread('cameraman.tif');
B = imread('cman_01.png'); % color version of the above
mk = A<150; % select dark regions
figure
imshow(B); hold on % show color image
hi = imshow(A); % put gray image on top
hi.AlphaData = ~mk; % set the alphadata
Similarly, trying to save images with transparency won't work by embedding NaNs in the image.
That said, there are some tools (e.g. John's inpaint_nans()) that can be utilized by embedding the mask into the image using NaNs.
Neo
el 23 de Jul. de 2022
Image Analyst
el 25 de Jul. de 2022
Yes, the region of interest is simply the binary image you get from thresholding.
roi = grayImage < someThreshold;
If you want a 1-D list of all pixel values in the ROI (usually you don't need to use this though) you can simply do
pixelsInROI = grayImage(roi);
Not sure what you really want to accomplish. Let's say that you have your ROI, by whatever method. Then what would you do with it? Some kind of blob analysis like getting the blobs' mean intensities, areas, diameters, count, etc.?
Neo
el 25 de Jul. de 2022
As an example:
grayImage = randi(255, 5, 10)
% Get a 2-D ROI by thresholding.
roi = grayImage < 60
% Count the number of pixels in the ROI
numPixelsInROI = nnz(roi)
% Get only the pixels in the ROI in a 1-D column vector.
pixelsInROI = grayImage(roi)
% Get the mean of those pixels.
meanOfPixelsInROI = mean(pixelsInROI)
Neo
el 29 de Jul. de 2022
Image Analyst
el 29 de Jul. de 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.
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

