matlab - 如何裁剪图像中的多个对象 [MATLAB]

标签 matlab image-processing neural-network

我是 MATLAB 的新手,正在使用 MATLAB 和神经网络开发“大米质量识别”应用程序。对于我的指导,我更喜欢这个 Research Paper

此应用程序包含 5 个阶段

  1. 图像采集
  2. 图像预处理
  3. 图像分割和识别感兴趣区域
  4. 特征提取
  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)

enter image description here

第 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);

enter image description here

第 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)

enter image description here

第 4 步:

 % Remove small objects fewer than 30 pixels from binary image
 pure = bwareaopen(BW,30);
 figure, imshow(pure)

enter image description here

第 5 步:标记

% Label Black and white & Image bounding box around each object
L=bwlabel(pure,8);
bb=regionprops(L,'BoundingBox');

我已经坚持第 6 步了 2 天了。第 6 步是使用带标签的二值图像从原始图像中裁剪多个对象

确切的输出应该如下图所示,

enter image description here

如果我能得到这个,我就可以轻松计算原始图像中每个对象的形态特征和颜色特征,以用于第 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/

相关文章:

php - 如何将 DOM 树转换为图像

machine-learning - 如何规范视频游戏机器学习(神经网络)的输入

python - Inverse_transform方法(LabelEncoder)

android - 图像内存不足错误

computer-vision - 从航拍图像绘制房屋轮廓

matlab - 基于反三角函数的成本函数

java - 检测图像中的物体(单词)

matlab - 如何在 MATLAB 中根据另一列聚合一列?

matlab - 脚本中的语句可以停止 Matlab M 脚本吗?

c# - 不安全边缘检测仍然很慢