首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算图像边缘时出错

计算图像边缘时出错
EN

Stack Overflow用户
提问于 2015-03-14 05:57:50
回答 1查看 259关注 0票数 0

我在试着从图像中提取边缘。我使用了以下algorithm.Input图像(E11),也给出了512 * 512灰度图像。

  1. 求输入图像的形态梯度(梯度)
  2. 找出梯度图像的负像(负像)
  3. 使用底部帽子变换(底部)从封闭图像中减去原始图像。
  4. 计算输入图像的平均像素(AVG)
  5. 找出基于AVG的二值图像来平滑图像。
  6. 查找最大的连接区域以查找较大的对象(CC)。
  7. 减去最大区域是从平滑的图像(边缘)。

下面给出了我编写的matlab代码

代码语言:javascript
复制
e11 = imread("lena.jpg");
e11= double(e11);
gradientim = imdilate(e11,se) - imerode(e11,se);
negativeim = imcomplement(gradientim);
bottomhatim = imclose(negativeim,se) -  e11 ;
AVG = mean2(e11);
%-------Computing binary image--------
for i=1:(height)
     for j=1:(width)
         if(AVG > bottomhatim(i,j,:))
              bottomhatim(i,j,:) = 1;
         else
              bottomhatim(i,j,:) = 0;
         end
     end
end
CC = bwconncomp(bottomhatim);
edge = bottomhatim - CC;

在执行步骤7时,由于连接组件(CC)的类型为“struct”,因此将得到如下错误

“未定义函数‘减号’用于输入‘struct’类型的参数”。

是否可以使用"bwconncomp“函数来查找最大的连接区域?是否有其他函数可供选择?请帮助我提前更正此code.Thanks。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-14 06:06:56

假设CC是一个数组,但它实际上是一个结构。具体来说,这就是文档对bwconncomp的看法。

代码语言:javascript
复制
 bwconncomp Find connected components in binary image.
    CC = bwconncomp(BW) returns the connected components CC found in BW. 
    BW is a binary image that can have any dimension. CC is a structure 
    with four fields:

       Connectivity   Connectivity of the connected components (objects).

       ImageSize      Size of BW.

       NumObjects     Number of connected components (objects) in BW.

       PixelIdxList   1-by-NumObjects cell array where the kth element
                      in the cell array is a vector containing the linear
                      indices of the pixels in the kth object.

通过算法描述,您希望找到最大的连接组件,并将其从图像中减去,以获得存储在edge中的最终图像。我建议您使用bwlabel,它返回一个标签映射,它用唯一的ID标记每个不同的对象。bwlabel的第二个输出返回检测到的对象数量。

我会使用bwlabel,结合histc来计算每个区域的像素数。我们将利用它来确定面积最大的地区。然后,您将制作一个由该对象组成的掩码,然后使用该掩码并将其与图像相减以获得最终的输出edge

具体来说,在代码的末尾执行以下操作:

代码语言:javascript
复制
%// Do bwlabel on image
[L, num] = bwlabel(bottomhatim);

%// Count how many values belong to each object, ignoring background
counts = histc(L(:), 1:num);

%// Find which object gives us the largest area
[~,max_id] = max(counts);

%// Make a mask and then subtract
CC = L == max_id;
edge = imsubtract(bottomhatim, CC);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29045952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档