how do I threshold pixels in an image and convert the background pixels to nothing instead of zero?
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
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
Neo
2022년 7월 22일
Neo
2022년 7월 22일
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
2022년 7월 22일
double(I).*VALUE this is normal multiple of array double just use to covert from logical to double
Jenifer NG
2022년 7월 23일
@DGM thanka you for advise
Image Analyst
2022년 7월 23일
편집: Image Analyst
2022년 7월 23일
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
2022년 7월 23일
Image Analyst
2022년 7월 25일
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
2022년 7월 25일
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
2022년 7월 29일
Image Analyst
2022년 7월 29일
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.
카테고리
도움말 센터 및 File Exchange에서 Image Arithmetic에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

