我是 MATLAB 的新手,正在使用 MATLAB 和神经网络开发“大米质量识别”应用程序。对于我的指导,我更喜欢这个 Research Paper
此应用程序包含 5 个阶段
- 图像采集
- 图像预处理
- 图像分割和识别感兴趣区域
- 特征提取
- 培训和测试
我现在处于第三阶段,已经为此应用程序开发了初始步骤
第1步:从计算机浏览图像并显示
% Get the orginal image & show , Figure 1
[fileName, pathName] = uigetfile('*.jpg;*.tif;*.png;*.gif','Select the Picture file');
I = fullfile(pathName, fileName);
I = imread(I);
imshow(I)
第 2 步:背景扣除
% selected rice image Background subtraction , Figure 2
% Use Morphological Opening to Estimate the Background
background = imopen(I,strel('disk',7));
I2 = I - background;
figure, imshow(I2);
第 3 步:
% get the Black and white Image , Figure 3
% output image BW replaces all pixels in the input image with luminance greater than 0.17 level
BW = im2bw(I2,0.17);
figure, imshow(BW)
第 4 步:
% Remove small objects fewer than 30 pixels from binary image
pure = bwareaopen(BW,30);
figure, imshow(pure)
第 5 步:标记
% Label Black and white & Image bounding box around each object
L=bwlabel(pure,8);
bb=regionprops(L,'BoundingBox');
我已经坚持第 6 步了 2 天了。第 6 步是使用带标签的二值图像从原始图像中裁剪多个对象
确切的输出应该如下图所示,
如果我能得到这个,我就可以轻松计算原始图像中每个对象的形态特征和颜色特征,以用于第 4 阶段。
形态特征
1.Area for each Object
2.scale of X, Y axis for each object in above picture
3.using X, Y axis I can Calculate Aspect Ratio
颜色特征
1. Red Mean
2. Green Mean
3. Blue Mean
您能否解释一下如何使用标记二值图像从原始图像中裁剪多个对象,即第 6 步。
最佳答案
如果我对步骤 #6 的解释是正确的,我相信它的意思是他们希望您使用您生成的二进制映射在步骤 #5 之后分割出最终对象。根据您的意见,您还希望提取步骤 #5 中描绘的边界框。如果是这种情况,那么您所要做的就是使用 bb
中定义的 RegionProps
结构,这将帮助我们为您完成此操作。作为对您的一点回顾,从图像中提取的每个对象的 RegionProps
结构的 BoundingBox
字段返回一个由 4 个数字组成的数组,如下所示:
[x y w h]
x
表示列/水平坐标,y
表示行/垂直坐标,w,h
表示边界框的宽度和高度。
您需要做的就是创建一个二值图,并循环遍历每个边界框来描绘我们需要从图像中剪切的位置。完成后,使用此二进制映射来提取像素。换句话说:
%//Initialize map to zero
bMap = false(size(pure));
%//Go through each bounding box
for i = 1 : numel(bb)
%//Get the i'th bounding box
bbox = bb(i).BoundingBox;
%//Set this entire rectangle to true
%//Make sure we cast off any decimal
%//co-ordinates as the pixel locations
%//are integer
bbox = floor(bbox);
bMap(bbox(2):bbox(4), bbox(1):bbox(3)) = true;
end
%//Now extract our regions
out = zeros(size(I));
out = cast(out, class(I)); %//Ensures compatible types
%//Extract cropped out regions for each channel
for i = 1 : size(out,3)
chanOut = out(:,:,i);
chanIm = I(:,:,i);
chanOut(bMap) = chanIm(bMap);
out(:,:,i) = chanOut;
end
这将创建一个存储在 out
中的输出图像,并根据步骤 #5 中给出的每个边界框,仅复制每个 channel 中真实的像素。
我相信这就是第 6 步所讨论的内容。如果我正确地解释了这一点,请告诉我。
关于matlab - 如何裁剪图像中的多个对象 [MATLAB],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23960882/