Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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:从二值图像裁剪对象_Matlab_Image Processing - Fatal编程技术网

Matlab:从二值图像裁剪对象

Matlab:从二值图像裁剪对象,matlab,image-processing,Matlab,Image Processing,我对matlab是新手,我有一个图像,我想裁剪所有三个圆并存储它们。我的代码适用于图像中的单个圆。但当我在图像中有更多的圆圈时,它就不起作用了 我的代码: im=imread('D:\capture.png'); im_gray = rgb2gray(im); BW = im2bw(im_gray, graythresh(im)); se = strel('disk',3); bw2=imopen(BW,se); bw2=~bw2; s = regionprops(bw2, 'BoundingB

我对matlab是新手,我有一个图像,我想裁剪所有三个圆并存储它们。我的代码适用于图像中的单个圆。但当我在图像中有更多的圆圈时,它就不起作用了

我的代码:

im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
rectangle('Position', s.BoundingBox);
imCrop = imcrop(bw2, s.BoundingBox);
figure, imshow(imCrop);

有什么想法吗?

你几乎可以用了。请记住,如果单独执行
s.BoundingBox
,则仅提取第一个圆。因此,我建议您创建一个单元数组,存储每个边界框的各个圆,然后在所有边界框中运行
for
循环。因此,单元格数组中的每个元素都将是一个裁剪圆。因此,请尝试这样做:

%// Your code
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');

%// New code here
circles = cell(1,numel(s));
for idx = 1 : numel(s)
    rect = s(idx).BoundingBox;
    circles{idx} = imcrop(bw2, rect);
end

现在将是一个裁剪圆的单元格数组。要访问第i个圆圈,只需执行以下操作:

imCrop = circles{i};

编辑 从您的评论中,您希望检测最大和最小的圆。这可以通过检查
regionprops
中的
Area
属性轻松完成。您将找到生成最小和最大面积的边界框。您需要修改
regionprops
调用以包含
区域
标志。因此:

s = regionprops(bw2, 'BoundingBox', 'Area');
[~,indMin] = min([s.Area]);
[~,indMax] = max([s.Area]);

circleSmall = circles{indMin};
circleLarge = circles{indMax};

上面的代码将找到具有最小和最大面积的圆,然后提取相应的圆,假设您已经运行代码来提取我前面编写的
循环中的所有圆。请记住,我必须用方括号括起
s.Area
。之所以这样做是因为当你这样做的时候,你可以将所有区域提取为一个数组,而不是一个单一维度的矩阵,并且
min
/
max
不能处理类似的事情。

裁剪完后,你能告诉我如何从3个圆中检测出最大和最小的圆吗?@James-Yup!我会编辑我的帖子。你只需要找到那个地方。