Recognizing Image Target Lines - python

I'm trying to write a script that views an image, looks at lines on the image, and creates bounding boxes around the lines. Here is what I'm talking about...
I have this image:
I'm trying to have a way of intelligently cropping each section using a script. The best idea I came up with was to have colored tape around each of the sections like this:
Given this image with the colored tape the program should be able to find the colored lines and identify where they intersect. Here is a visual of what the program should be able to locate: (Black lines are where the tape is, red dots are the intersecting positions)
The end game here is for the program to be able to use this data to
Know how many sections there are (in this case 9)
Know where the sections are and create a bounding box around each one
Visually something like this:
OpenCV has facial detection and feature detection so something like this with a static image should be fairly possible. What is the best method to accomplish this?

There are many ways to do what you want.
One is to use sift:
https://docs.opencv.org/3.3.0/da/df5/tutorial_py_sift_intro.html
You will need to use keypoint detection, something like:
sift = cv2.SIFT()
kp = sift.detect(img,None)
You can check if the points are right with:
img2 = cv2.drawKeyPoints(kp)
Then you will need to use cv2.boundingRect
box = cv2.boundingRect(kp)
If your marker is of a different color then the rest of the image, you only need to make a color filter to find the points.

Related

How to use opencv functions on roi

My project is REM sleep detector, and the provided pictures show the contour of my eyelid. As my eye looks in directions, this contour moves in a distinct way. For lack of a better solution, my first attempt is to draw a grid of rois on my video stream, with that in place I want to use the countnonzero function or use blob detection on the rois. Depending on which rois in the grid change values, movement and direction is detected. (I am sure there is better way)
Problem: I can not specify one or several rois of my choice, the function always work only on the entire image. How do I retrieve values from each roi specifically? Rois are set up by means of multiple rectangle functions. Code is in python. Any help greatly appreciated.
Contour of eyelid:

OpenCV Python: retrieve rectangle main area from image then crop image

I'm trying to isolate the main area art from Pokemon cards and crop them. For example,
I want to extract the main area, as seen in the bounding box below.
I want to use the OpenCV in Python for the task. I've tried out shape detection and corner detection, but I can't seem to make them work as intended, and they seem to pick up anything but the main area that I want to extract.
I can't hard-code the bounding box because I want to process many cards and the position of the main area is different per card.
What are the steps needed to extract the main area and save a png file of just the main area?
If the area of interest is always contained inside a somewhat contrasted quasi-rectangular frame you can try your luck with a Hough transform, keep the long horizontal and vertical edges (obtained separately) and try to reconstitute that frame.
To begin, process a small number of cards and observe the results of Hough and figure out which systematic rules you could use to select the right segments by length/position/alignment/embedding in a larger frame...

How to crop image around box in python openCV?

I am working on a program to crop a image around a rectangle in OpenCV. How could I go about doing this. I also need it to be able to turn multiple rectangles into cropped images.
I've tried using this tutorial: https://www.pyimagesearch.com/2016/02/08/opencv-shape-detection/, but I dont know how to get the borders of the shape and crop around it.
I hope to get an output of multiple images, that have pictures of the contents of the triangle.
Thank you in advance
I have just recently done this for one of my projects, and it worked perfectly.
Here is the technique I use to implement this in Python OpenCV:
Display the image using OpenCV's cv2.imshow() function.
Select 2 points (x, y) on an image. This can be done by capturing mouse click events with OpenCV. One way to do this is to click with your mouse where the first point is, while still clicking move towards the second points, and let go from the mouse click once the cursor is over the correct point. This selects the 2 points for you. In OpenCV, you can do this with cv2.EVENT_LBUTTONDOWN and cv2.EVENT_LBUTTONUP. You can write a function to record the two points using the mouse capture events and pass it to cv2.setMouseCallback().
Once you have your 2 coordinates, you can draw a rectangle using OpenCV's cv2.rectangle() function, where you can pass the image, the 2 points and additional parameters such as the colour of the rectangle to draw.
Once you're happy with those results, you can crop the results using something like this:
image = cv2.imread("path_to_image")
cv2.setMouseCallback("image", your_callback_function)
cropped_img = image[points[0][1]:points[1][1], points[0][0]:points[1][0]]
cv2.imshow("Cropped Image", cropped_img)
cv2.waitKey(0)
Here is one of the results I get on one of my images.
Before (original image):
Region of interest selected with a rectangle drawn around it:
After (cropped image):
I started by following this excellent tutorial on how to implement it before further improving it on my own, so you can get started here: Capturing mouse click events with Python and OpenCV. You should also read the comments at the bottom of the attached tutorial to easily improve the code.
You can get co-ordinates of box using 'BoundedRect' function. Then use slicing operation, to extract required part of image.

How to draw multiple lines on a face image (facial graph) in OpenCV?

I would like to draw facial graph on a face captured by OpenCV using a webcam (a sample image is attached for a better understanding).
I have the node locations and I would like to draw a graph on a face. I want to use "CV2.line". But, the problem is that I need to repeat this code for every line that I want to draw. The total number of lines is 167.
Is there any better way to do that without repeating "CV2.line" code?
Please note that this is not a complete graph. Every node is connected to several specific nodes. So, For loop is not helpful here.
I tried networkx library. Unfortunately, networkx is not able to put the graph on a frame captured by OpenCV.
Here take a look at "Note" after polylines function:
"cv.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is a much better and faster way to draw a group of lines than calling cv.line() for each line."

Corner detection in Python OpenCV for extraction of squares

I am working on a form extraction module which detects text in specific segments of an image. So far I am able to remove the text and retain only the bounding box in the image.
My next step was to extract each box in the image. To do that I am trying to detect corners in the image. But here is where I am stuck. I tried template matching. This was the result. Although the results look promising the drawback is that this method is very time consuming. And few corners are still not detected.
I also tried Shi-Tomasi Corner Detector after dilating the image.
What would be the best approach to solve this problem?
I suggest you detect the lines instead, e.g. using Hough transform, followed by edge chaining, followed by robust line fitting on each chain.

Categories