Threshold
28 次查看(过去 30 天)
显示 更早的评论
Hi, i already did a threshold to my image using function below: image1 = im2bw(image, 70); imshow(image1);
In my image, I only want the pixels that value from 70-130, so can I threshold the value above 130? Since I know that threshold function will remove the pixel value BELOW the standard value we put in(70 in my case). How can I do that? Or is there any other function I can do this?
Thank you.
0 个评论
采纳的回答
Image Analyst
2011-8-1
See my demo (simply copy and paste):
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cell.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Threshold the image.
binaryImage = grayImage >= 70 & grayImage <= 130;
% Display the binary image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Mask the image.
maskedImage = grayImage; % Initialize.
maskedImage(~binaryImage) = 0;
% Display the masked image.
subplot(2, 2, 3);
imshow(maskedImage, []);
title('Masked Image', 'FontSize', fontSize);
msgbox('Done with demo!');
5 个评论
更多回答(1 个)
Pramod Bhat
2011-8-25
I dont think u need that much big program. the program below exemplifies ur need. But i this case the pixels below 70 and above 130 will be assigned to the value 0.
img=imread('e.gif');
img1=img(img>130); img2=img2(img1<70);
imshow(img2);
1 个评论
Image Analyst
2011-8-25
Yes, I have that in there. Sorry if you don't like the extra tutorial stuff I added like giving titles to the images, checking for existence of the Image Processing Toolbox, enlarging to full screen, and displaying some informative intermediate images on screen. You can just ignore that stuff and string multiple lines together on the same line (like you did) if you just want a super compact, bare bones version. (By the way, your code doesn't work because you didn't define img2 before you tried to use it, among other reasons. You should test code before you post it, like I do.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!