更改零矩阵中的像素无法正常工作

时间:2015-01-12 20:49:23

标签: c++

我尝试在新的零矩阵上进行循环,并将每个像素更改为白色。

cv::Mat background = cv::Mat::zeros(frame.rows, frame.cols,frame.type());

for (int i=0; i<frame.rows; i++)
{
    for (int j=0; j<frame.cols; j++)
    {
        background.at<char>(i,j)=255;
    }
}

通常在最后我必须有一个完全白色的矩阵但是我不明白为什么最后我有这张照片:

enter image description here

由于

修改 溶液:

cv::Mat background = cv::Mat::zeros(frame.rows, frame.cols,frame.type());

for (int i=0; i<frame.rows; i++)
{
    for (int j=0; j<frame.cols; j++)
    {

         Vec3b bgrPixel = Vec3b(255,255,255);
         background.at<Vec3b>(i,j)=bgrPixel;

    //    background.at<char>(i,j)=255;


    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您的矩阵由i * j像素组成 - 每个像素由3(RGB)或4(RGBA)字符(字节/通道)组成。当你需要循环遍历i * j像素时,你只是循环遍历矩阵的第一个i * j字节。我猜测你传入的任何类型,因为第三个参数是像素类型&#39;。

在此处查看示例用法:OpenCV get pixel channel value from Mat image