Ask Your Question
0

Add red background color to image with openCV

asked 2015-12-03 11:19:39 -0600

baro gravatar image

Hello!!! I have to add a red (or other color) background with a previously uploaded image, someone could tell me how I can do this with openCV? In other words I must show the previously uploaded image, but with its background red. Thank you all for the help

edit retag flag offensive close merge delete

Comments

1

A sample image would be helpful...

LorenaGdL gravatar imageLorenaGdL ( 2015-12-03 12:03:48 -0600 )edit
baro gravatar imagebaro ( 2015-12-04 03:37:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-12-04 04:04:57 -0600

LorenaGdL gravatar image

Given your sample image, here two versions, depending of what you want exactly

Version 1:

Mat image = imread("woman.jpg", 0);     //read grayscale version of image (1-channel)
cvtColor(image, image, CV_GRAY2BGR);    //rgb version, r=g=b
Scalar color = Scalar(0, 0, 255);       //define desired color
Mat mask = Mat(image.rows, image.cols, CV_8UC3, color);     //create a mask of such color
Mat result;
addWeighted(image, 0.5, mask, 0.5, 0, result, CV_8UC3);     //weights can be adjusted to needs

Version 2:

Mat image = imread("woman.jpg", 1);     //read color version of image (3-channel)
Scalar color = Scalar(0, 0, 255);       //define desired color
Mat mask = Mat(image.rows, image.cols, CV_8UC3, color);     //create a mask of such color
Mat result;
addWeighted(image, 0.5, mask, 0.5, 0, result, CV_8UC3);     //weights can be adjusted to needs

Results (original/version1/version2):

image descriptionimage descriptionimage description

edit flag offensive delete link more

Comments

+1 I think the second solution is more natural and better :)

StevenPuttemans gravatar imageStevenPuttemans ( 2015-12-04 04:23:03 -0600 )edit
1

I do too, but the provided sample seems to be grayscale. So let give him/her choice ^^

LorenaGdL gravatar imageLorenaGdL ( 2015-12-04 04:24:40 -0600 )edit

thanks you so much!!!!! could you tell me now how I do this only in some image's parts? Because I divided the image with a grid size 40x40. For each boxs in the grid I execute some calculations and I change in red only some boxes. thank you :)

baro gravatar imagebaro ( 2015-12-07 12:45:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-03 11:19:39 -0600

Seen: 5,245 times

Last updated: Dec 04 '15