Threshold

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.

 采纳的回答

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 个评论

Fathin
Fathin 2011-8-2
Hi,
Thank you for your answer. It's really fast and easy for beginner like me. This is the real image and the result after threshold;
real image:
http://www.flickr.com/photos/64698236@N03/6002580719/in/photostream
result:
http://www.flickr.com/photos/64698236@N03/6002580781/in/photostream/
I already remove all the leaves and stem and some of the background, how can I get ride all of the remain background? So that the image left is only fruit? For now, I am trying some dilution, erosion and also imopen and imclose function, but nothing works well. maybe my combination of all those are suitable enough to get ride the background pixel.
after applying the command you provided above, I used the code below:
se=strel('disk', 2);
image3=imclose(image2,se);
imshow(image3);
se1=strel('disk', 22);
image4=imerode(image3,se1);
imshow(image4);
se2=strel('disk', 20);
image5=imdilate(image4,se2);
imshow(image5);
%Segmentation
tic;
[accum, circen, cirrad] = CircularHough_Grd(image5, [20 60],95,47, 1);
toc
%post-processing
any(cirrad <= 0)
if any(cirrad <= 0)
inds = find(cirrad>0);
cirrad = cirrad(inds);
circen = circen(inds,:);
end
here is the result for erosion,dilation
http://www.flickr.com/photos/64698236@N03/6002580801/in/photostream/
this is the result for detection
http://www.flickr.com/photos/64698236@N03/6003126672/in/photostream/
I was thinking that, if I can remove all the background, so that the result of the apple detection will be perfectly accurate.
Am I right? If you have any other suggestion, you are most welcome..
Many thanks,
:P
That is a challenging problem. Finding green fruit on a green cluttered background is tough. Not the kind of thing I can answer in an online message. It's more like a Master's or Ph.D. project. You might have to do lots of things and combine them, like color segmentation, texture segmentation, Hough, then do shape filtering, etc. Sorry I can't help you anymore.
Fathin
Fathin 2011-8-3
Thank you very much for your help..
:p
Fathin
Fathin 2011-8-3
I am sorry, can I ask you 1 more simple question?
I have used your function below, how can I change the pixel inside the boundary which the value less than threshold(0.8) to be 0?
I have tried add several line of command but failed.
here the image that I means:
http://www.flickr.com/photos/64698236@N03/6006901788/in/photostream
[B,L] = bwboundaries(image7,'noholes');
% Display the label matrix and draw each boundary
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
if metric < threshold
L(i,j)=0;
end
text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
'FontSize',14,'FontWeight','bold');
end
Thank you in advance.
binaryImage = maskedImage < 0.8;
maskedImage(binaryImage) = 0;

请先登录,再进行评论。

更多回答(1 个)

Pramod Bhat
Pramod Bhat 2011-8-25

0 个投票

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 个评论

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!

Translated by