【问题标题】:How to find Correlation of a grayscale image如何找到灰度图像的相关性
【发布时间】:2012-05-10 22:09:42
【问题描述】:

有一个固定大小为 256*256 的图像 A。我能够找到水平和垂直相邻像素之间的相关性。但我不明白如何准确地从图像中随机选择 4096 对两个对角相邻像素,计算它们的相关系数,然后绘制这些对角相邻像素的分布。

xed = A(1:end-1,1:end-1);  % All but the last row and column
yed = A(2:end,2:end);      % All but the first row and column

randIndex = randperm(numel(xed));  % A random permutation of the integers from 1 to numel(x)
randIndex = randIndex(1:4096);     % Pick the first 4096 indices
xRand = xod(randIndex);            % 4096 random values from x
yRand = yod(randIndex);            % The corresponding 4096 values from y

% Compute the Correlation coefficient of x and y
red_xy = corrcoef(xRand(:),yRand(:));

相同的算法用于加密彩色和灰度图像。对于彩色图像,它单独应用于 RGB 平面,仅应用于灰度一次。在彩色图像的情况下,对角相关系数几乎为零。在灰度中,我想知道我在计算相关系数时是否出错。

【问题讨论】:

  • @PaulR 我尝试随机选择水平和垂直像素并计算相关系数。我在 stackoverflow 本身上使用了 gnovice 给出的答案之一。我将它用于彩色图像并获得了令人满意的结果。我不知道将它应用于灰度图像时哪里出错了。
  • @PaulR 相同的算法用于加密彩色和灰度图像。对于彩色图像,它单独应用于 RGB 平面,仅应用于灰度一次。在彩色图像的情况下,对角相关系数几乎为零。在灰度中我想知道我在计算相关系数时是否出错。
  • 再一次,您在 cmets 中发布相关信息 - 将其移至问题中 - 这将有助于人们更好地回答您的问题。
  • 你确定你一次正确地提取了一个颜色平面吗?您是否查看过xRandyRand 中的值,看看它们看起来是否合理?此处的代码显示 xodyod 而不是 xedyed 是否只是此处的转录错误,或者这可能是您的问题的原因?
  • @GarethMcCaughan 上面的代码是我为灰度图像尝试的代码。对于彩色图像,我仅使用了 stackoverflow 上提供的代码。上面的代码就是对灰度图的修改。

标签: matlab image-processing computer-vision


【解决方案1】:

我希望这是真的,并且每个人都会有所帮助。对不起我的英语。

%usage [k1,k2,k3,k4,k5,k6]=resim_korelasyon('lennagri.bmp','lenagrisifreli1.bmp',0);
%k1,k2,k3 Original Image correlation coefficient
%k4,k5,k6 encrypted image correlation coefficient
%color  ==> 0 gray , 1 RGB
%kyatayO,kdikeyO,kkosegenO,kyatayI,kdikeyI,kkosegenI  correlation coefficients

