4

我正在尝试从 Mat 对象中获取像素。为了测试,我尝试在正方形上画一条对角线,并期望得到一条从左上角到右下角的完美线。

for (int i =0; i<500; i++){
     //I just hard-coded the width (or height) to make the problem more obvious

  (image2.at<int>(i, i)) = 0xffffff;
     //Draw a white dot at pixels that have equal x and y position.
}

然而,结果并不像预期的那样。这是在彩色图片上绘制的对角线。 在此处输入图像描述 这是一张灰度图片。 在此处输入图像描述 有人看到问题了吗?

4

2 回答 2

6

问题是您试图将每个像素作为 int (每像素图像 32 位)访问,而您的图像是 3 通道无符号字符(每像素图像 24 位)或 1 通道无符号字符(每像素 8 位)图像)为灰度之一。您可以尝试像这样访问每个像素以获得灰度像素

for (int i =0; i<image2.width; i++){
  image2.at<unsigned char>(i, i) = 255;
}

或者像这样的颜色

for (int i =0; i<image2.width; i++){     
      image2.at<Vec3b>(i, i)[0] = 255;
      image2.at<Vec3b>(i, i)[1] = 255;
      image2.at<Vec3b>(i, i)[2] = 255;
}
于 2013-02-28T08:34:25.420 回答
3
(image2.at<int>(i, i)) = 0xffffff;

看起来您的彩色图像是 24 位的,但您的 int 寻址像素似乎是 32 位。

于 2013-02-28T08:29:25.273 回答