尝试访问位置向量时索引超出矩阵维度
Index exceeds matrix dimensions when trying to access position vectors
我只是在读取图像,并希望可视化由 returns 位置向量的 matlab 斑点分析返回的边界框。
这是我的代码
img = imread(file_name);
img = im2bw(img);
gblob = vision.BlobAnalysis('AreaOutputPort', true, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 0, ...
'MaximumBlobArea', 600000, ...
'MaximumCount', 1000, ...
'MajorAxisLengthOutputPort',true, ...
'MinorAxisLengthOutputPort',true, ...
'OrientationOutputPort',true);
[Area,centroid, bbox, MajorAxis, MinorAxis,Orientation] = step(gblob, img);
% each bbox is position vector of the form [x y width height]
for i = 1:1:length(MajorAxis)
figure;imshow(img(bbox(i,2):bbox(i,2) + bbox(i,4),bbox(i,1):bbox(i,1)+bbox(i,3)));
end
这样做我得到一个错误Index exceeds matrix dimensions.
我也试过
figure;imshow(img(bbox(i,1):bbox(i,1) + bbox(i,3),bbox(i,2):bbox(i,2)+bbox(i,4)));
但我仍然遇到同样的错误。
这是一个示例图像,其中此代码给出了错误
这是一个简单的错误索引案例。斑点检测器 returns 斑点左上角的 x
和 y
坐标。 x
在这种情况下是 水平 坐标,而 y
是 垂直 坐标。因此,您只需要交换访问图像的方式,因为首先需要垂直,然后是水平。
另外关于你的图像,我会反转图像,因为一旦你将它转换为二进制,该对象将被视为白色背景上的深色对象。斑点检测器通过检测黑色背景上的白色物体来工作。因此,一旦发生这种情况,请反转图像并进行一些形态学关闭以清除噪声:
img = imread('http://www.aagga.com/wp-content/uploads/2016/02/Sample.jpg');
img = ~im2bw(img); %// Change - invert
img_clean = imclose(img, strel('square', 7)); %// Change, clean the image
我现在得到这张图片:
imshow(img_clean);
不错....现在 运行 你真正的斑点检测器。请注意,您放入斑点检测器中的图像是 运行 变量名称。您现在需要将其命名为 img_clean
:
gblob = vision.BlobAnalysis('AreaOutputPort', true, ...
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 0, ...
'MaximumBlobArea', 600000, ...
'MaximumCount', 1000, ...
'MajorAxisLengthOutputPort',true, ...
'MinorAxisLengthOutputPort',true, ...
'OrientationOutputPort',true);
[Area,centroid, bbox, MajorAxis, MinorAxis,Orientation] = step(gblob, img_clean);
现在终于提取出每个 blob:
% each bbox is position vector of the form [x y width height]
for i = 1:1:length(MajorAxis)
figure;
imshow(img_clean(bbox(i,2):bbox(i,2) + bbox(i,4),bbox(i,1):bbox(i,1)+bbox(i,3))); %// Change
end
我现在得到以下9个数字:
请注意,上面的内容并不完美,因为标志的周边是断开的,所以斑点检测器会将其解释为不同的斑点。解决此问题的一种方法是改变图像的阈值,或者使用 graythresh
执行自适应阈值处理,以便确保正确连接边界。但是,这是开始解决您的问题的好方法。
小注
一种更简单的方法是取消计算机视觉工具箱并使用图像处理工具箱。具体使用 regionprops
并使用 Image
属性 提取出 blob 本身包含的实际图像:
%// Code from before
img = imread('http://www.aagga.com/wp-content/uploads/2016/02/Sample.jpg');
img = ~im2bw(img); %// Change - invert
img_clean = imclose(img, strel('square', 7)); %// Change, clean the image
%// Create regionprops structure with desired properties
out = regionprops(img_clean, 'BoundingBox', 'Image');
%// Cycle through each blob and show the image
for ii = 1 : numel(out)
figure;
imshow(out(ii).Image);
end
这应该实现与我上面向您展示的相同的效果。您可以查看文档以获取有关每个 blob 适合您的属性类型 regionprops
returns 的更多详细信息。
我只是在读取图像,并希望可视化由 returns 位置向量的 matlab 斑点分析返回的边界框。 这是我的代码
img = imread(file_name);
img = im2bw(img);
gblob = vision.BlobAnalysis('AreaOutputPort', true, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 0, ...
'MaximumBlobArea', 600000, ...
'MaximumCount', 1000, ...
'MajorAxisLengthOutputPort',true, ...
'MinorAxisLengthOutputPort',true, ...
'OrientationOutputPort',true);
[Area,centroid, bbox, MajorAxis, MinorAxis,Orientation] = step(gblob, img);
% each bbox is position vector of the form [x y width height]
for i = 1:1:length(MajorAxis)
figure;imshow(img(bbox(i,2):bbox(i,2) + bbox(i,4),bbox(i,1):bbox(i,1)+bbox(i,3)));
end
这样做我得到一个错误Index exceeds matrix dimensions.
我也试过
figure;imshow(img(bbox(i,1):bbox(i,1) + bbox(i,3),bbox(i,2):bbox(i,2)+bbox(i,4)));
但我仍然遇到同样的错误。
这是一个示例图像,其中此代码给出了错误
这是一个简单的错误索引案例。斑点检测器 returns 斑点左上角的 x
和 y
坐标。 x
在这种情况下是 水平 坐标,而 y
是 垂直 坐标。因此,您只需要交换访问图像的方式,因为首先需要垂直,然后是水平。
另外关于你的图像,我会反转图像,因为一旦你将它转换为二进制,该对象将被视为白色背景上的深色对象。斑点检测器通过检测黑色背景上的白色物体来工作。因此,一旦发生这种情况,请反转图像并进行一些形态学关闭以清除噪声:
img = imread('http://www.aagga.com/wp-content/uploads/2016/02/Sample.jpg');
img = ~im2bw(img); %// Change - invert
img_clean = imclose(img, strel('square', 7)); %// Change, clean the image
我现在得到这张图片:
imshow(img_clean);
不错....现在 运行 你真正的斑点检测器。请注意,您放入斑点检测器中的图像是 运行 变量名称。您现在需要将其命名为 img_clean
:
gblob = vision.BlobAnalysis('AreaOutputPort', true, ...
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 0, ...
'MaximumBlobArea', 600000, ...
'MaximumCount', 1000, ...
'MajorAxisLengthOutputPort',true, ...
'MinorAxisLengthOutputPort',true, ...
'OrientationOutputPort',true);
[Area,centroid, bbox, MajorAxis, MinorAxis,Orientation] = step(gblob, img_clean);
现在终于提取出每个 blob:
% each bbox is position vector of the form [x y width height]
for i = 1:1:length(MajorAxis)
figure;
imshow(img_clean(bbox(i,2):bbox(i,2) + bbox(i,4),bbox(i,1):bbox(i,1)+bbox(i,3))); %// Change
end
我现在得到以下9个数字:
请注意,上面的内容并不完美,因为标志的周边是断开的,所以斑点检测器会将其解释为不同的斑点。解决此问题的一种方法是改变图像的阈值,或者使用 graythresh
执行自适应阈值处理,以便确保正确连接边界。但是,这是开始解决您的问题的好方法。
小注
一种更简单的方法是取消计算机视觉工具箱并使用图像处理工具箱。具体使用 regionprops
并使用 Image
属性 提取出 blob 本身包含的实际图像:
%// Code from before
img = imread('http://www.aagga.com/wp-content/uploads/2016/02/Sample.jpg');
img = ~im2bw(img); %// Change - invert
img_clean = imclose(img, strel('square', 7)); %// Change, clean the image
%// Create regionprops structure with desired properties
out = regionprops(img_clean, 'BoundingBox', 'Image');
%// Cycle through each blob and show the image
for ii = 1 : numel(out)
figure;
imshow(out(ii).Image);
end
这应该实现与我上面向您展示的相同的效果。您可以查看文档以获取有关每个 blob 适合您的属性类型 regionprops
returns 的更多详细信息。