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)
Related
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.
I am trying to make a 2-D color plot with a script that formerly was dealing with a rectangle of data. However, I now need to use it to work with only a single point on the x axis having data.
What I've got is:
self.fig = plt.figure('Title', (10.,10.))
ax=plt.subplot(111)
im=ax.imshow(color_array,interpolation='none',extent=[100,100,50,150],aspect=0.15)
# setting labels, climate, color bar, saving image, etc..
I'm sure what's causing the issue is the extent = [100,100, I'm just not exactly sure how to write the code differently in order for the plot to show up as something other than a narrow vertical rectangle with nothing inside.
The color array is typically a 2-d array of numbers, but in this limited case, it is essentially a 1-d array. What happens is, there are three 2-d arrays, all the same dimensions, and two of them make up the x and y axes, and the third (the color array) determines the coloring of the field. Right now it they look (simplified) like this:
y-axis: [[90,100,110,120]]
x-axis: [[100,100,100,100]]
color: [[10,11,13,14]]
The answer turned out to be very simple. All that's required is a simple adjustment to the extent. Basically, just make sure the two values on the same axes have a distance of larger than 0 between them.
im=ax.imshow(color_array,interpolation='none',extent[100-1,100+1,50,150],aspect=0.15)
I used a value of 1 to separate the above, but really, any number will work (you can also scale it to the changing dimensions of your plots if you like it to be consistent).
I used an if statement to make sure that this only occurs in situations where the first index of the axis array is equal to the last index.
In my SFML bomberman clone I have a tilemap that consists of tiles.
Tiles can overlap, so those tiles that are farther down will be drawn over those tiles farther up. The same goes for all other entities like the player.
In order to draw all entities in the correct order, I thought about giving each entity some sort of z-coordinate (entities whose z-coord is lower will be drawn first).
My plan was to have one vector with ALL entities (tiles, player, bombs, power-ups, etc.) and whenever an element is added, the vector's elements are sorted from lowest to highest z-coord so they are drawn in the correct order.
My idea was to assign the following z-coords to the entities:
0: tiles in the 1st row
2: tiles in the 2nd row
4: tiles in the 3rd row
etc.
Entities that are for example on the 1st row of tiles will have a coord of 1. Those on the 2nd row will have a coord of 3, etc.
Does using a std::vector make sense for this? Maybe a list, or a map (z would be the key)? I'm struggling to find the best and easiest-to-use container for this.
I know this post lacks code, but I'm really lost and would be so grateful if someone could give me some hints.
Actually, you don't need vector for static tiles.
You can just create a normal 2D array of tiles.
It's faster and you don't need to use Z-buffer:Instead, you should just write for loop in a such way that tiles are rendered from top to bottom.
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
I don't usually make questions without some code in them but this time i cant find a starting point in what I want to do ,I want to split a sprite(uv ,vertices) to two other sprites(uv,vertices) between 2 points ,just like in fruit ninja where you split the fruits, but in 2d sprites.
I don't want you to write the code,just explain the general idea of how to do it .
I am using Libgdx if that matters
This process is called clipping.
In your case, you have a polygon defined by 4 vertices (including their positions and UV coordinates). You split this via a line given by two points.
A simple algorithm would check on which side of the line each of the 4 points is. If it is on the left side, add it to your first result, if it is on the right side, add it to your second. If two consecutive vertices end up on different sides of the line, you need to compute the intersection of the line and that edge and add it to both results.