Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to replace information in a mat based on a mask?

I have an image, and a mask. I want to remove the information in the image in the mask area.

cv::Mat plateMat = imgIn(theRect); // theRect is the roi in the input image
cv::Mat plateMsk = cv::Mat::zeros(imgIn.rows, imgIn.cols, CV_8UC1);
cv::fillPoly(plateMsk, shapeContour, 255); // shape contour is marking the region that I want to remove
int yIdx = 0;
for (int i = plateY; i < plateY + plateH; i++)
{
  int xIdx = 0;
  for (int j = plateX; j < plateX + plateW; j++)
  {
    if (plateMsk.ptr<uchar>(i)[j] == 255)
    { // I am adding green for better visualization
      retImage.ptr<cv::Vec3b>(i)[j][0] = 0;
      retImage.ptr<cv::Vec3b>(i)[j][1] = 255;
      retImage.ptr<cv::Vec3b>(i)[j][2] = 0;
    }
    xIdx++;
  }
  yIdx++;
}

I have done it in for loops, but I think that it is a better way (optimized in OpenCV mode), but right now I have no idea. Can you help me a little?

How to replace information in a mat based on a mask?

I have an image, and a mask. I want to remove the information in the image in the mask area.

cv::Mat plateMat = imgIn(theRect); // theRect is the roi in the input image
cv::Mat plateMsk = cv::Mat::zeros(imgIn.rows, imgIn.cols, CV_8UC1);
cv::fillPoly(plateMsk, shapeContour, 255); // shape contour is marking the region that I want to remove
int yIdx = 0;
for (int i = plateY; i < plateY + plateH; i++)
{
  int xIdx = 0;
  for (int j = plateX; j < plateX + plateW; j++)
  {
    if (plateMsk.ptr<uchar>(i)[j] == 255)
    { // I am adding green for better visualization
      retImage.ptr<cv::Vec3b>(i)[j][0] = 0;
      retImage.ptr<cv::Vec3b>(i)[j][1] = 255;
      retImage.ptr<cv::Vec3b>(i)[j][2] = 0;
    }
    xIdx++;
  }
  yIdx++;
}

I have done it in for loops, but I think that it is a better way (optimized in OpenCV mode), but right now I have no idea. Can you help me a little?