-2

I am new in OpenCV. I have a image what I want is to change the color of each and every pixel of image with any single color. I found that code but when I run this part of code then a exception is generated.

for (i = 0;i < img1.rows;i++) {
    for (j = 0;j < img1.cols;j++) {
        img1.at<Vec3b>(i, j) = Vec3b(255, 105, 240);
    }
}

Can anyone please tell me the solution. Or what I found is that this take a lot of time for the conversion So if their is any other approach then please tell me.

Lakshraj
  • 291
  • 2
  • 18
  • 2
    I didn't downvote, but you're not supposed to complain that some code gives you an error but then not post the error and only post an incomplete fragment that gives no context on why it might be occurring. This is prominently mentioned in the Help Centre about getting help with errors. You then change the question into something else instead - "Or what I found is that this take a lot of time for the conversion" - when questions are meant to have single focus. – underscore_d May 02 '18 at 09:24

3 Answers3

2
// Make a 3 channel image
cv::Mat img(480,640,CV_8UC3);

// Fill entire image with cyan (blue and green)
img = cv::Scalar(255,255,0);
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

You can use Mat::operator() to select the roi and then assign a value to it.

void main()
{
    cv::Mat img = cv::Mat::ones(5, 5, CV_8UC3);
    img({2, 4}, {2, 4}) = cv::Vec3b(7, 8, 9);
    cout << img;
}

This will print

[  1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0;
   1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0;
   1,   0,   0,   1,   0,   0,   7,   8,   9,   7,   8,   9,   1,   0,   0;
   1,   0,   0,   1,   0,   0,   7,   8,   9,   7,   8,   9,   1,   0,   0;
   1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0,   1,   0,   0]
Timo
  • 9,269
  • 2
  • 28
  • 58
0

To fill image with single color, use rectangle with CV_FILLED argument.

(there might be some reasons for exception - image was not created, wrong pixel format etc - it is hard to diagnose a problem with given information)

MBo
  • 77,366
  • 5
  • 53
  • 86