Matlab 计算图像边缘时出错

Matlab 计算图像边缘时出错,matlab,image-processing,edge-detection,mathematical-morphology,connected-components,Matlab,Image Processing,Edge Detection,Mathematical Morphology,Connected Components,我试图从图像中提取边缘。我使用了下面的算法。输入图像(e11)也是512*512灰度图像 查找输入图像的形态梯度(gradientim) 查找渐变图像的负片图像(negativeim) 使用底帽变换(bottomhatim)从闭合图像中减去原始图像 计算输入图像的平均像素(平均值) 基于AVG算法找到二值图像,对图像进行平滑处理 查找最大的连接区域以查找较大的对象(CC) 从平滑图像(边缘)中减去最大区域 下面给出了我编写的matlab代码 e11 = imread("lena.jpg"); e

我试图从图像中提取边缘。我使用了下面的算法。输入图像(e11)也是512*512灰度图像

  • 查找输入图像的形态梯度(gradientim)
  • 查找渐变图像的负片图像(negativeim)
  • 使用底帽变换(bottomhatim)从闭合图像中减去原始图像
  • 计算输入图像的平均像素(平均值)
  • 基于AVG算法找到二值图像,对图像进行平滑处理
  • 查找最大的连接区域以查找较大的对象(CC)
  • 从平滑图像(边缘)中减去最大区域
  • 下面给出了我编写的matlab代码

    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”函数是否可用于查找最大的连接区域?是否有其他函数?请帮助我更正此代码。提前感谢。

    您假设
    CC
    是一个数组,但实际上是一个结构。具体来说,这就是文档所说的:

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

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

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

    %// 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);
    
    %// 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);