Extract area inside vector of points - c++

I'm trying to extract the area enclosed by a vector of points which do not necessarily form a rectangle. For example:
I want to extract the area inside the yellow figure.
The way I drew the yellow figure is by drawing lines between pairs of points that I have as a vector<Point2f>.
I tried floodFilling with some color starting from some pixel inside, in order to use this as a mask later and I got this:
(problem here is obviously the black holes inside the letters)
I also tried filling the outside area with the same color as the text (white):
But some black wholes remained outside...
How can I do this correctly without leaving any holes?

To fill the polygon defined by your vector of points, you can use fillPoly. Draw the polygon on an empty image and use that as a mask in a second step.

Related

Chart.js Draw grid lines inside or over the polar points

Looking for a solution to get the grid lines inside or over the polar area points.
Like the image attached.
I found the code which draws the gridLines and the one draws the point.
I also see it's possible to pass a creationPattern() as background.
But I don't know how to get them working together.
Maybe a solution can be simply show the gridLines over the points and not behind.

Divide 2d Matrix into different rectangles C++

I want to create c++ code that get all different rectangles within 2d matrix
i need to get all cases for example the left part of the picture i got all vertical rectangles and the right picture i got some vertical and some horizontal
i need to get all possible ways like picture
in other way i need to have output like that
array each element within it have array of objects every object have the start of the rectangle and the end of it say (0,0) to (0,3)

Using Opencv how to detect a box in image while eliminating objects printed inside box?

I am trying to develop box sorting application in qt and using opencv. I want to measure width and length of box.
As shown in image above i want to detect only outermost lines (ie. box edges), which will give me width and length of box, regardless of whatever printed inside the box.
What i tried:
First i tried using Findcontours() and selected contour with max area, but the contour of outer edge is not enclosed(broken somewhere in canny output) many times and hence not get detected as a contour.
Hough line transform gives me too many lines, i dont know how to get only four lines am interested in out of that.
I tried my algorithm as,
Convert image to gray scale.
Take one column of image, compare every pixel with next successive pixel of that column, if difference in there value is greater than some threshold(say 100) that pixel belongs to edge, so store it in array. Do this for all columns and it will give upper line of box parallel to x axis.
Follow the same procedure, but from last column and last row (ie. from bottom to top), it will give lower line parallel to x axis.
Likewise find lines parallel to y axis as well. Now i have four arrays of points, one for each side.
Now this gives me good results if box is placed in such a way that its sides are exactly parallel to X and Y axis. If box is placed even slightly oriented in some direction, it gives me diagonal lines which is obvious as shown in below image.
As shown in image below i removed first 10 and last 10 points from all four arrays of points (which are responsible for drawing diagonal lines) and drew the lines, which is not going to work when box is tilted more and also measurements will go wrong.
Now my question is,
Is there any simpler way in opencv to get only outer edges(rectangle) of box and get there dimensions, ignoring anything printed on the box and oriented in whatever direction?
I am not necessarily asking to correct/improve my algorithm, but any suggestions on that also welcome. Sorry for such a big post.
I would suggest the following steps:
1: Make a mask image by using cv::inRange() (documentation) to select the background color. Then use cv::not() to invert this mask. This will give you only the box.
2: If you're not concerned about shadow, depth effects making your measurment inaccurate you can proceed right away with trying to use cv::findContours() again. You select the biggest contour and store it's cv::rotatedRect.
3: This cv::rotatedRect will give you a rotatedRect.size that defines the width en the height of your box in pixels
Since the box is placed in a contrasting background, you should be able to use Otsu thresholding.
threshold the image (use Otsu method)
filter out any stray pixels that are outside the box region (let's hope you don't get many such pixels and can easily remove them with a median or a morphological filter)
find contours
combine all contour points and get their convex hull (idea here is to find the convex region that bounds all these contours in the box region regardless of their connectivity)
apply a polygon approximation (approxPolyDP) to this convex hull and check if you get a quadrangle
if there are no perspective distortions, you should get a rectangle, otherwise you will have to correct it
if you get a rectangle, you have its dimensions. You can also find the minimum area rectangle (minAreaRect) of the convexhull, which should directly give you a RotatedRect

moving a set of items that contains a clip-rect attribute

I have a set containing image objects. Each object is cropped using the 'clip-rect' attribute.
when I transform the entire set
allframes.transform("t50,0")
all the images move 50 pixels to the right, but the clip-rect stays in place.
how can I get the clip-rect attributes to transform with the entire set?
I am working on this for hours and I am drawing blanks.
I did come up with an inefficient solution:
When I first create the images and crop them, I create rectangles with the attributes of the crop, and push them into the set. when I transform the set, the rectangles move too, and then I reset the attributes on the clip-rect so it matches the getBBox of the corresponding rectangles. it worked for a while but now the code became too complicated for that.
Is there any way to move 'clip-rect' relative to their position?
Here is an illustration of my problem:
http://jsfiddle.net/28Fcn/
the yellow square is the same size as the other squares but it is cropped.
when I transform the entire set, all the elements move but the crop stays in place.
click on the rectangles and see.
this is my solution which is not ideal
http://jsfiddle.net/PgK6w/
any ideas?

Extracting an object from a low contrast background

I need to extract an object from an image where the background is almost flat...
Consider for example a book over a big white desktop.. I need to get the coordinates of the 4 corners of the book to extract a ROI.
Which technique using OpenCV would you suggest? I was thinking to use k Means but I can't know the color of the background a priori (also the colors inside the object can be vary)
If your background is really low contrast, why not try a flood fill from the image borders, then you can obtain bounding box or bounding rect afterwards.
Another option is to apply Hough transform and take intersection of most outer lines as corners. This is, if your object is rectangular.