【问题标题】:How to detect edges of a colored photo?如何检测彩色照片的边缘?
【发布时间】:2016-05-08 08:18:11
【问题描述】:

我正在尝试实现 Mathworks 网站上的盲反卷积算法示例,但在进行边缘检测时我遇到了问题,因为您不能对 RGB 照片使用边缘检测功能。所以我将照片转换为 YUV,但在此之后我不知道我应该按什么顺序进行处理,我什至不知道我是否使用了正确的方法。

我为所有三个 (y,u,v) 应用了 edge() 函数,然后我使用 YUV 到 RGB 方法再次组合它们。没用,我无法获得最终的 WEIGHT 值。

我的代码如下,示例链接位于http://www.mathworks.com/help/images/deblurring-with-the-blind-deconvolution-algorithm.html

Img = imread('image.tif');
PSF = fspecial('motion',13,45); 
Blurred = imfilter(Img,PSF,'circ','conv'); 
INITPSF = ones(size(PSF));
[J P] = deconvblind(Blurred,INITPSF,30);

%   RGB to YUV
R=Img(:,:,1); G=Img(:,:,2); B=Img(:,:,3);
Y=round((R+2*G+B)/4);
U=R-G;
V=B-G;

% finding edges for Y,U,V
WEIGHT1 = edge(Y,'sobel',.28);
se1 = strel('disk',1);
se2 = strel('line',13,45);
WEIGHT1 = ~imdilate(WEIGHT1,[se1 se2]);
WEIGHT1 = padarray(WEIGHT1(2:end-1,2:end-1),[1 1]);

WEIGHT2 = edge(U,'sobel',.28);
se1 = strel('disk',1);
se2 = strel('line',13,45);
WEIGHT2 = ~imdilate(WEIGHT2,[se1 se2]);
WEIGHT2 = padarray(WEIGHT2(2:end-1,2:end-1),[1 1]);

WEIGHT3  = edge(V,'sobel',.28);
se1 = strel('disk',1);
se2 = strel('line',13,45);
WEIGHT3 = ~imdilate(WEIGHT3,[se1 se2]);
WEIGHT3 = padarray(WEIGHT3(2:end-1,2:end-1),[1 1]);


% YUV to RGB again
G=round((WEIGHT1-(WEIGHT2+WEIGHT3)/4));
R=WEIGHT2+G;
B=WEIGHT3+G;
WEIGHT(:,:,1)=G; WEIGHT(:,:,2)=R; WEIGHT(:,:,3)=B;

P1 = P;
P1(find(P1 < 0.01))= 0;

[J2 P2] = deconvblind(Blurred,P1,50,[],double(WEIGHT));
figure, imshow(J2)
title('Newly Deblurred Image');
figure, imshow(P2,[],'InitialMagnification','fit')
title('Newly Reconstructed PSF')  

【问题讨论】:

    标签: matlab image-processing edge-detection


    【解决方案1】:

    最终形式的代码如下,

    Img = imread('image.tif');
    PSF = fspecial('motion',13,45);
    Blurred = imfilter(Img,PSF,'circ','conv');
    INITPSF = ones(size(PSF));
    [J P] = deconvblind(Blurred,INITPSF,30);
    
    eRed = edge(Img(:,:,1), 'sobel');
    eGreen = edge(Img(:,:,2), 'sobel');
    eBlue = edge(Img(:,:,3), 'sobel');
    WEIGHT = eRed | eGreen | eBlue;
    se1 = strel('disk',1);
    se2 = strel('line',13,45);
    WEIGHT = ~imdilate(WEIGHT,[se1 se2]);
    WEIGHT = padarray(WEIGHT(2:end-1,2:end-1),[1 1]);
    figure
    imshow(WEIGHT)
    title('Weight Array')
    
    P1 = P;
    P1(find(P1 < 0.01))= 0;
    
    WEIGHT2 = repmat(WEIGHT,[1 1 3]);
    WEIGHT3 = im2double(WEIGHT2);
    
    [J2 P2] = deconvblind(Blurred,P1,50,[],WEIGHT3);
    figure, imshow(J2)
    title('Newly Deblurred Image');
    figure, imshow(P2,[],'InitialMagnification','fit')
    title('Newly Reconstructed PSF')
    

    我还不能发布图片。所以我给出了输出链接。最后一个输出,新恢复,有问题,正如我在照片标题中指出的那样。我猜,仍然存在数据类型问题。 输出链接http://imgur.com/a/dDF2N

    【讨论】:

      【解决方案2】:

      我不会在这里讨论deconvblind 去模糊,但让我向您展示彩色图像的边缘检测是如何工作的。

      % load an image
      I = imread('peppers.png');
      

      % note that this is a RGB image. 
      e = edge(I, 'sobel');  
      

      会失败,因为 edge 想要一个 2D 图像,而 RGB 或 YUV 图像是 3D 的,因为第三维是颜色通道。

      有几种方法可以解决此问题。一种是将图像转换为灰度,使用

      gray = rgb2gray(I);
      

      然后可以将其传递到边缘,以根据“gray”中的灰度强度返回边缘。

      e = edge(gray,'sobel'); % also try with different thresholds for sobel.
      

      如果您真的对每个通道中的边缘信息感兴趣,您可以简单地将各个通道分别传递到边缘。例如,

      eRed = edge(I(:,:,1), 'sobel'); % edges only in the I(:,:,1): red channel.
      eGreen = edge(I(:,:,2), 'sobel');
      eBlue = edge(I(:,:,3), 'sobel');
      

      然后根据这些eRedeGreeneBlue 的外观,您可能会使用逻辑“或”进行组合,这样如果任何通道独立思考,结果就是优势这是一个优势。

      eCombined = eRed | eGreen | eBlue;
      

      您最初所做的可能是无意的,因为 YUV 色彩空间会扭曲边缘感。 R 平面中的边缘可能不是 Y、U 或 V 平面中的边缘,因此您需要确保使用正确的色彩空间来检测边缘,以便之后可以将它们组合起来,如图所示早先的 RGB 颜色空间。

      【讨论】:

      • 非常感谢。我是图像处理的初学者。我在学校项目中需要这个,而且我完全理解所有内容的时间非常有限,所以我需要紧急运行代码。eRed、eBlue .. . 部分正是我需要的我发现 eCombined 但在下一个函数中我得到“eCombined 必须具有输入图像的大小”。错误。我将如何均衡它们的大小。图像是 ,eCombined 是 。下一个函数,[J2 P2] = deconvblind(Blurred,P1,50,[],double(WEIGHT));
      • 最好的方法可能是使用 repmat:mathworks.com/help/matlab/ref/repmat.html。像 out = repmat(eCombined,[1 1 3]); .您也可能会遇到数据类型问题,在这种情况下,您可以使用 im2double (mathworks.com/help/matlab/ref/im2double.html),例如。
      • 抱歉,但我有点困惑为什么您要使用磁盘后跟一行来执行扩张。我确实认为您在这些操作之前的边缘图像是正确的。另外,我不完全确定您为什么要进行两次 deconvblind 操作。
      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多