Image 裁剪圆形对象内的最大正方形-Matlab
我正试图找到一种方法,从一个圆形物体(图a)中裁剪出一个能放在里面的最大正方形 有人能解释/告诉我如何找到圆(图I)内空白区域的最大方形拟合参数,并根据这些参数裁剪原始图像(图A)中的方形吗 脚本:Image 裁剪圆形对象内的最大正方形-Matlab,image,matlab,image-processing,geometry,image-segmentation,Image,Matlab,Image Processing,Geometry,Image Segmentation,我正试图找到一种方法,从一个圆形物体(图a)中裁剪出一个能放在里面的最大正方形 有人能解释/告诉我如何找到圆(图I)内空白区域的最大方形拟合参数,并根据这些参数裁剪原始图像(图A)中的方形吗 脚本: A = imread('E:/CirTest/Test.jpg'); %imshow(A) level = graythresh(A); BW = im2bw(A,level); %imshow(BW) I = imfill(BW, 'holes'); imshow(I) d = imdistli
A = imread('E:/CirTest/Test.jpg');
%imshow(A)
level = graythresh(A);
BW = im2bw(A,level);
%imshow(BW)
I = imfill(BW, 'holes');
imshow(I)
d = imdistline;
[centers, radii, metric] = imfindcircles(A,[1 500]);
imageCrop=imcrop(A, [BoxBottomX BoxBottomY NewX NewY]);
我为您提供了一个解决方案,但它需要一些额外的工作。我首先要做的是直接在灰度图像上使用
imfill
。这样,均匀区域中的噪声像素将以相同的强度进行修复,因此阈值设置更容易。您仍然可以使用灰度阈值
或Otsu的阈值,并在修复的图像上执行此操作
以下是一些让您开始学习的代码:
figure; % Open up a new figure
% Read in image and convert to grayscale
A = rgb2gray(imread('http://i.stack.imgur.com/vNECg.jpg'));
subplot(1,3,1); imshow(A);
title('Original Image');
% Find the optimum threshold via Otsu
level = graythresh(A);
% Inpaint noisy areas
I = imfill(A, 'holes');
subplot(1,3,2); imshow(I);
title('Inpainted image');
% Threshold the image
BW = im2bw(I, level);
subplot(1,3,3); imshow(BW);
title('Thresholded Image');
上面的代码执行我提到的三个操作,我们可以看到下图:
请注意,经过阈值处理的图像具有需要移除的边界像素,因此我们可以将注意力集中在圆形对象上。可以使用该函数删除边框像素。当我们这样做时:
% Clear off the border pixels and leave only the circular object
BW2 = imclearborder(BW);
figure; imshow(BW2);
。。。我们现在看到这张图片:
不幸的是,存在一些噪声像素,但我们可以很容易地使用形态学,特别是使用小圆盘结构元素的打开操作来移除这些噪声像素。除此之外,使用适当的结构元素应有助于实现以下目的:
% Clear out noisy pixels
SE = strel('disk', 3, 0);
out = imopen(BW2, SE);
figure; imshow(out);
我们现在得到:
此遮罩包含圆形对象的位置,我们现在需要使用它来裁剪原始图像。最后一部分是使用此遮罩确定行和列的位置,以定位原始图像的左上角和右下角,从而对其进行裁剪:
% Find row and column locations of circular object
[row,col] = find(out);
% Find top left and bottom right corners
top_row = min(row);
top_col = min(col);
bottom_row = max(row);
bottom_col = max(col);
% Crop the image
crop = A(top_row:bottom_row, top_col:bottom_col);
% Show the cropped image
figure; imshow(crop);
我们现在得到:
这并不完美,但它当然会让你开始。如果您想完整复制和粘贴此文件,并在您的计算机上运行此文件,我们提供:
figure; % Open up a new figure
% Read in image and convert to grayscale
A = rgb2gray(imread('http://i.stack.imgur.com/vNECg.jpg'));
subplot(2,3,1); imshow(A);
title('Original Image');
% Find the optimum threshold via Otsu
level = graythresh(A);
% Inpaint noisy areas
I = imfill(A, 'holes');
subplot(2,3,2); imshow(I);
title('Inpainted image');
% Threshold the image
BW = im2bw(I, level);
subplot(2,3,3); imshow(BW);
title('Thresholded Image');
% Clear off the border pixels and leave only the circular object
BW2 = imclearborder(BW);
subplot(2,3,4); imshow(BW2);
title('Cleared Border Pixels');
% Clear out noisy pixels
SE = strel('disk', 3, 0);
out = imopen(BW2, SE);
% Show the final mask
subplot(2,3,5); imshow(out);
title('Final Mask');
% Find row and column locations of circular object
[row,col] = find(out);
% Find top left and bottom right corners
top_row = min(row);
top_col = min(col);
bottom_row = max(row);
bottom_col = max(col);
% Crop the image
crop = A(top_row:bottom_row, top_col:bottom_col);
% Show the cropped image
subplot(2,3,6);
imshow(crop);
title('Cropped Image');
。。。我们的最终数字是:
您可以使用L\u inf
距离(也称为'chessboard'
)获得到区域边缘的轴对齐距离,从而得出最大有界框的尺寸:
bw = imread('http://i.stack.imgur.com/7yCaD.png');
lb = bwlabel(bw);
reg = lb==2; %// pick largest area
d = bwdist(~reg,'chessboard'); %// compute the axis aligned distance from boundary inward
r = max(d(:)); %// find the largest distance to boundary
[cy cx] = find(d==r,1); %// find the location most distant
boundedBox = [cx-r, cy-r, 2*r, 2*r];
结果是
figure;
imshow(bw,'border','tight');
hold on;
rectangle('Position', boundedBox, 'EdgeColor','r');
获得边界框后,可以使用裁剪原始图像
imageCrop = imcrop(A, boundedBox);
或者,你可以
imageCrop = A(cy + (-r:r-1), cx + (-r:r-1) );
如果
imfindcircles
为您提供了一个中心和一个半径,您就不能从中计算出边界框了吗?把它画在一张纸上,再看一眼。(imfindcircles在这类图像上是否正确工作是另一回事)。非常感谢,令人印象深刻。我添加了out=bwareaopen(out,500);在“清除噪声像素百分比”区域中。链接:。方盒形裁剪是否可能位于圆内,如图中所示。此外,作物怎么可能是圆圈内最大的正方形。这是最大的部分。感谢耐心,但我的意思是链接中最大的正方形(链接中标记为红色)。很抱歉,我看不到图片。顺便说一句,使用bwreaopeni做得很好,我得到了这个错误“未定义的函数或变量'lb'。Untitled2(第2行)中的错误reg=lb==2;%//选择最大区域”。==是什么意思?你所说的lb=be标签(be)是什么意思;?谢谢,如何将boundedBox=[cx-r,cy-r,2*r,2*r]行转换为crop=A(顶行:底行,顶列:底列)结构?做得好。我不明白OP想要什么,链接也不起作用。@Shai我一定会试试:)给我一点时间