Hi,
i want to run a single image through multiple thresholds. Initially, i had separate lines of code for each threshold (im2bw(img,.1), im2bw(img,.2) and so on). Now i want to run the image between thresholds ranging from 0:.001:.5. thats a total of 500 hundred values. How do I write that in few lines rather than write 500 lines?
I have tried for loop
thld = 0 : .001 : .5
for i=1:500
a(i)=im2bw(img,thld)
end
but this doesn't work.
Thank you for the help

 채택된 답변

Guillaume
Guillaume 2014년 10월 23일

2 개 추천

Nearly there. Use cell arrays for storing your images or a 3d matrix:
thld = 0 : .001 : .5;
for idx = 1:numel(thld)
a{idx} = im2bw(img, thld(idx));
end
or
thld = 0 : .001 : .5;
a = zeros([size(img), numel(thld)]); %better preallocate huge array
for idx = 1:numel(thld)
a(:,:, idx) = im2bw(img, thld(idx));
end

댓글 수: 8

jchris14
jchris14 2014년 10월 23일
This works well. But now need to find the difference in the number of black pixels to white pixels.
I used to do,
wpix=sum(a(:)); %a is the im2bw function
bpix=numel(a)-wpix;
this wont work with cell arrays unfortunately. Any ideas? I tried using cell2mat with no luck unless im using it wrong.
thanks again
Image Analyst
Image Analyst 2014년 10월 23일
편집: Image Analyst 2014년 10월 23일
First of all, img better be a double image. If it's a uint8 image, or even a double image that was created from a uint8 image with something like imdouble(img) or img/max(img(:)) then you're not going to get more than 256 threshold, not 501. You can do this:
thld = 0 : .001 : .5;
numWhitePixels = zeros(1, length(thld));
numBlackPixels = zeros(1, length(thld));
for idx = 1:numel(thld)
thisImage = im2bw(img, thld(idx));
numWhitePixels(idx) = sum(thisImage(:));
numBlackPixels(idx) = numel(thisImage) - numWhitePixels(idx);
% Save to a cell array only if
% you need all images after the loop has exited.
a{idx} = thisImage;
end
jchris14
jchris14 2014년 10월 23일
편집: jchris14 2014년 10월 23일
I am trying to quantify a TIF image. But this is the error i get when i run the code.
Error using im2bw Expected input number 1, I, X or RGB, to be one of these types:
single, uint8, uint16, int16, logical, double
Instead its type was char.
Error in im2bw>parse_inputs (line 79)
validateattributes(varargin{1},...
Error in im2bw (line 38)
[A,map,level] = parse_inputs(varargin{:});
Error in R1 (line 20) thisImage = im2bw(img
Your img is a character string. Did you pass the filename to im2bw() instead of the image array? It should look like this:
img = imread(fullFileName);
binaryImage = im2bw(img,..............
My code says
a= uigetfile('*.tif');
thld = 0 : .001 : .5;
numWhitePixels = zeros(1, length(thld));
numBlackPixels = zeros(1, length(thld));
for idx = 1:numel(thld)
thisImage = im2bw(a, thld(idx));
numWhitePixels(idx) = sum(thisImage)
numBlackPixels(idx) = numel(thisImage) - numWhitePixels(idx)
% Save to a cell array only if
% you need all images after the loop has exited.
%a{idx} = thisImage;
end
Use more descriptive name than a for your variable and you'll see where you go wrong.
You ask for a filename with uigetfile but you never load the image. In addition even if you'd loaded the image in a, you would have overwritten it in the loop since you're also using a for storing the binary image.
Your code should be something like:
imgfilename = uigetfile('*.tif');
sourceimage = imread(imgfilename);
...
binaryimage = im2bw(sourceimage, thld(idx));
binaryimages{idx} = binaryimage;
Image Analyst
Image Analyst 2014년 10월 23일
Yep, like I said, the badly-named "a" is the culprit. It's a string just like I said. Did you use imread() like I suggested in my prior comment? No, not in that code you just posted. Why don't you use my suggestion? It will be alright then if you do.
jchris14
jchris14 2014년 10월 23일
Thank you for the help. I will use them.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 10월 23일

댓글:

2014년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by