function [kyatayO,kdikeyO,kkosegenO,kyatayI,kdikeyI,kkosegenI]=resim_korelasyon(ImageOriginal,ImageEncrypted,color) 
 %Original Image
 I=imread(ImageOriginal);
 A = im2double(I);
 %encrypted image
 I2=imread(ImageEncrypted);
 A2 = im2double(I2);

 if (color==0)
    %For GRAY image
    %==================================================
    %Original Image
    %horizontal
    x1 = A(:,1:end-1);  
    y1 = A(:,2:end);
    kyatayO=hesap(x1,y1);
    %Vertical
    x2 = A(1:end-1,:);  
    y2 = A(2:end,:);    
    kdikeyO=hesap(x2,y2);
    %diagonal
    x3 = A(1:end-1,1:end-1);  
    y3 = A(2:end,2:end);     
    kkosegenO=hesap(x3,y3);

    %==================================================
    %for encrypted image
    %horizontal
    x4 = A2(:,1:end-1);  
    y4 = A2(:,2:end);
    kyatayI=hesap(x4,y4);
    %Vertical
    x5 = A2(1:end-1,:);  
    y5 = A2(2:end,:);    
    kdikeyI=hesap(x5,y5);
    %diagonal
    x6 = A2(1:end-1,1:end-1);  
    y6 = A2(2:end,2:end);     
    kkosegenI=hesap(x6,y6);
    %==================================================
    %graphics
    h=figure;
    subplot(3,2,1),grafik(x1,y1),title('Horizontal');
    subplot(3,2,3),grafik(x2,y2),title('Vertical');
    subplot(3,2,5),grafik(x3,y3),title('Diagonal');
    subplot(3,2,2),grafik(x4,y4),title('Horizontal');
    subplot(3,2,4),grafik(x5,y5),title('Vertical');
    subplot(3,2,6),grafik(x6,y6),title('Diagonal');
    saveas(h,'correlationGray.jpg');
 end

 if(color==1) %For RGB
            %==================================================
            %Orjinal Görüntü İçin
            %Yatay korelasyon
            x1 = A(:,1:end-1,1);  %RED değerine göre hesaplanır. 1 RED 2 GREEN 3 BLUE
            y1 = A(:,2:end,1); 
            kyatayO=hesap(x1,y1);
            %dikey korelasyon
            x2 = A(1:end-1,:,1);  
            y2 = A(2:end,:,1);  
            kdikeyO=hesap(x2,y2);
            %diagonal / çapraz kolerasyon (Sağ üst köşeden sola)
            x3 = A(1:end-1,1:end-1,1);  
            y3 = A(2:end,2:end,1);  
            kkosegenO=hesap(x3,y3);
            %======================================================
            %İşlenmiş Görüntü İçin
            %Yatay korelasyon
            x4 = A2(:,1:end-1,1);  %RED değerine göre hesaplanır. 1 RED 2 GREEN 3 BLUE
            y4 = A2(:,2:end,1); 
            kyatayI=hesap(x4,y4);
            %dikey korelasyon
            x5 = A2(1:end-1,:,1);  
            y5 = A2(2:end,:,1);  
            kdikeyI=hesap(x5,y5);
            %diagonal / çapraz kolerasyon (Sağ üst köşeden sola)
            x6 = A2(1:end-1,1:end-1,1);  
            y6 = A2(2:end,2:end,1);  
            kkosegenI=hesap(x6,y6);
            %==================================================
            %grafikler çizdiriliyor ve kaydediliyor
            h=figure;
            subplot(3,2,1),grafik(x1,y1),title('Horizontal');
            subplot(3,2,3),grafik(x2,y2),title('Vertical');
            subplot(3,2,5),grafik(x3,y3),title('Diagonal');
            subplot(3,2,2),grafik(x4,y4),title('Horizontal');
            subplot(3,2,4),grafik(x5,y5),title('Vertical');
            subplot(3,2,6),grafik(x6,y6),title('Diagonal');
            saveas(h,'correlationRGB.jpg');
 end  
end

function [correlation_coefficient]=hesap(x,y)
    correlation_coefficient = corrcoef(x(:),y(:));
end

function grafik(x,y)
    randIndex = randperm(numel(x));  
    randIndex = randIndex(1:2000);   
    xRand = x(randIndex);            
    yRand = y(randIndex); 
    xRand = xRand * 256;
    yRand = yRand * 256;  
    scatter(xRand,yRand,'.');
end

【讨论】:

    【解决方案2】:

    (这最初是在(我的)cmets 中,但实际上是答案。我正在将其变成可以接受的实际答案,例如https://meta.stackexchange.com/questions/54718/how-to-handle-questions-which-are-answered-in-the-comments。)

    问题是你的部分数组是这样定义的

    xed = A(1:end-1,1:end-1);
    

    名称为xedyed,但这样使用

    xRand = xod(randIndex);
    

    名称为xodyod。大概你有其他变量,在别处定义,使用这些名称,这就是为什么你的代码会出错而不是失败并出现明显错误的原因。

    这里有一个更普遍的寓意:您可以通过为变量提供更长、信息更丰富和更独特的名称来降低此类错误的风险,这些名称不能通过单字符拼写错误相互转换:-)。

    【讨论】:

    • 谢谢!以后我会好好处理的。
    • 您也应该接受答案,因为它确实回答了您的问题。 (当然我这么说是别有用心的:会给我更多的SO声望点数。)
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2020-07-20
    • 2012-03-19
    • 2015-11-20
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多