How to find Correlation of a grayscale image

2019-08-26 19:17发布

There is an image A of fixed size 256*256. I am able to find correlation between horizontally and vertically adjacent pixels. But I am not understanding how exactly to randomly select 4096 pairs of two diagonally adjacent pixels from the image, calculate their correlation co-efficient and then plot the distribution of these diagonally adjacent pixels.

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(:));

Same algorithm is used to encrypt both color and grayscale images. For color images it is applied to RGB planes separately and for grayscale only once. Diagonal correlation coefficient almost zero is obtained in case of color image. In grayscale I want to know if I am going wrong in calculating correlation coefficient.

2条回答
放荡不羁爱自由
2楼-- · 2019-08-26 20:08

(This was originally in (my) comments, but turns out actually to be the answer. I'm making it into an actual answer that can be accepted as recommended at, e.g., https://meta.stackexchange.com/questions/54718/how-to-handle-questions-which-are-answered-in-the-comments.)

The problem is that your partial arrays are defined like this

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

with names xed and yed, but used like this

xRand = xod(randIndex);

with names xod and yod. Presumably you have other variables, defined elsewhere, with those names, which is why your code does something wrong instead of failing with an obvious error.

There's a more general moral here: you can reduce the risk of this kind of error by giving your variables longer, more informative and more distinctive names that can't be turned into one another by single-character typos :-).

查看更多
Rolldiameter
3楼-- · 2019-08-26 20:12

I hope that is true, and everyone is helpful. I'm sorry for my English.

%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
查看更多
登录 后发表回答