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
采纳的回答
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 个评论
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
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
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;
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.
Thank you for the help. I will use them.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
