如何为标记图像放置矩形?使用CC = bwconncomp(BW)标记

时间:2015-04-19 07:22:03

标签: matlab image-processing

我使用CC = bwconncomp(BW);标记了图像,并使用代码

屏蔽了最大区域的标签
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;

现在我想在最大区域周围放置薄矩形框而不是遮住它。怎么做?

完整代码:

f = imread('test.PNG');
subplot(2,2,1); imshow(f,[]); title('Original Image');
for i = 1:3 
    Image = medfilt2(Image,[3 3]);
end
[Image_Num, num] = bwlabel(Image,8);
subplot(2,2,3); imshow(Image); title('after median filtering'); %labeling algorithm
BW=im2bw(Image);
subplot(2,2,4); imshow(BW); title('binary image');
CC = bwconncomp(BW); %area based segmentation
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;
figure;
imshow(BW); title('AFTER AREA BASED MASKING');

2 个答案:

答案 0 :(得分:2)

您可以将bwlabelregionpropsrectangle注释一起使用:

 lb = bwlabel( bw ); %// label each CC
 st = regionprops( lb, 'Area', 'BoundingBox' ); %// get area and bounding box for each region
 [mxa mxi] = max( [st.Area] ); %// find max area region

现在您可以注释

 figure;
 imshow( bw ); hold on;
 rectangle('Position', st(mxi).BoundingBox, 'EdgeColor', 'r' );

'coins.png'图片上,结果如下: enter image description here

答案 1 :(得分:1)

CC.PixelIdxList{idx}会为您提供图像中您的对象所在位置的线性索引。

您可以使用ind2sub将线性索引转换为行和列位置,然后我们可以确定这些行和列索引的左上角和右下角。一旦我们这样做,我们就可以相应地标记您的图像。

因此:

%// Determine row and column locations
[row,col] = ind2sub(size(BW), CC.PixelIdxList{idx});

%// Get top left and bottom right coordinates
topleft_row = min(row);
topleft_col = min(col);
bottomright_row = max(row);
bottomright_col = max(col);

%// Draw a white box around the object
%// Left vertical line
BW(topleft_row:bottomright_row, topleft_col) = true;
%// Right vertical line
BW(topleft_row:bottomright_row, bottomright_col) = true;
%// Top horizontal line
BW(topleft_row, topleft_col:bottomright_col) = true;
%// Bottom horizontal line
BW(bottomright_row, topleft_col:bottomright_col) = true;

以下是使用内置于MATLAB的coins.png的示例。我在图像中读取,阈值并填入孔。

im = imread('coins.png');
BW = im2bw(im, graythresh(im));
BW = imfill(O, 'holes');

当我这样做并运行上面的代码在最大的对象周围绘制一个矩形时,这就是我得到的:

enter image description here


此代码与您的计划相关联,进行任何屏蔽。用上面的代码替换掩蔽代码.​​.....所以:

f = imread('test.PNG');
subplot(2,2,1); imshow(f,[]); title('Original Image');
for i = 1:3 
    Image = medfilt2(Image,[3 3]);
end
[Image_Num, num] = bwlabel(Image,8);
subplot(2,2,3); imshow(Image); title('after median filtering'); %labeling algorithm
BW=im2bw(Image);
subplot(2,2,4); imshow(BW); title('binary image');
CC = bwconncomp(BW); %area based segmentation
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);

%-------- CHANGE HERE

%// Determine row and column locations
[row,col] = ind2sub(size(BW), CC.PixelIdxList{idx});

%// Get top left and bottom right coordinates
topleft_row = min(row);
topleft_col = min(col);
bottomright_row = max(row);
bottomright_col = max(col);

%// Draw a white box around the object
%// Left vertical line
BW(topleft_row:bottomright_row, topleft_col) = true;
%// Right vertical line
BW(topleft_row:bottomright_row, bottomright_col) = true;
%// Top horizontal line
BW(topleft_row, topleft_col:bottomright_col) = true;
%// Bottom horizontal line
BW(bottomright_row, topleft_col:bottomright_col) = true;