Ask Your Question
1

Is there a simple method to highlight the mask?

asked 2017-09-07 14:22:32 -0600

yode gravatar image

updated 2017-09-07 14:25:42 -0600

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("image.jpg");
    Mat mask = 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?
edit retag flag offensive close merge delete

Comments

there is no alpha channel in image processing but you can use this tutorial

LBerger gravatar imageLBerger ( 2017-09-07 14:28:44 -0600 )edit

@LBerger Thanks,but it is not a transparent red..Just a transprent whilte..

yode gravatar imageyode ( 2017-09-07 14:36:35 -0600 )edit

replace circle(srcImg, Point(j,i), 3, Scalar(0, 0, 128,128)); with srcImg.at<Vec3b>(i, j) = srcImg.at<Vec3b>(i, j) - Vec3b(100, 100, 0);

sturkmen gravatar imagesturkmen ( 2017-09-07 15:26:52 -0600 )edit

also take a look at this i prefer doing this kind of operations without pixel by pixel approach

sturkmen gravatar imagesturkmen ( 2017-09-07 15:32:21 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-09-07 16:18:42 -0600

you can replace

circle(srcImg, Point(j,i), 3, Scalar(0, 0, 128,128));

with

srcImg.at<Vec3b>(i, j) = srcImg.at<Vec3b>(i, j) - Vec3b(100, 100, 0);

but i prefer the way below

#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>

using namespace cv;

int main(int argc, char** argv)
{
    Mat srcImg = imread("image.png");
    Mat mask = imread("mask.png", IMREAD_GRAYSCALE) > 200;

    Mat red;
    cvtColor(mask, red, COLOR_GRAY2BGR);
    red = (red - Scalar(0, 0, 255)) / 2;
    srcImg = srcImg - red;

    imshow("image", srcImg);

    waitKey();
    return 0;
}

image description

edit flag offensive delete link more
0

answered 2017-09-07 21:29:10 -0600

Ziri gravatar image

updated 2017-09-07 21:33:07 -0600

Draw contours with CV_FILLED for line thickness parameter; check this example

then use addWeighted to copy contour drawing on top of your image.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-07 14:22:32 -0600

Seen: 2,103 times

Last updated: Sep 07 '17