image to binary issues

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

 Risposta accettata

Guillaume
Guillaume il 23 Ott 2014
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 Commenti

jchris14
jchris14 il 23 Ott 2014
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 il 23 Ott 2014
Modificato: Image Analyst il 23 Ott 2014
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 il 23 Ott 2014
Modificato: jchris14 il 23 Ott 2014
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 il 23 Ott 2014
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 il 23 Ott 2014
Thank you for the help. I will use them.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by