0

Mask image

Mask image

Result image

Result image

Expected image

Expected image

Is there any function to draw a rectangle with mask image using opencv? i have attached the expected image. Please help me. thanks in advance.

Prasaathviki
  • 1,147
  • 2
  • 11
  • 22

2 Answers2

1

I think your asked question isn't quite clear, but if the first image is your original image (circle), the second one (rectangle) is your binary mask image, and you want to apply that mask on the original image, than you can apply the mask as followed:

inputMat.copyTo(outputMat, maskMat);

Src.: https://stackoverflow.com/a/18161322/4767895

If you haven't already created a binary mask, do it this way: Create a mask with the same size as your original image (set all zero), and draw a filled rectangle (set all one) with specific size in it.

cv::Mat mask = cv::Mat::zeros(Rows, Cols, CV_8U); // all 0
mask(Rect(StartX,StartY,Width,Height)) = 1; //make rectangle 1

Src.: https://stackoverflow.com/a/18136171/4767895

Feel free to response if I misunderstood your question.

Community
  • 1
  • 1
Mr.Sheep
  • 1,368
  • 1
  • 15
  • 32
0

Try using Boolean Operation provided in Opencv

Please refer to this code (source). I have added all the bitwise operation in case you need any.

int main(  )
{
    Mat drawing1 = Mat::zeros( Size(400,200), CV_8UC1 );
    Mat drawing2 = Mat::zeros( Size(400,200), CV_8UC1 );

    drawing1(Range(0,drawing1.rows),Range(0,drawing1.cols/2))=255; imshow("drawing1",drawing1);
    drawing2(Range(100,150),Range(150,350))=255; imshow("drawing2",drawing2);

    Mat res;
    bitwise_and(drawing1,drawing2,res);     imshow("AND",res);
    bitwise_or(drawing1,drawing2,res);      imshow("OR",res);
    bitwise_xor(drawing1,drawing2,res);     imshow("XOR",res);
    bitwise_not(drawing1,res);              imshow("NOT",res);


    waitKey(0);
    return(0);
}
beaker
  • 16,331
  • 3
  • 32
  • 49
Rahul galgali
  • 775
  • 2
  • 10
  • 26
  • Links die. It would be better to include a description of the approach and some sample code (it would only be one line, after all). – beaker Mar 17 '15 at 21:28
  • 1
    I've made some minor edits, including adding back the link to the source of your code. I had intended to suggest that you provide code in *addition* to the link, not instead. Attribution is still important. If you disagree with any of my edits, feel free to change them or revert. – beaker Mar 18 '15 at 16:49