Help me understand the matlab bwlabel

10 views (last 30 days)
can anybody explain me what is happening in the code below
[L,Ne]=bwlabel(Bw);
img_wk_bw_L=L==100;
img_wk_bw_L_total=img_wk_bw_L*0;
  4 Comments
atiqa zahid
atiqa zahid on 9 Dec 2019
% clc; % Clear the command window.
% close all; % Close all figures (except those of imtool.)
% Read the image from disk.
im1 = imread('img11_Inp.jpg');
% Test code if you want to try it with a gray scale image.
% Uncomment line below if you want to see how it works with a gray scale image.
% rgbImage = rgb2gray(rgbImage);
% Display image full screen.
%imshow(im1);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(im1)
%==========================================================================
% The first way to divide an image up into blocks is by using mat2cell().
blockSizeR = 128; % Rows in block.
blockSizeC = 128; % Columns in block.
% Figure out the size of each block in rows.
% Most will be blockSizeR but there may be a remainder amount of less than that.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows), rem(rows, blockSizeR)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols), rem(columns, blockSizeC)];
% Create the cell array, ca.
% Each cell (except for the remainder cells at the end of the image)
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(im1, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(im1, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
% Display the original image in the upper left.
% subplot(4, 6, 1);
% imshow(im1);
% title('Original Image');
% Inform user of next stage where we process a gray scale image.
%promptMessage = sprintf('Now I will do the same for a gray scale image.');
%titleBarCaption = 'Continue?';
%button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
%if strcmpi(button, 'Cancel')
% return;
%end
atiqa zahid
atiqa zahid on 9 Dec 2019
i put this code ...but this code creates some extra image divisions of original image.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 11 Apr 2017
It labels the binary image so that each connected blob had an "ID number", called the label. L is an image of pixels with the value being the label that they have been assigned. For example if you have 2 blobs, the not-very-descriptively-named Ne will be 2 and each blob in L will have values of either 1 or 2 for the pixels in that blob.
The next line just creates an image of blob #100. All other blobs are removed.
The next line just creates an image of all zeros, essentially wiping away blob #100 that you just extracted. I don't know why anyone would do that. It's essentially the same as
img_wk_bw_L_total = false(size(L));
an all black/0/false binary image.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!