如何为标记图像放置矩形??使用 CC = bwconncomp(BW) 标记
how to put rectangle for labeled images?? which is labeled using CC = bwconncomp(BW)
我使用 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');
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');
当我这样做并且 运行 上面的代码围绕最大的对象绘制一个矩形时,这就是我得到的:
如果此代码与您的程序相关,不要进行任何屏蔽。用上面的替换掩码代码...所以:
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;
可以使用bwlabel
with regionprops
and rectangle
注解:
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'
图像上,结果为:
我使用 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');
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');
当我这样做并且 运行 上面的代码围绕最大的对象绘制一个矩形时,这就是我得到的:
如果此代码与您的程序相关,不要进行任何屏蔽。用上面的替换掩码代码...所以:
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;
可以使用bwlabel
with regionprops
and rectangle
注解:
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'
图像上,结果为: