Fill Masked Contour Intersected with Rect - c++

I've got a Mat image which is a binary mask that I segmented and a cv::Rect that identifies an specific region. When I get the contours of the binary mask the image is like this:
Binary Mask
Contours generated
I would like to fill in the mask the region that intersects with the rectangle. How would I do that?
Thanks in advance.

There is way simpler than #ZdaR's solution: using Regions Of Interest (ROI) which directly selects the bounding rectangle region to process.
cv::Rect boundingRect(/* x, y, width, height */);
contours_image(boundingRect).setTo(255, binary_image(boundingRect));
Here, I select each region with operator parenthesis contours_image(boundingRect) and binary_image(boundingRect), and use the binary image part as a mask to set all corresponding pixels to 255.

A good choice would be to use cv::min() with the binary image and another cv::Mat() with the area under cv::Rect() painted as white. It will filter out the desired portion under the Rect as:
// Create a grayscale canvas with black background
cv::Mat canvas = cv::Mat(binary_img.size(), CV_8UC1, cv::Scalar(0));
// I created a dummy rect replace it with original rect coordinates.
cv::Rect boundingRect = cv::Rect(100, 100, 200, 200);
// Draw filled rect onto the black canvas with white color
cv::rectangle(binary_image, boundingRect, cv::Scalar(255), -1);
// Take the min of binary image and canvas to filter out the contours
cv::min(binary_image, canvas, binary_image);
EDIT:
If you want to filter the contours which intersect the cv::Rect, then you need to iterate over each contour, calculate the boundingRect and check if it intersects the given rect.
for (int i=0; i<contours.size(); i++) {
if ((cv::boundingRect(contours[i]) & boundingRect).area() > 0) {
// Your desired contours found.
}
}

Related

substract region from image and keep the borders

I have a 200x200 pixels image and I want to keep only the data for a certain region inside it.
Check the following image:
The whole out square is 200x200 pixels.I want to remove from it the smaller square(white).So ,keep only the information that is included in the blue area.But , I want to keep the 200x200 dimensions.
I tried:
Mat whiteArea;
whiteArea = ImageInitial( Range(50,200) , Range(50,200) );
Size size(200,200);
Mat dst;
resize(whiteArea,dst,size);
Mat FinalImage;
subtract(ImageInitial,dst,FinalImage);
I am resizing the white area because I want to substract it from the initial image.
My problem is that it gives me the initial image.
Maybe the resize is the problem .but then how to substract 2 different sized images?
try to use subimages or use a mask:
// use a roi (nice if your target area is rectangular and you know the position)
Rect whiteArea = Rect(50,50, 200,200); // creates a roi of the inner rect
Mat FinalImage = ImageInitial.clone();
// now set the roi area to zero:
FinalImage (whiteArea).setTo(Scalar(0,0,0));
// or FinalImage(whiteArea) = FinalImage(whiteArea) - FinalImage(whiteArea);
imshow("version 1 with subimage", FinalImage);
waitkey(0);
// or use a mask (nice if that region can has arbitrary shape etc and you have to extract it first):
Scalar lowerColorBound = Scalar(x,y,z); //some BGR values to find the color you want to eliminate
Scalar upperColorBound = Scalar(a,b,c); //some BGR values to find the color you want to eliminate
Mat mask;
inRange(ImageInitial, lowerColorBound, upperColorBound mask)
// use the mask for subtraction:
subtract(ImageInitial, ImageInitial, FinalImage , mask);
imshow("version 2 with mask", FinalImage);
waitkey(0);

Drawing rects on certain pixels openCV

I'm trying to locate some regions of a frame, the frame is in Ycbcr color space. and I have to select those regions based on their Y values.
so I wrote this code:
Mat frame. ychannel;
VideoCapture cap(1);
int key =0;
int maxV , minV;
Point max, min;
while(key != 27){
cap >> frame;
cvtColor(frame,yframe,CV_RGB_YCrCb); // converting to YCbCr color space
extractChannel(yframe, yframe, 0); // extracting the Y channel
cv::minMaxLoc(yframe,&minV,&maxV,&min,&max);
cv::threshold(outf,outf,(maxV-10),(maxV),CV_THRESH_TOZERO);
/**
Now I want to use :
cv::rectangle()
but I want to draw a rect around any pixel (see the picture bellow)that's higher than (maxV-10)
and that during the streaming
**/
key = waitKey(1);
}
I draw this picture hopping that it helps to understand what I what to do .
thanks for your help.
Once you have applied your threshold you will end up with a binary image containing a number of connected components, if you want to draw a rectangle around each component then you first need to detect those components.
The OpenCV function findContours does just that, pass it your binary image, and it will provide you with a vector of vectors of points which trace the boundary of each component in your image.
cv::Mat binaryImage;
std::vector<std::vector<cv::Point>> contours;
cv::findContours(binaryImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE)
Then all you need to do is find the bounding rectangle of each of those sets of points and draw them to your output image.
for (int i=0; i<contours.size(); ++i)
{
cv::Rect r = cv::boundingRect(contours.at(i));
cv::rectangle(outputImage, r, CV_RGB(255,0,0));
}
You have to find the each of the connected components, and draw their bounding box.

OpenCV Image to Black and White Shape

