How to remove lines from the sides in opencv? - opencv

I have to remove some lines from the sides of hundreds of grayscale images.
In this image lines appear in three sides.
The lines are not consistent though, i.e, they appear above, below, left and/or right side of the image. And they are of unequal length and width.

If you could assume that the borders are free of important information, you may crop the photo like this:
C++ code:
cv::Mat img;
//load your image into img;
int padding=MAX_WIDTH_HEIGHT_OF_THE LINEAS_AREA
img=img(cv::Rect(padding,padding,img.cols-padding,img.rows-padding));
If not, you have to find a less dumb solution like this for example:
Findcontours
Delete contours that are far from the borders.
Draw contours on blank image
Apply hough line with suitable thresholds.
Delete contours that intersect with lines inside the image border.
Another solution, assuming the handwritten shape is connected:
Findcontours
Get the contour with the biggest area.
Draw it on a blank image with -1(fill) flag in the strock argument.
bitwise_and between the original image and the one you made
Another solution, asuming that the handwritten shape could be discontinuity :
Findcontours
Delete any contour that its all points are very close to the border (using euclidian distance with a threshold)
Draw all remaining contours on a blank image with -1(fill) flag in the strock argument.
bitwise_and between the original image and the one you made
P.S. I did not touch HoughLine transform since I do not about the shapes. I assume that some of them may contain very straight lines.

Related

Find contour without negative area inside shape

As a follow-up question to finding contiguous black pixels in image, I use OpenCV's findContours() to detect shapes of black on white (I invert the colors for the function to work better). In the images below, OpenCV detects the outer shape of the "g" and the inner bowl as different shapes:
I could use the hierarchy to discard shapes inside other shapes, but I would rather avoid it in case OpenCV detects an overarching contour around the whole image. Does findContours have some tuning that cause it to find only contiguous pixels and not the inside negative shape?

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.

Get (inner edge) contour pixel with opencv

I'm searching for a way to extract the "inner" contours of a binary image with opencv. I know that findContours extracts contours but I need the silhouette pixel which belong to the thresholded object in my binary image and not the outer contours.
Here is a fictive image which describes better what I'm searching for. I am searching for a method to extract the red contour.
I already tried a naive approach in copying the original binary image and shrinking the copy by 2 pixels each side and filling up the edges with black pixels and used findContoursbut the outcome is not satisfying.
You could just run findContours() on the negative image.
Little update:
I solved it with opencv erosion and dilatation.

Segmenting circle-like shapes out of Binary Image

I have several binary images and my task is to segment circle-like shape. The circles are not perfect rounded circle, but all of them will look like circle. Here are some example images and what I need:
As you can see from above, the left images are original images, and the right images are what I need to do. The circles intersect with other shapes, but I only want the circle, as indicated in red. The imaginary lines to close the circle will be required. What can I do in this case in Image Processing?
EDIT: in case, the image above is broken, here: http://imageshack.us/photo/my-images/835/circleonly.jpg/
Do you know the radii of the disks you are looking for?
If yes, morphological openings (erosion then dilation) would be straightforward, and very fast. The result using Mathematica:
Opening[img, DiskMatrix[15]]
If not, as other proposed, computing the contour image and then using the Hough transform would be a method worth pursuing. The image just above shows the contour image.
You can use hough transform, first you need is the edge image then you use a hough transform like you can see in this papers
http://www.cis.rit.edu/class/simg782/lectures/lecture_10/lec782_05_10.pdf
http://www.sci.utah.edu/~gerig/CS6640-F2010/FINALPROJECT/Ballard-GHT-1981.pdf
http://www.sciencedirect.com/science/article/pii/003132039290064P
http://www.markschulze.net/java/hough/

Resources