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 平面,仅应用于灰度一次。在彩色图像的情况下,对角相关系数几乎为零。在灰度中,我想知道我在计算相关系数时是否出错。

4

2 回答 2

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
于 2013-06-13T13:35:22.700 回答
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);

带有名称xedand yed,但像这样使用

xRand = xod(randIndex);

带有名称xodyod. 大概你有其他变量,在其他地方定义了这些名称,这就是为什么你的代码做错了,而不是因为明显的错误而失败。

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

于 2012-05-02T08:55:50.653 回答