im2bw help: image threshold array

1 view (last 30 days)
Morpheuskibbe
Morpheuskibbe on 29 May 2016
Commented: Image Analyst on 29 May 2016
Is there any way to get the output of im2bw to go into an array?
I know the function BW = im2bw(I, level) produces a black white image and that bwboundaries(bw) will then draw the boundarys
I want to do this a bunch for the same image when arrays don't seem to work? my code looks like this.
I=imread('matmap.bmp');
b=I(:,:,3);
for a=1:1:20
bw(a)=im2bw(b, a/20);
end
imshow(I)
hold on
for k = 1:numel(bw)
bb(k)=bwboundaries(bw);
plot(bb{k}(:,2), bb{k}(:,1), 'b', 'Linewidth', 1)
end

Answers (1)

Image Analyst
Image Analyst on 29 May 2016
Why are you doing two separate loops??? You need to put the call to bwboundaries() immediately after im2bw() - right there in the same loop.
  2 Comments
Morpheuskibbe
Morpheuskibbe on 29 May 2016
you mean something like this?
for a=1:1:20
bb(a)=bwboundaries(im2bw(b, a/20));
end
still doesn't seem to like me using an array
Image Analyst
Image Analyst on 29 May 2016
No. Here's a full demo:
% rgbImage = imread('peppers.png');
rgbImage = imread('onion.png');
% Extract blue channel.
b = im2double(rgbImage(:,:,3));
subplot(5,5, 1);
imshow(rgbImage)
subplot(5,5, 2);
imshow(b);
title('Blue Channel', 'FontSize', 13);
for a = 1 : 20
bw = im2bw(b, a/20);
subplot(5,5, a + 2);
imshow(bw);
caption = sprintf('a/20 = %.4f', a/20);
title(caption, 'FontSize', 13);
hold on;
boundaries = bwboundaries(bw);
for k = 1 : length(boundaries)
plot(boundaries{k}(:,2), boundaries{k}(:,1), 'b', 'Linewidth', 1);
end
drawnow;
end
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!