本文介绍了在OpenCV中复制像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有RGB图片,例如如下所示:

I have RGB images, e.g. of size 2x2 as follows:

[  0,  14, 255,  75, 156, 255;
  45, 255, 234, 236, 141, 255]

每个像素(所有RGB通道)2x2次,并获得如下的图像:

I would like to duplicate each pixel (all of the RGB channels) 2x2 times and obtain images that look like:

[  0,  14, 255,  0, 14, 255,  75, 156, 255, 75, 156, 255;
   0,  14, 255,  0, 14, 255,  75, 156, 255, 75, 156, 255;
  45, 255, 234,  45, 255, 234, 236, 141, 255, 236, 141, 255;
  45, 255, 234,  45, 255, 234, 236, 141, 255, 236, 141, 255  ]

在Matlab或Python中,我只是简单地使用 kron 函数,但我不知道如何轻松地在OpenCV C ++ 。注意,这是一个简单的例子,我实际上想要复制每个像素的16x16,并在更大的图像,而不是2x2当然。

In Matlab or Python, I would do this simply by using the kron function but I couldn`t figure out how to do it easily in OpenCV C++. Note that this is a trivial example, I would actually like to duplicate each pixel by 16x16 and do it on much bigger images, not 2x2 of course.

推荐答案

您可以使用 resize 与最近邻插值 INTER_NEAREST

You can use resize with nearest neighbor interpolation INTER_NEAREST:

代码:

Mat mInput(2, 2, CV_8UC3),mOutput(4, 4, CV_8UC3);

mInput.at<Vec3b>(0,0)= Vec3b(0,14,255);
mInput.at<Vec3b>(0,1)= Vec3b(75,156,255);
mInput.at<Vec3b>(1,0)= Vec3b(45,255,234);
mInput.at<Vec3b>(1,1)= Vec3b(236,141,255);

cout<<mInput<<"\n";

resize(mInput,mOutput,Size(4,4),0,0,INTER_NEAREST);

cout<<mOutput<<"\n";;

输出

[  0,  14, 255,  75, 156, 255;
  45, 255, 234, 236, 141, 255]
[  0,  14, 255,   0,  14, 255,  75, 156, 255,  75, 156, 255;
   0,  14, 255,   0,  14, 255,  75, 156, 255,  75, 156, 255;
  45, 255, 234,  45, 255, 234, 236, 141, 255, 236, 141, 255;
  45, 255, 234,  45, 255, 234, 236, 141, 255, 236, 141, 255]
Press any key to continue . . .

感谢@wendelbsilva在评论中指出这一点。

Thanks to @wendelbsilva for pointing this out in the comments.

这篇关于在OpenCV中复制像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:07