如何找到图像的相关性?

发布于 2024-09-18 07:48:13 字数 512 浏览 4 评论 0 原文

有一个固定大小为 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是两个水平、垂直和对角线之间的相关系数这两个图像的相邻像素。

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

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

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)

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

Q1: How to do the above computation in MATLAB?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2024-09-25 07:48:13

与典型的真彩色 RGB 图像,有几个关键问题需要首先解决。我在我的回答中提到了这些< a href="https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab">您的其他问题涉及不同的图像处理算法,但它们具有此处重复:

  • 弄清楚如何处理第三维: RGB 图像实际上是一组三个二维矩阵(每个矩阵代表像素的红色、绿色和蓝色分量)沿着第三个维度串联起来。当执行逐像素操作时,您必须决定是否要执行三次操作(即每个颜色平面一次),或者是否要以某种方式沿第三维折叠值(即转换为 灰度强度图像,具有诸如 RGB2GRAY) 为您提供一组要操作的二维图像数据。
  • 注意数据类型:加载到 MATLAB 中的图像数据通常采用 无符号8位整数,但有时也可以是无符号16位整数或双精度类型。处理整数类型时,通常需要先转换为 双精度执行某些操作以避免整数算术的某些方面,例如舍入和饱和。

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

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

    x = A(:,1:end-1,1); %# 红色平面上的所有行和列 1 到 255
    y = A(:,2:结束,1); %# 红色平面中第 2 到 256 行和列的所有行和列
    

    按照与上面类似的逻辑,您可以以这种方式构造整套独特的垂直像素对:

    x = A(1:end-1,:,1); %# 第 1 行到第 255 行以及红色平面上的所有列
    y = A(2:结束,:,1); %# 第 2 行到第 256 行以及红色平面上的所有列
    

    同样,对于一组独特的对角线像素对,其中“对角线”在矩阵中从左上角延伸到右下角:

    x = A(1:end-1,1:end-1,1); %# 除了最后一行和最后一列之外的所有内容
    y = A(2:结束,2:结束,1); %# 除了第一行和第一列之外的所有内容
    

    或者对于“反对角线”,其中“对角线”在矩阵中从左下角延伸到右上角:

    x = A(2:end,1:end-1,1); %# 除了第一行和最后一列之外的所有内容
    y = A(1:结束-1,2:结束,1); %# 除了最后一行和第一列之外的所有内容
    

    现在,您可以选择这些 xy 数据组中的任意一组来对红色平面执行所需的统计计算。您可以重复上述操作,将每行中的最后一个索引替换为 2 或 3,以分别计算绿色和蓝色平面。

  2. 执行统计测试:这部分很简单。已经有一个内置函数 CORRCOEF 用于计算相关系数在 MATLAB 中。您可能必须首先使用 单冒号索引

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

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

关于你的第二个问题,你可以首先像我上面那样为所有唯一的水平相邻像素对创建 xy ,然后创建一个具有随机排列的向量使用函数 x 和 y ="nofollow noreferrer">RANDPERM。选择这些随机排列索引的前 5000 个条目将为您提供 xy 的 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

这将为您提供 中的 5000 对水平相邻像素值>xy。但是,尚不清楚“绘​​制分布”是什么意思。我猜您最终会使用函数 HIST 或也许函数 SCATTER 用于此目的。

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:

  • 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.

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. 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
    

    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.

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

    Functions also exist for the other formulae as well: MEAN for E(x), VAR for D(x), and COV for cov(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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文