C++;-彩色像素 我是C++新手,我想创建一个500×500像素的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 image.at(i,j) = Vec<int,3>(r,g,b)

C++;-彩色像素 我是C++新手,我想创建一个500×500像素的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 image.at(i,j) = Vec<int,3>(r,g,b),c++,colors,interpolation,pixels,C++,Colors,Interpolation,Pixels,最终结果应如下所示: 然后程序在窗口中显示图像等。。。我可以做那件事,但我只需要弄清楚如何在图像中添加颜色。到目前为止,我的代码是: #include <QtCore/QCoreApplication> #include <opencv/cv.h> #include <opencv/highgui.h> #include <iostream> #include <string> #include <sys/stat.h>

最终结果应如下所示:

然后程序在窗口中显示图像等。。。我可以做那件事,但我只需要弄清楚如何在图像中添加颜色。到目前为止,我的代码是:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
int main()
{
Mat图像;
创建(500500,CV_8UC3);
//左上角
Vec3b-ul(255,0,0);
//右上角
Vec3b-ur(0,255,0);
//左下角
Vec3b-bl(0,0255);
//右下角
Vec3b-br(255,0255);
namedWindow(“彩色像素”);
imshow(“彩色像素”,图像);
//显示图像5秒钟
waitKey(5000);
返回0;
}
输出是


我很高兴听到你的建议

听起来你真正想问的是‘如何设置表示图像的OpenCV Mat实例的元素?’谷歌搜索一下就会产生这个问题,它记录了Mat接口。它包括一个具有此签名的函数:

模板_Tp&Mat::at(inti,intj)

因此,您应该能够进行类似image.at(100250)的调用,以获取对100250像素的像素数据的引用。然后,将像素数据指定给该像素

至于如何以矩阵理解的形式分配像素数据。。。以下内容也来自上述链接:

如果您熟悉OpenCV CvMat的类型表示法,CV 8U。。。个人简历 _32FC3、CV 64FC2等,则可以将基元类型定义为可以在表单中为其提供唯一标识符的类型 CV|U|S|F}C。通用OpenCV 结构能够存储这种原始数据类型的单个实例 是Vec

因此,听起来像是int类型和大小为3的OpenCV Vec可能是兼容的。比如:

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
image.at(i,j) = Vec<int,3>(r,g,b)
image.at(i,j)=Vec(r,g,b)
希望它与你想要的非常接近


请注意,我没有使用OpenCV,也没有测试过这段代码;这就是我从链接文档中搜集到的东西。

我不明白这与C++本身有什么关系。