计算两个(二值)图像之间的相关性
我正在尝试计算模板图像(属于较大图像的一部分的图像)与模板所属图像之间的(交叉?)相关性。
假设模板图像为3x2,大图像为20x20。我首先做的是对两个图像进行灰度化。然后我得到了灰度值的平均值(同样是两者)。之后,我逐像素检查当前像素是否低于或高于平均值。如果它较低,那么我将像素着色为黑色,如果它较高,则像素将为白色。所以基本上这给我留下了一个二值图像。其中 1==白色,0==黑色。
我的模板图像二进制值为:101010
然后在大图像中我开始扫描每个像素以查看它是否与模板匹配。因此,我从大图像中的 x=0、y=0 开始,并将 Y 轴上前两行的 X 轴上的前三个像素与模板图像的像素进行比较。其二进制值为:111010
那么下一步是检查相关性,对吧?现在这对我来说是棘手的部分,因为我不确定我做得是否正确。但这就是我想出的结果:
101010(模板图像)
Sum = 3
Mean = 0.5
Standard Deviation = 4,2
111010(大图像,第一部分)
Sum = 4
Mean = 0,66
Standard Deviation = 2,82
然后我尝试像这样计算相关性:
这给了我以下结果: r = -0,04
由于这个数字根本不接近 1,这意味着不存在密切相关,对吗? 或者也许我必须将它与 n-2 = 临界值进行比较。所以在这种情况下,6-2 = 4。由于它也不接近 4,这也意味着不存在相关性,对吗?
当它接近-1时,这意味着什么,这是否意味着相关性更小?
最重要的是,我的计算正确吗?还是我还缺少一些东西..?
I'm trying to calculate the (cross?) correlation between a template image (an image which is part of a bigger image), to the image which the template belongs to.
Suppose the template image is 3x2 and the large image is 20x20. What i did first is grayscaled both images. Then i got the mean of the gray values (again for both). After that i checked pixel by pixel if the current pixel is lower or higher than the mean. If it's lower then i colored the pixel black, if it's higher then the pixel will be white. So basically this leaves me with a binary image. Where 1==white and 0==black.
My template image binary value is: 101010
Then in the large image i start scanning each pixel to see if it matches the template. So i start at x=0, y=0 in the big image and i compare the first three pixel on the X axis from the first two rows on the Y axis with the ones of the template image. The binary value for that is: 111010
So the next step is to check the correlation, right? Now here's the tricky part for me because i'm not sure if i'm doing it right. But this is what i've come up with:
101010 (template image)
Sum = 3
Mean = 0.5
Standard Deviation = 4,2
111010 (big image, first section)
Sum = 4
Mean = 0,66
Standard Deviation = 2,82
Then i tried to calculate the correlation like so:
Which got me the following result:
r = -0,04
Since this number isn't close to 1 at all, this means there is no close correlation right?
Or maybe i have to compare it to n-2 = the critical value. So in this case, 6-2 = 4. Since it isn't close to 4 either this also means that there is no correlation, right?
And what does it mean when it's close to -1, does this mean that there is even less correlation?
And most important, are my calculation correct..? Or am i still missing something..??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我又看了一遍你的问题。我认为您混淆了 交叉相关 与 卷积。
基本上,当您有两个相同大小的图像并且您想知道它们有多相似时,您可以使用互相关。例如,您可以在视频中的两个后续帧上使用它。互相关产生单个数字。
当您有一个小尺寸的模板图像并且想要在较大尺寸的图像中找到它的位置时,您可以使用卷积(因此,它通常也称为 模板匹配)。卷积产生新的图像。该新图像中的每个像素 (x, y) 表示模板与原始图像位置 (x, y) 的匹配强度。通常,执行卷积后,您会在卷积结果中查找最大值(或多个值),并将其用作模板的检测位置。
鉴于您提出问题的方式,听起来卷积确实是您所追求的。在您的情况下,图像的大小不同,因此您无法真正有意义地计算互相关。
最后,在您弄清楚它是如何工作的之后,您应该安慰自己,大多数图像处理库都包含此功能。不过,每个做过像样图像处理的人都至少实现过一次卷积,只是为了好玩:)
I've had another look at your question. I think you're confusing cross-correlation with convolution.
Basically, you use cross-correlation when you have two images of the same size, and you want to tell how similar you are. For example, you could use it on two subsequent frames in a video. Cross-correlation yields a single number.
You'd use convolution when you have a template image of a small size and you want to find its location in an image of a larger size (for this reason, it's also commonly known as template matching). Convolution yields a new image. Each pixel (x, y) in this new image represents the strength of the match of the template to the original image location (x, y). Typically, after performing convolution, you look for the maximum value (or values) in the convolution result, and use that as the detected locations of the template.
Given the way you've worded your question, it sounds like convolution is indeed what you are after. In your cases, the images are of different sizes, so you can't really calculate cross-correlation meaningfully.
Finally, after you've figured out how it all works, you should comfort yourself in the thought that most image processing libraries handle include this functionality. Everyone who's done any decent image processing has implemented convolution at least once just for the fun of it, though :)