Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何放置带标签图像的矩形??使用CC=BWConComp(BW)标记_Matlab_Image Processing - Fatal编程技术网

Matlab 如何放置带标签图像的矩形??使用CC=BWConComp(BW)标记

Matlab 如何放置带标签图像的矩形??使用CC=BWConComp(BW)标记,matlab,image-processing,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(

我使用
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}
将为您提供对象所在图像中位置的线性索引

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

因此:

%// 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;
您可以与和注释一起使用:

 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'
图像上,此结果为:

我的例子就是这样。没有你所有的代码,我无法判断你做错了什么。很抱歉祝你好运!这是我的cmplte代码'f=imread('test.PNG');子地块(2,2,1)imshow(f,[]),i=1:3图像的标题(“原始图像”)=medfilt2(图像[3]);结束[Image_Num,Num]=bwlabel(Image,8);子批次(2,2,3);imshow(图像);标题(“中值过滤后”);%标记算法BW=im2bw(图像);子批次(2,2,4);imshow(BW);标题(“二进制图像”);CC=BWConComp(BW);%基于区域的分割numPixels=cellfun(@numel,CC.PixelIdxList);[最大值,idx]=最大值(numPixels);BW(CC.PixelIdxList{idx})=0;图,imshow(BW);标题(“基于区域的遮罩后”);“更新你的帖子。不要把它放在评论里。去睡觉。对不起@我想我知道发生了什么。您仍在使用遮罩代码遮罩最大的对象。不要那样做。用上面的代码替换那个代码。。。它适合我。你是否考虑过使用
矩形
注释功能?如果有两个对象具有相同的面积,那么它是如何工作的??因为在我的图像中,我有两个具有相同面积的对象…当我屏蔽它们时,它们都会被屏蔽,但当我使用此代码时,其放置矩形仅用于一个对象…@santoshpatil在这种情况下,您需要使用
find
mxi=find([st.area]==max([st.area])
获取面积等于max的区域的所有索引。在循环中为每个这样的区域调用
矩形