How to get bounding rectangle of a set of point? - opencv

I want to get bounding rectangle of a set of points. The bounding rectangle must fit most of these points.
When I use cv2.minAreaRect ,I can only get rectangle that has min area.As shown below:
enter image description here
But the rectangle I want to get is shown by the red rectangle in the figure below, which can fit the points
enter image description here
how can I get such rectangle?
is there some fitting function can fit points to rectangle?
I really need some help,thank you!

Related

opencv - crop according to coloured points in an image

Given an image with say just two coloured points in it.. Is it possible to crop the image from the coordinates of the first colured point to the coordinates of the second coloured point .
A sample image where i have to crop between two green points
This is possible, if the colored points have a distinct range of color when compared with the rest of the image.
Algorithm:
1. Convert the image to HSV color space
2. Scan the image while looking for pixels in the range of hue and saturation of the color/s of the points.
3. Record minimum and maximum X,Y coordinates of the points that match.
4. calculate the bounding box of the region using the coordinates.
5. Crop the image using the bounding box.
You can try to follow these steps and edit the question with code if/when you come up with errors. Uploading a sample image somewhere and linking to it will help us provide better answers.

How can i fit an ellipse inside an object

I have the following image. I want to fit ellipse into each black region. Then I want to measure the major and minor axis length. I would be grateful if any one can give me any idea. I know how to fit an ellipse over the image object.

scilab - Drawing bounding box

So in scilab I did a analyzeblobs on my image and got a feature which is called BoundingBox which shows the rectangle around my object.
Now when I call this bounding Box I get 4 numbers, which I suppose are related to the corners of the rectangle.
What I don't know is that what are these numbers representing? Are they the pixel Index? or what?
Basically I want to calculate the width of the rectangle of my bounding box, so I need the coordinates of those four corners, but I don't know how to get it.
So I got the Answer:
the four elements are in order (x,y, width, height).
x,y are the coordinates of the top left corner
and the next two are the width and height of the rectangle.
So my second question has also been answered.

Is there a way to detect near-rectangle in opencv?

I'm going to find the most look-like rectangles among shapes. The first image is the original image with shapes which possibly be rectangles but they are not. The green rectangles in the second image is what I want. So is there a way to do this with opencv? I've tried hough lines but the result's not good
The source image:
And what I want is to find out the most look-like rectangle among these shapes, like the rectangles in green.
What I want:
A very simple approach is, after you have a rectangle bounding box around your shape, count the percentage of pixels inside the box which are white.
The higher the percentage of white pixels, the closest to a rectangle it is.
To get the bounding boxes you should take a look at either findContours from opencv, or some Blob extracting algorithm, you will find plenty of questions regarding those.
Edit:
Maybe you should first get the Minimum bounding rectangles of the shapes and then do this kind of heuristic:
Shrink the rectangle dimensions until the white-pixel percentage inside the rectangle reaches some threshold defined by you (like 90% of white pixels inside the rectangle).
To get the Minimum bounding rectangle (the smallest rectangle which contains the whole shape), you might check this tutorial:
http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rects_circles/bounding_rects_circles.html
One thing that might also help is doing the difference of sizes from the minimum bounding rectangle and the maximum inner rectangle (the biggest rectangle you can fit inside the white shape). The less difference there is between those rectangle's properties (width, height, area, center coordinates) the closest is the shape to a rectangle.

rotated crop in opencv

I am trying to crop a picture on right on along the contour. The object is detected using surf features and than i want to crop the image of extactly as detected.
When using crop some outside boundaries of other object is includes. I want to crop along the green line below. OpenCV has RotatedRect but i am unsure if its good for cropping.
Is there way to perfectly crop along the green line
I assume you get you get your example from http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html, so what you can do is to find the minimum axis aligned bounding box around the green bounding box, crop it from the image, use the inverted homography (H.inv()) matrix to transform that sub image into a new image (call cv::warpPerspective), and then crop your green bounding box (it should be axis aligned in your new image).
You can get the equations of the lines from the end points for each. Use these equations to check whether any given pixel lies within the green box or not i.e. does it lie between the left and right lines and between the top and bottom lines. Run this over the entire image and reset anything that doesn't lie within the box to black.
Not sure about in-built functionality to do this, but this simple methodology is guaranteed to work. For higher accuracy, you may want to consider sub-pixel checks.

Resources