简体   繁体   English

在OpenCV中比较强度像素值Vec3b

[英]Compare intensity pixel value Vec3b in OpenCV

I have a 3 channel Mat image, type is CV_8UC3 . 我有一个3通道Mat图像,类型为CV_8UC3 I want to compare, in a loop, the intensity value of a pixel with its neighbours and then set 0 or 1 if the neighbour is greater or not. 我想在一个循环中将一个像素的强度值与其相邻像素进行比较,然后如果相邻像素是否更大,则将其设置为01 I can get the intensity calling Img.at<Vec3b>(x,y) . 我可以得到强度调用Img.at<Vec3b>(x,y)

But my question is: how can I compare two Vec3b ? 但是我的问题是:如何比较两个Vec3b

Should I compare pixels value for every channel (BGR or Vec3b[0] , Vec3b[1] and Vec3b[2] ), and then merge the three channels results into a single Mat object? 我应该比较每个通道(BGR或Vec3b[0]Vec3b[1]Vec3b[2] )的像素值,然后将三个通道结果合并到一个Mat对象中吗?

Me again :) 又是我 :)

If you want to compare (greater or less) two RGB values you need to project the 3-dimensional RGB space onto a plane or axis. 如果要比较(大于或小于)两个RGB值,则需要将3维RGB空间投影到平面或轴上。

Of course, there are many possibilities to do this, but an easy way would be to use the HSV color space. 当然,这样做的可能性很多,但是一种简单的方法是使用HSV颜色空间。 The hue ( H ), however, is not appropriate as a linear order function because it is circular (ie the value 1.0 is identical with 0.0, so you cannot decide if 0.5 > 0.0 or 0.5 < 0.0). 但是,色相( H )不适合用作线性函数,因为它是圆形的(即值1.0与0.0相同,因此无法确定0.5> 0.0还是0.5 <0.0)。 However, the saturation ( S ) or the value ( V ) are appropriate projection functions for your purpose: 但是,饱和度( S )或值( V )是适合您目的的投影函数:

  • If you want to have colored pixels "larger" than monochrome pixels, you will prefer S . 如果要使彩色像素“大于”单色像素,则最好使用S
  • If you want to have lighter pixels larger than darker pixels, you will probably prefer V . 如果要使较亮的像素大于较暗的像素,则可能会首选V
  • Also any combination of S and V would be a valid projection function, eg S+V . 同样, SV任何组合都是有效的投影函数,例如S+V

As far as I understand, you want a measure to calculate distance/similarity between two Vec3b pixels. 据我了解,您需要一种措施来计算两个Vec3b像素之间的距离/相似度。 This can be reflected to the general problem of finding distance between two vectors in an n-mathematical space. 这可以反映到在n数学空间中找到两个向量之间的距离这一普遍问题。

One of the famous measures (and I think this is what you're asking for), is the Euclidean distance. 欧几里德距离是著名的量度之一(我想这是您要的)。

If you are using Opencv then you can simply use: 如果您使用的是Opencv,则可以简单地使用:

cv::Vec3b a(1, 1, 1);
cv::Vec3b b(5, 5, 5);
double dist = cv::norm(a, b, CV_L2);

You can refer to this for reading about cv::norm and its options. 您可以参考来阅读有关cv :: norm及其选项的信息。

Edit: If you are doing this to measure color similarity, it's recommended to use the LAB color space as it's proved that Euclidean distance in LAB space is a good approximation for human perception of colors. 编辑:如果要执行此操作以测量颜色相似度,建议使用LAB颜色空间,因为它证明LAB空间中的欧几里得距离可以很好地逼近人类对颜色的感知。

Edit 2: I see what you mean, for this you can get the magnitude of each vector and then compare them, something like this: 编辑2:我明白你的意思了,为此,您可以获取每个向量的大小,然后将它们进行比较,如下所示:

    double a_magnitude = cv::norm(a, CV_L2);
    double b_magnitude = cv::norm(b, CV_L2);
    if(a_magnitude > b_magnitude)
      // do something
    else
      // do something else.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM