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
Image Analyst
Image Analyst 2014 年 10 月 23 日
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,..............
jchris14
jchris14 2014 年 10 月 23 日
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
Guillaume
Guillaume 2014 年 10 月 23 日
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 ExchangeLoops 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