登录论坛

查看完整版本 : 如何找到图像的相关性?


poster
2019-12-10, 20:48
有一个固定尺寸为256 * 256 * 3(RGB)的图像A众所周知,图像中两个相邻像素值x,y之间的协方差的数学公式为:

cov(x,y) = 1/n summation from i = 1 to n of [E(x_i-E(x))(y_i-E(y))] r_xy = cov(x,y) / (sqrt(D(x)*D(y))) D(x) = 1/n summation from i = 1 to n of square[(x_i - E(x))] E(x) = 1/n summation from i = 1 to n of (x_i) 其中, r_xy是这两个图像的两个水平,垂直和对角线相邻像素之间的相关系数。

问题1:如何在MATLAB中进行上述计算?

Q2:如何从图像中随机选择5000对两个水平相邻像素,然后绘制这两个水平相邻像素的分布?



回答:

正如处理真彩色RGB图像的 (http://www.mathworks.com/help/techdoc/creating_plots/f2-10709.html#f2-12468)典型处理一样,首先要解决几个关键问题。我在回答 (https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab/3691909#3691909) 其他 (https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab)涉及不同图像处理算法的问题时 (https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab) 回答 (https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab/3691909#3691909)了这些问题 (https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab) ,但此处需要重复:


弄清楚如何处理三维: RGB图像实际上是沿着三维连接在一起的一组三个二维矩阵(每个像素的红色,绿色和蓝色分量)。在执行逐像素操作时,您必须决定是要执行三次操作(即每个颜色平面一次)还是要以某种方式折叠第三维的值(即转换为灰度强度) (http://www.mathworks.com/help/techdoc/creating_plots/f2-10709.html#f2-175)具有RGB2GRAY之类的 (http://www.mathworks.com/help/toolbox/images/ref/rgb2gray.html)功能的图像 (http://www.mathworks.com/help/techdoc/creating_plots/f2-10709.html#f2-175) ),以提供一组要操作的二维图像数据。
请注意数据类型:加载到MATLAB中的图像数据通常采用无符号8位整数的形式 (http://www.mathworks.com/help/techdoc/ref/uint8.html) ,但有时它可以是无符号16位整数或双精度类型。在处理整数类型时,通常需要在执行某些操作之前转换为双精度 (http://www.mathworks.com/help/techdoc/ref/double.html) ,以避免整数运算的某些方面(例如舍入和饱和)。
好了,既然这些手续已不复存在,我认为您的上述问题包括两个步骤。首先,您必须从图像中选择配对像素的子集,例如所有水平配对的像素。其次,您必须应用上面的统计公式。在下面的示例中,我假设操作是在矩阵A的红色(即第一个)彩色平面上执行A :


选择成对像素的子集:让我们从一组独特的像素水平配对开始。如果我在A的第一列中选择像素并将其放置在子集x ,则水平相邻的像素将在A的第二列中,并将它们放置在子集y 。我还可以将第二列中的像素添加到子集x ,然后将第三列中水平相邻的像素放置在子集y 。对A所有列重复此操作,我们可以看到第1到255列中的像素将在子集x ,而第2到256列中的像素将在子集y 。因此, 矩阵索引 (http://www.mathworks.com/help/techdoc/math/f1-85462.html#f1-85544)将如下所示:

x = A(:,1:end-1,1); %# All rows and columns 1 through 255 from red plane y = A(:,2:end,1); %# All rows and columns 2 through 256 from red plane 遵循与上述类似的逻辑,您可以按以下方式构造像素的唯一唯一垂直配对的整个集合:

x = A(1:end-1,:,1); %# Rows 1 through 255 and all columns from red plane y = A(2:end,:,1); %# Rows 2 through 256 and all columns from red plane 同样,对于一组独特的像素对角线配对,其中“对角线”从矩阵的左上角到右下角:

x = A(1:end-1,1:end-1,1); %# All but the last row and column y = A(2:end,2:end,1); %# All but the first row and column 或对于“反对角线”,其中“对角线”从矩阵的左下角到右上角:

x = A(2:end,1:end-1,1); %# All but the first row and last column y = A(1:end-1,2:end,1); %# All but the last row and first column 现在,您可以选择这些x和y数据集中的任何一组来执行您想要的红色平面的统计计算。您可以重复上述操作,将每行的最后一个索引替换为2或3,以分别计算绿色和蓝色平面。

执行统计测试:这部分很简单。已经有一个内置函数CORRCOEF (http://www.mathworks.com/help/techdoc/ref/corrcoef.html)用于在MATLAB中计算相关系数。您可能必须首先使用单冒号索引 (http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egbu-1)将像素值x和y的子集重塑为列向量:

r_xy = corrcoef(x(:),y(:)); 其他公式也有函数: E(x) MEAN (http://www.mathworks.com/help/techdoc/ref/mean.html) , D(x) VAR (http://www.mathworks.com/help/techdoc/ref/var.html) , cov(x,y) COV (http://www.mathworks.com/help/techdoc/ref/cov.html) 。

关于第二个问题,您可以像上面我一样为水平相邻像素的所有唯一对创建x和y ,然后使用函数RANDPERM (http://www.mathworks.com/help/techdoc/ref/randperm.html)创建一个整数索引随机排列为x和y的向量 (http://www.mathworks.com/help/techdoc/ref/randperm.html) 。选择那些随机排列的索引的前5000个条目将使您在x和y 5000个随机索引:

randIndex = randperm(numel(x)); %# A random permutation of the integers %# from 1 to numel(x) randIndex = randIndex(1:5000); %# Pick the first 5000 indices xRand = x(randIndex); %# 5000 random values from x yRand = y(randIndex); %# The corresponding 5000 values from y 这将为您提供x和y 5000对水平相邻像素值。但是,不清楚“绘制分布”是什么意思。我猜您将为此目的而使用HIST (http://www.mathworks.com/help/techdoc/ref/hist.html)函数或SCATTER (http://www.mathworks.com/help/techdoc/ref/scatter.html)函数。



更多&回答... (https://stackoverflow.com/questions/3689840)