0

Using OpenCV, I am detecting a face, detecting the left and right eyes of that face, and extracting the eye into a new Mat image. I am then converting the eye image colour from BGR to HSV.

I am checking to see if the eye colour is in a certain range using inRange(). This displays the area of the eye that is red (see below image).

My question is: I would like to change the eye colour (detected using inRange()) from red to black. I'm not too sure where to go from here.

Any help is appreciated! Thanks!


Current result:

LKB
  • 1,020
  • 5
  • 22
  • 46

2 Answers2

1

You already have the mask, just do a for loop and set the pixels to black(.at = Vec3b(0,0,0), in BGR space of course) where the mask is 255.

James Harper
  • 480
  • 4
  • 8
  • Thanks @james-harper. Would I implement a for loop for the rows and columns, and then... okay I've lost myself now, hah.. – LKB Jul 31 '14 at 07:05
  • 2
    change the first line of diip_thomas's answer to cv::Vec3b pixelColor(0,0,0); He mixed Scalar and Vector. Also, you don't have to specifically define a Point2f, just use at(y,x), (y,x). Because it's row-major. – James Harper Jul 31 '14 at 07:37
1

You can do this with the following for loop if you want to make the eyes blue for instance

cv::Vec3b pixelColor(255,0,0);
for(int y=0;y<img.rows;y++){
 for(int x=0;x<img.cols;x++){
  cv::Point2f point(x, y);
  if (mask.at<uchar>(point))  image.at<Vec3b>(cv::Point(x,y)) = pixelColor;
 }
}
diip_thomas
  • 1,531
  • 13
  • 25
  • Thanks for the help, @diip-thomas. The line `image.at(cv::Point(x,y)) = pixelColor;` is giving me an error. – LKB Jul 31 '14 at 07:07