C++ - 彩色像素

标签 c++ colors interpolation pixel

我是 C++ 的新手,我正在尝试创建一个 500x500 像素的 RGB 图像。图像应该由四种颜色的线性插值填充,每种颜色都为一个角定义,所以我应该有类似的东西:

Vec3b ul( 255, 0, 0 ); //upper left corner
Vec3b ur( 0, 255, 0 ); //upper right corner
Vec3b bl( 0, 0, 255 ); //bottom left corner
Vec3b br( 255, 0, 255 ); //bottom right corner

最终结果应该是这样的:

enter image description here

程序然后在窗口中显示图像等等...我可以做这些,但我只需要弄清楚如何将颜色放入图像中。到目前为止我的代码是这样的:

#include <QtCore/QCoreApplication>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
#include <string>
#include <sys/stat.h>

using namespace cv;
int main()
{

    Mat image;
    image.create( 500, 500, CV_8UC3);
    //upper left corner
    Vec3b ul( 255, 0, 0 );
    //upper right corner
    Vec3b ur( 0, 255, 0 );
    //bottom left corner
    Vec3b bl( 0, 0, 255 );
    //bottom right corner
    Vec3b br( 255, 0, 255 );

    namedWindow("Colored Pixels");
    imshow("Colored Pixels", image);
    // shows image for 5 seconds
    waitKey(5000);
    return 0;
}

输出是

enter image description here

我很高兴听到您的建议!

最佳答案

听起来您真正想问的是“如何设置表示图像的 OpenCV Mat 实例的元素?”一点谷歌搜索产生这个link ,它记录了 Mat 接口(interface)。它包括一个具有此签名的函数:

template _Tp& Mat::at(int i, int j)

因此,您应该能够调用 image.at(100,250) 来获取对 100,250 处像素的像素数据的引用。然后,将像素数据分配给像素。

至于怎么把像素数据按照矩阵理解的形式赋值...下面的也是来自上面的链接:

If you are familiar with OpenCV CvMat ‘s type notation, CV _ 8U ... CV _ 32FC3, CV _ 64FC2 etc., then a primitive type can be defined as a type for which you can give a unique identifier in a form CV_{U|S|F}C . A universal OpenCV structure able to store a single instance of such primitive data type is Vec.

因此,听起来类型为 int 且大小为 3 的 OpenCV Vec 可能是兼容的。像这样的东西:

image.at(i,j) = Vec<int,3>(r,g,b)

希望与您正在寻找的内容相当接近。

请注意,我没有使用过 OpenCV,也没有测试过这段代码;这正是我从链接文档中收集到的内容。

关于C++ - 彩色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12829991/

相关文章:

python - 使用格式运算符 % 将 RGB 值的 numpy 数组转换为十六进制

c# - 字符串插值 - 重复

C++ - 执行一系列不同实现的最佳方式

c++ - C++ 中两个 map 之间的同时并集和交集

c++ - 带范围检查的自制迭代器

c++ - 如何在 C++ 中将字符串转换为 vector ?

android - 单像素颜色校正

r - ggplot 将颜色代码列与 R 数据框中的另一列匹配

indexing - 使用带有 float 的 Pandas reindex : interpolation

python - interp1d 与流数据?