Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is there a simple method to highlight the mask?

If I have mask like

And I have a image( the size is same to the mask) like

Mathematica graphics

I want to hightlight the mask in the image. If I'm in other Language,I just

As you can see, the result image have a transparent red show the mask. I hope implement this in OpenCV. So I write this code

#include <opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    Mat srcImg = imread("C:/Users/zyd199012/Desktop/image.jpg");
    Mat mask = imread("C:/Users/zyd199012/Desktop/mask.jpg", IMREAD_GRAYSCALE)>200;

    for(int i=0;i<srcImg.rows;i++)
        for(int j=0;j<srcImg.cols;j++)
            if(mask.at<uchar>(i, j)==255)
                circle(srcImg, Point(j,i), 3, Scalar(0, 0, 128,128));
    imshow("image",srcImg);

    waitKey();
    return 0;
}

But as you see, I use a alpha value in Scalar, but it is not a transparent red.

Maybe this is due to the srcImg just have 3 channels. I have two question about this

  1. How to hightlight the mask with a transparent red(even the image just have 3 channels)?
  2. I have to draw circle pixel by pixel to do this thing?

Is there a simple method to highlight the mask?

If I have mask like

And I have a image( the size is same to the mask) like

Mathematica graphics

I want to hightlight the mask in the image. If I'm in other Language,I just

As you can see, the result image have a transparent red show the mask. I hope implement this in OpenCV. So I write this code

#include <opencv.hpp>

using namespace cv;
using namespace std;

int main() {
    Mat srcImg = imread("C:/Users/zyd199012/Desktop/image.jpg");
imread("image.jpg");
    Mat mask = imread("C:/Users/zyd199012/Desktop/mask.jpg", imread("mask.jpg", IMREAD_GRAYSCALE)>200;

    for(int i=0;i<srcImg.rows;i++)
        for(int j=0;j<srcImg.cols;j++)
            if(mask.at<uchar>(i, j)==255)
                circle(srcImg, Point(j,i), 3, Scalar(0, 0, 128,128));
    imshow("image",srcImg);

    waitKey();
    return 0;
}

But as you see, I use a alpha value in Scalar, but it is not a transparent red.

Maybe this is due to the srcImg just have 3 channels. I have two question about this

  1. How to hightlight the mask with a transparent red(even the image just have 3 channels)?
  2. I have to draw circle pixel by pixel to do this thing?