0

I've done quite a bit of digging and haven't found any questions that seem to match my exact issue. I'm also a CV noob so I've come up empty trying to work out a solution from what I've found so far.

I have a set of contours that I'm able to display (filled in) on an image via:

Mat img = CvInvoke.Imread(HttpContext.Current.Server.MapPath("IMAGELOCATION"));
VectorOfMat contours = new VectorOfMat();

..    
//Various Morphological Transformations
..

CvInvoke.FindContours(maskDilate, contours, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxNone);
CvInvoke.DrawContours(img, contours,-6, new MCvScalar(255), -1);

These contours are non-rectangular and the data within them is of interest. Originally I planned on cropping out all of the contours but it seems like the better solution is just to blank out the rest of the original image except for the ROI outlined by my contours, without affecting the inner part of the contours.

This seems like a job for a mask of the contours overlaid on the original image, with something like the matrix containing the original image blanked to zeros except for each ROI outlined by the contours.

The closest I've come to what I need is from this thread copying non-rectangular roi opencv however I'm working in C# and don't have the coordinates outlining my ROIs because they're often oddly shaped and can vary in location on the image. Some direction or help would be much appreciated!

TechnoloG
  • 56
  • 7

1 Answers1

0

Alright I figured this out. It appears I was making things more complicated than necessary.

First I created a Matrix to hold the final result. I was under the impression I could specify the background color using the MCvScalar but it didn't seem to affect my final result when I tried different args and always resulted in a black background. If someone has an explanation for this that'd be great.

Matrix<int> blackedOut = new Matrix<int>(img.Size);
crop.SetValue(new MCvScalar(255, 255, 255));

Finding and drawing the contours was completely unnecessary since I already had a mask (maskDilate) which outlined my regions of interest.

So all I had to do was copy the original image to the blackedOut matrix with maskDilate applied.

img.CopyTo(crop, maskDilate);

So if you're in a similar situation to me you should already have a mask that you're passing into FindContours that you can use directly without messing with contours at all.

I used this as a reference https://www.bytefish.de/blog/extracting_contours_with_opencv/

TechnoloG
  • 56
  • 7