如何找到图像的相关性? [英] How to find correlation of an image?

查看:198
本文介绍了如何找到图像的相关性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图片 A 固定尺寸​​256 * 256 * 3(RGB)。一个图像中两个相邻像素值 x,y 之间的协方差的数学公式通常被称为:

There is an image A of fixed size 256*256*3 (RGB). The mathematical formula for covariance between two adjacent pixels values x,y in an image is popularly known to be:

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 是这两个像素的两个水平,垂直和对角相邻像素之间的相关系数图片。

where r_xy is the correlation coefficients between two horizontally, vertically, and diagonally adjacent pixels of these two images.

Q1:如何在MATLAB中进行上述计算?

Q1: How to do the above computation in MATLAB?

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

Q2: How to randomly select say 5000 pairs of two horizontally adjacent pixels from the images and then plot the distribution of these two horizontally adjacent pixels?

推荐答案

truecolor RGB图像,首先要解决几个关键问题。我在我的回答中提到了这些a href =https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab>您的另一个问题涉及不同的图像处理算法,但他们承担在这里重复:

As is typical with image processing for truecolor RGB images, there are a couple of key issues to first address. I mentioned these in my answer to your other question involving a different image processing algorithm, but they bear repeating here:


  • 弄清楚如何处理第三维: RGB图像实际上是一组三个2-D矩阵(每个矩阵用于像素的红色,绿色和蓝色分量)沿第三维连接。在执行逐像素操作时,您必须决定是否要执行三次操作(即每个颜色平面执行一次),或者是否要以某种方式沿第三维折叠值(即转换为灰度强度图像,其功能如 RGB2GRAY )为您提供一组二维图像数据进行操作。

  • 注意数据类型:加载到MATLAB中的图像数据通常,形式为无符号8位整数,但有时它可以是无符号的16位整数或双精度精密型。处理整数类型时,通常需要转换为双倍精度执行某些操作以避免整数运算的某些方面,如舍入和饱和。

  • Figuring out how to deal with the third dimension: An RGB image is actually a set of three 2-D matrices (one each for the red, green, and blue color components of the pixels) concatenated along a third dimension. When performing pixel-wise operations, you have to decide whether you are going to perform the operations three times (i.e. once for each color plane) or whether you are going to somehow collapse the values along the third dimension (i.e. converting to a grayscale intensity image with functions like RGB2GRAY) to give you a set of 2-D image data to operate on.
  • Be mindful of data types: Image data loaded into MATLAB is typically in the form of an unsigned 8-bit integer, but sometimes it can be an unsigned 16-bit integer or a double precision type. When dealing with integer types, conversion to double precision is usually desired before performing certain operations in order to avoid certain aspects of integer arithmetic such as round-off and saturation.

好了,现在这些手续是在此之外,我认为您的问题包含两个步骤。首先,您必须从图像中选择成对像素的子集,例如所有水平配对像素。其次,您必须应用上面的统计公式。在下面的例子中,我假设操作是在矩阵的红色(即第一个)颜色平面上执行的 A

Alright, now that those formalities are out of the way, I see your problem above as comprising two steps. First, you have to select subsets of paired pixels from the image, such as all horizontally-paired pixels. Second, you have to apply the statistical formulae you have above. In the examples below, I'll assume the operations are being performed on the red (i.e. first) color plane of the matrix A:


  1. 选择成对像素的子集:让我们从一组独特的水平像素对开始。如果我选择 A 的第一列中的像素并将它们放在子集 x 中,那么水平相邻的像素将是 A 的第二列中的那些,这些将放在子集 y 中。我还可以将第二列中的像素添加到子集 x 中,然后将第三列中的水平相邻像素放在子集中ÿ。对 A 中的所有列重复此操作,我们可以看到第1列到第255列中的像素将在子集 x 中,第2列到第256列中的像素将位于子集 y 中。因此,矩阵索引将如下所示:

  1. Selecting subsets of paired pixels: Let's start with the set of unique horizontal pairings of pixels. If I select the pixels in the first column of A and place them in the subset x, then the horizontally adjacent pixels will be those in the second column of A, and these will be placed in the subset y. I can also add the pixels in the second column to the subset x, and the horizontally adjacent pixels in the third column would then be placed in the subset y. Repeating this for all columns in A, we can see that the pixels in columns 1 through 255 will be in subset x, and the pixels in columns 2 through 256 will be in the subset y. The matrix indexing would therefore look like this:

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

遵循类似的逻辑,你可以构造这种方式的整个独特的垂直像素配对:

Following similar logic as above, you can construct the entire set of unique vertical pairings of pixels in this fashion:

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

同样对于唯一对角线配对的集合像素,其中对角线在矩阵中从左上角到右下角:

And likewise for the set of unique diagonal pairings of pixels, where "diagonal" runs from top left to bottom right in the matrix:

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

或反对角线,其中对角线在矩阵中从左下角到右上角运行:

Or for "anti-diagonals", where "diagonal" runs from bottom left to top right in the matrix:

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代替每行中的最后一个索引,分别得到绿色和蓝色平面的计算。

Now, you can choose any one of these sets of x and y data to perform the statistical calculations you want for the red color plane. You can repeat the above substituting 2 or 3 for the last index in each line to get the calculation for the green and blue color planes, respectively.

执行统计测试:这部分很简单。已经有一个内置函数 CORRCOEF 用于计算相关系数在MATLAB中。您可能必须首先使用 x 和 y 重新整形为列向量://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egbu-1\"rel =nofollow noreferrer>单冒号索引

Performing the statistical tests: This part is simple. There is already a built-in function CORRCOEF for computing the correlation coefficient in MATLAB. You may have to reshape the subsets of pixel values x and y into column vectors first using single-colon indexing:

r_xy = corrcoef(x(:),y(:));

其他公式的函数也存在: MEAN E(x) VAR D(x) COV cov(x, y)

Functions also exist for the other formulae as well: MEAN for E(x), VAR for D(x), and COV for cov(x,y).

关于第二个问题,你可以先创建 x y 正如我上面对所有独特的水平相邻像素对所做的那样,然后创建一个带有随机排列的向量的向量整数索引到 x y 使用函数 RANDPERM 。选择那些随机置换索引的前5000个条目将为您提供5000个随机索引到 x y

In regard to your second question, you can first create x and y as I did above for all unique pairs of horizontally adjacent pixels, then create a vector with a random permutation of the integer indices into x and y using the function RANDPERM. Selecting the first 5000 entries of those randomly permuted indices will give you 5000 random indices into x and y:

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

这将为您提供5000对水平相邻的像素值 x y 。但是,目前还不清楚绘​​制分布是什么意思。我猜你最终会使用 HIST 或也许是为此目的 SCATTER 的功能。

This will give you your 5000 pairs of horizontally adjacent pixel values in x and y. However, it is unclear what you mean by "plot the distribution". I'm guessing you will either end up using the function HIST or perhaps the function SCATTER for this purpose.

这篇关于如何找到图像的相关性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