从不同平面中选择最大值

发布于 2024-12-27 19:30:47 字数 1353 浏览 0 评论 0原文

我想从两个图像中获取最大值,同时保留其他信息。

例如,我有两个 HSL 格式的图像,我希望生成的图像由最亮的像素组成。 (这是一个例子,实际上我无法转换为RGB,找到最大值,然后转换回来)

所以我不能使用cvMax因为它会分别给我最大H,最大S和最大L 我需要的是任一

图像中具有最大 L 的图像,并附有同一图像中的 H 和 S 值(或其他信息)。

例如,考虑这些 2x1 像素图像及其 HSV 标量:

IplImage* img1 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(1, 2, 45)); // Remember: HSV values
cvSet2D(img1, 0, 1, cvScalar(3, 4, 123));
IplImage* img2 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(5, 6, 114));
cvSet2D(img1, 0, 1, cvScalar(7, 8, 33));

IplImage* result = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);

//Do something to get the brightest pixels

CvScalar px0 = cvGet2D(result, 0, 0);
CvScalar px1 = cvGet2D(result, 0, 1);

cout << px0.val[0] << " " << px0.val[1] << " " << px0.val[2] << endl;
cout << px1.val[0] << " " << px1.val[1] << " " << px1.val[2] << endl;

输出应该变为:

5 6 114
3 4 123

是否有一种简洁的“OpenCV”方法可以做到这一点,而不是制作我自己的(可能更慢的)算法来解析每个像素?


只是一个想法,但我可以做这样的事情吗?

  • 将它们分割成6个灰度图像,(实际上,我已经有这种情况,所以我不需要分割)
  • 取两个V图像,
  • 生成每个元素的图(1U或8U图像),其中'0'或“1”值表示最高值分别位于第一张或第二张图像中。
  • 使用此值组合来自其他平面的选定像素。

大胆的一步是我被困住的地方。

I want to get the maximum values from two images, while preserving other information.

For example, I have two images in HSL format, and I want the resulting image to consist of the brightest pixels. (this is an example, in reality I can't convert to RGB, finding max, and converting back)

So I cannot use cvMax because it will separately give me the maximum H, maximum S and maximum L.

What I need is an image with the maximum L from either image, accompanied by the H and S values (or other information) from the same image.

For example, consider these 2x1 pixel images, with their HSV scalars:

IplImage* img1 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(1, 2, 45)); // Remember: HSV values
cvSet2D(img1, 0, 1, cvScalar(3, 4, 123));
IplImage* img2 = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);
cvSet2D(img1, 0, 0, cvScalar(5, 6, 114));
cvSet2D(img1, 0, 1, cvScalar(7, 8, 33));

IplImage* result = cvCreateImage(cvSize(2, 1), IPL_DEPTH_32F, 3);

//Do something to get the brightest pixels

CvScalar px0 = cvGet2D(result, 0, 0);
CvScalar px1 = cvGet2D(result, 0, 1);

cout << px0.val[0] << " " << px0.val[1] << " " << px0.val[2] << endl;
cout << px1.val[0] << " " << px1.val[1] << " " << px1.val[2] << endl;

Output should become:

5 6 114
3 4 123

Is there a neat 'OpenCV' way of doing this, instead of making my own, possibly slower, algorithm that parses each pixel?


Just an idea, but can I do something like this?

  • Split them into 6 grayscale images, (actually, I already have this situation, so I don't need to split)
  • Take both V images,
  • Generate a per-element map (1U or 8U image) where a '0' or '1' value means the highest value was in the first or second image respectively.
  • Use this to combine selected pixels from the other planes.

The bold step is where I'm stuck.

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

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

发布评论

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

评论(1

早乙女 2025-01-03 19:30:47

没有内置的 opencv 函数可以做到这一点,但编写自己的函数并不困难。

当然,可以

for(i=0;i<h;i++)
   for (j=0;j<w;j++)
      dst[j+w*i] = (src1[j+w*i]>src2[j+w*i])?src1[]:src2[];

做一些小的优化,比如预先计算 j+w*i,或者使用指针访问而不是数组索引。

There is no build-in opencv function to do it, but it's not thah difficult to write your own.

You can simply

for(i=0;i<h;i++)
   for (j=0;j<w;j++)
      dst[j+w*i] = (src1[j+w*i]>src2[j+w*i])?src1[]:src2[];

Of course, some little optimization can be done, like precalculating j+w*i, or using pointer access instead of array indexes.

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