i want the hand image to be a black and white shape of the hand. here's a sample of the input and the desired output:
using a threshold doesn't give the desired output because some of the colors inside the hand are the same with the background color. how can i get the desired output?
Adaptive threshold, find contours, floodfill?
Basically, adaptive threshold turns your image into black and white, but takes the threshold level based on local conditions around each pixel - that way, you should avoid the problem you're experiencing with an ordinary threshold. In fact, I'm not sure why anyone would ever want to use a normal threshold.
If that doesn't work, an alternative approach is to find the largest contour in the image, draw it onto a separate matrix and then floodfill everything inside it with black. (Floodfill is like the bucket tool in MSPaint - it starts at a particular pixel, and fills in everything connected to that pixel which is the same colour with another colour of your choice.)
Possibly the most robust approach against various lighting conditions is to do them all in the sequence at the top. But you may be able to get away with only the threshold or the countours/floodfill.
By the way, perhaps the trickiest part is actually finding the contours, because findContours returns an arraylist/vector/whatever (depends on the platform I think) of MatOfPoints. MatOfPoint is a subclass of Mat but you can't draw it directly - you need to use drawContours. Here's some code for OpenCV4Android that I know works:
private Mat drawLargestContour(Mat input) {
/** Allocates and returns a black matrix with the
* largest contour of the input matrix drawn in white. */
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(input, contours, new Mat() /* hierarchy */,
Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = 0;
int index = -1;
for (MatOfPoint contour : contours) { // iterate over every contour in the list
double area = Imgproc.contourArea(contour);
if (area > maxArea) {
maxArea = area;
index = contours.indexOf(contour);
}
}
if (index == -1) {
Log.e(TAG, "Fatal error: no contours in the image!");
}
Mat border = new Mat(input.rows(), input.cols(), CvType.CV_8UC1); // initialized to 0 (black) by default because it's Java :)
Imgproc.drawContours(border, contours, index, new Scalar(255)); // 255 = draw contours in white
return border;
}
Two quick things you can try:
After thresholding you can:
Do a morphological closing,
or, the most straightforward: cv::findContours, keep the largest if it's more than one, then draw it using cv::fillConvexPoly and you will get this mask. (fillConvexPoly will fill the holes for you)

With OpenCV, try to extract a region of a picture described by ArrayOfArrays

I am developing some image processing tools in iOS. Currently, I have a contour of features computed, which is of type InputArrayOfArrays.
Declared as:
std::vector<std::vector<cv::Point> > contours_final( temp_contours.size() );
Now, I would like to extract areas of the original RGB picture circled by contours and may further store sub-image as cv::Mat format. How can I do that?
Thanks in advance!
I'm guessing what you want to do is just extract the regions in the the detected contours. Here is a possible solution:
using namespace cv;
int main(void)
{
vector<Mat> subregions;
// contours_final is as given above in your code
for (int i = 0; i < contours_final.size(); i++)
{
// Get bounding box for contour
Rect roi = boundingRect(contours_final[i]); // This is a OpenCV function
// Create a mask for each contour to mask out that region from image.
Mat mask = Mat::zeros(image.size(), CV_8UC1);
drawContours(mask, contours_final, i, Scalar(255), CV_FILLED); // This is a OpenCV function
// At this point, mask has value of 255 for pixels within the contour and value of 0 for those not in contour.
// Extract region using mask for region
Mat contourRegion;
Mat imageROI;
image.copyTo(imageROI, mask); // 'image' is the image you used to compute the contours.
contourRegion = imageROI(roi);
// Mat maskROI = mask(roi); // Save this if you want a mask for pixels within the contour in contourRegion.
// Store contourRegion. contourRegion is a rectangular image the size of the bounding rect for the contour
// BUT only pixels within the contour is visible. All other pixels are set to (0,0,0).
subregions.push_back(contourRegion);
}
return 0;
}
You might also want to consider saving the individual masks to optionally use as a alpha channel in case you want to save the subregions in a format that supports transparency (e.g. png).
NOTE: I'm NOT extracting ALL the pixels in the bounding box for each contour, just those within the contour. Pixels that are not within the contour but in the bounding box are set to 0. The reason is that your Mat object is an array and that makes it rectangular.
Lastly, I don't see any reason for you to just save the pixels in the contour in a specially created data structure because you would then need to store the position for each pixel in order to recreate the image. If your concern is saving space, that would not save you much space if at all. Saving the tightest bounding box would suffice. If instead you wish to just analyze the pixels in the contour region, then save a copy of the mask for each contour so that you can use it to check which pixels are within the contour.
You are looking for the cv::approxPolyDP() function to connect the points.
I shared a similar use of the overall procedure in this post. Check the for loop after the findContours() call.
I think what you're looking for is cv::boundingRect().
Something like this:
using namespace cv;
Mat img = ...;
...
vector<Mat> roiVector;
for(vector<vector<Point> >::iterator it=contours.begin(); it<contours.end(); it++) {
if (boundingRect( (*it)).area()>minArea) {
roiVector.push_back(img(boundingRect(*it)));
}
}
cv::boundingRect() takes a vector of Points and returns a cv::Rect. Initializing a Mat myRoi = img(myRect) gives you a pointer to that part of the image (so modifying myRoi will ALSO modify img).
See more here.

Dealing with pixels in contours (OpenCV)?

I have retrieved a contour from an image and want to specifically work on the pixels in the contour. I need to find the sum (not area) of the pixel values in the contour. OpenCV only supports rectangle shaped ROI, so I have no idea how to do this. cvSum also only accepts complete images and doesn't have a mask option, so I am a bit lost on how to proceed. Does anyone have any suggestions on how to find the sum of the values of pixels in a specific contour?
First get all of your contours. Use this information to create a binary image with the white parts being the contour's outline and area. Perform an AND operation on the two images. The result will be the contours and area on a black background. Then just sum all of the pixels in this image.
If I understand right you want to sum all the pixel intensities from a gray image that are inside a contour. If so, the method that i think of is to draw that contour on a blank image and fill it , in so making yourself a mask. After that to optimize the process you can also compute the bounding rect of the contour with :
CvRect cvBoundingRect(CvArr* points, int update=0 );
After this you can make an intermediate image with :
void cvAddS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask=NULL);
using the value 0, the mask obtained from the contour and setting before as ROI the bounding rect.
After this, a sum on the resulting image will be a little faster.
To access the contour's point individually follow the code
vector<vector<Point> > contours;
...
printf("\n Contours pixels \n");
for(int a=0; a< contours.size(); a++)
{
printf("\nThe contour NO = %d size = %d \n",a, contours[a].size() );
for( int b = 0; b < contours[a].size(); b++ )
{
printf(" [%d, %d] ",contours[a][b].x, contours[a][b].y );
}
}