Lorenz Leitner
Lorenz Leitner

Reputation: 591

Only one third of image written in OpenCV

In this piece of code I'm trying to write values (0.123 for this example) into the out image, which is a copy of the input image. But for some reason, only one third of the image is being written, while the other two thirds are still the copied input image, as you can see in the screenshot.

What am I doing wrong?

// image is of type CV_32FC3 and XYZ, values in range [0,1]
Mat out = Mat::zeros(image.size(), image.type());
image.copyTo(out);

for (int i = 0; i < out.rows; i++)
{
  for (int j = 0; j < out.cols; j++)
  {
    out.at<float>(i,j) = 0.123;
  }
}

Screenshot

PS: The bigger picture goal is a HDR image fusion into the out image, which actually works, but only for the left third of the out image, same as here.

Upvotes: 1

Views: 297

Answers (2)

Vasanth
Vasanth

Reputation: 1278

If you are just setting the array to a scalar, you can use either

out = Scalar(0.123, 0.123, 0.123); or

out.setTo(Scalar(0.123, 0.123, 0.123));

It is a 3 channel image but you are accessing using float (which used to access pixels in a gray scale image), hence the 1/3rd. Use Vec3f if you want to use for-loop to access the elements.

out.at<cv::Vec3f>(i,j) = cv::Vec3f(0.123, 0.123, 0.123);

You can access a particular channel's element by indexing.

out.at<cv::Vec3f>(j,i)[0] = 0.123;
out.at<cv::Vec3f>(j,i)[1] = 0.123;
out.at<cv::Vec3f>(j,i)[2] = 0.123;

Upvotes: 1

DimChtz
DimChtz

Reputation: 4333

You need to change this:

out.at<float>(i,j) = 0.123;

to this:

out.at<cv::Vec3f>(i,j)[0] = 0.123;
out.at<cv::Vec3f>(i,j)[1] = 0.123;
out.at<cv::Vec3f>(i,j)[2] = 0.123;

because it's a color image, so it has 3 channels

Upvotes: 1

Related Questions