首页 文章

如何找到灰度图像的相关性

提问于
浏览
0

有一个固定大小为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平面,仅用于灰度等级一次 . 在彩色图像的情况下获得对角线相关系数几乎为零 . 在灰度级中,我想知道我在计算相关系数时是否出错 .

2 回答

  • 0

    (这最初是在(我)的评论中,但事实证明它实际上是答案 . 我将它变成一个可以接受的实际答案,例如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 . 据推测,你有其他变量,在其他地方定义,这些名称,这就是为什么你的代码做错了而不是失败明显的错误 .

    这里有一个更普遍的道德:你可以通过给你的变量更长,更多信息和更独特的名称来减少这种错误的风险,这些名称不能通过单字符拼写错误相互转换:-) .

  • 2

    我希望这是真的,每个人都很有帮助 . 对不起我的英语 .

    %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
    

相关问题