有一个固定大小为 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?
发布评论
评论(1)
与典型的真彩色 RGB 图像,有几个关键问题需要首先解决。我在我的回答中提到了这些< a href="https://stackoverflow.com/questions/3690246/code-not-working-for-image-processing-in-matlab">您的其他问题涉及不同的图像处理算法,但它们具有此处重复:
好吧,既然这些手续已经完成,我认为您上面的问题包括两个步骤。首先,您必须从图像中选择配对像素的子集,例如所有水平配对像素。其次,您必须应用上面的统计公式。在下面的示例中,我假设操作是在矩阵
A
的红色(即第一个)颜色平面上执行的:选择配对像素的子集: 让我们从一组独特的水平像素对开始。如果我选择
A
第一列中的像素并将它们放入子集x
中,则水平相邻的像素将是A 第二列中的像素
,并且这些将被放置在子集y
中。我还可以将第二列中的像素添加到子集x
中,然后将第三列中的水平相邻像素放置在子集y
中。对A
中的所有列重复此操作,我们可以看到第 1 至 255 列中的像素将位于子集x
中,第 2 至 256 列中的像素将位于子集y
。因此,矩阵索引将如下所示:按照与上面类似的逻辑,您可以以这种方式构造整套独特的垂直像素对:
同样,对于一组独特的对角线像素对,其中“对角线”在矩阵中从左上角延伸到右下角:
或者对于“反对角线”,其中“对角线”在矩阵中从左下角延伸到右上角:
现在,您可以选择这些
x
和y
数据组中的任意一组来对红色平面执行所需的统计计算。您可以重复上述操作,将每行中的最后一个索引替换为 2 或 3,以分别计算绿色和蓝色平面。执行统计测试:这部分很简单。已经有一个内置函数 CORRCOEF 用于计算相关系数在 MATLAB 中。您可能必须首先使用 单冒号索引:
其他公式也存在函数:MEAN对于
E(x)
,VAR对于D(x)
和 COV forcov(x,y)
。关于你的第二个问题,你可以首先像我上面那样为所有唯一的水平相邻像素对创建
x
和y
,然后创建一个具有随机排列的向量使用函数 x 和y
="nofollow noreferrer">RANDPERM。选择这些随机排列索引的前 5000 个条目将为您提供x
和y
的 5000 个随机索引:这将为您提供
中的 5000 对水平相邻像素值>x
和y
。但是,尚不清楚“绘制分布”是什么意思。我猜您最终会使用函数 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:
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
: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 subsetx
, then the horizontally adjacent pixels will be those in the second column ofA
, and these will be placed in the subsety
. I can also add the pixels in the second column to the subsetx
, and the horizontally adjacent pixels in the third column would then be placed in the subsety
. Repeating this for all columns inA
, we can see that the pixels in columns 1 through 255 will be in subsetx
, and the pixels in columns 2 through 256 will be in the subsety
. The matrix indexing would therefore look like this:Following similar logic as above, you can construct the entire set of unique vertical pairings of pixels in this fashion:
And likewise for the set of unique diagonal pairings of pixels, where "diagonal" runs from top left to bottom right in the matrix:
Or for "anti-diagonals", where "diagonal" runs from bottom left to top right in the matrix:
Now, you can choose any one of these sets of
x
andy
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.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
andy
into column vectors first using single-colon indexing:Functions also exist for the other formulae as well: MEAN for
E(x)
, VAR forD(x)
, and COV forcov(x,y)
.In regard to your second question, you can first create
x
andy
as I did above for all unique pairs of horizontally adjacent pixels, then create a vector with a random permutation of the integer indices intox
andy
using the function RANDPERM. Selecting the first 5000 entries of those randomly permuted indices will give you 5000 random indices intox
andy
:This will give you your 5000 pairs of horizontally adjacent pixel values in
x
andy
. 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.