Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect holes, ends and beginnings of a line using openCV?

I'm trying to create a Python script that detects holes, ends and beginnings of a line. I thought that openCV would be great to achieve this.

So for example everything starts with this image:

enter image description here

finally what I want to achieve is this:

enter image description here

So I began with importing the image into Python and converting it in grayscale. Now I came to the idea to track the holes by using the goodFeaturesToTrack() method. It's normally used to find corners in the image.

However that didn't work so well because after that the script knows the points, but it doesn't know if a point is from a hole or if it's the beginning or end of the line. Another problem is that if I use another image this method detects more points than just the holes, beginnings and ends of the line.

Here is my full code to understand my problem a bit better:

import cv2
import numpy as np
import matplotlib.pyplot as plt

# lodes in img

img = cv2.imread('png1.png', cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

corners = cv2.goodFeaturesToTrack(img_gray, 200, 0.05, 10)

for corner in corners:
    x, y = corner.ravel()
    cv2.circle(img, (x,y), 7, (255,255,0), -1)

cv2.imshow('img',img)

I have no idea to get around this problem.

like image 396
RunningRails Avatar asked Mar 07 '16 15:03

RunningRails


People also ask

What is the OpenCV line detection function?

The OpenCV line detection function is a very useful function that is utilized in a plethora of applications especially related to image detection. This inbuilt function is used to isolate specific features with respect to uh particular shape, which is present within the image specified for processing.

How do you represent a line in OpenCV?

This representation is used in OpenCV). So Any line can be represented in these two terms, (r, θ). First it creates a 2D array or accumulator (to hold values of two parameters) and it is set to zero initially. Let rows denote the r and columns denote the (θ)theta. Size of array depends on the accuracy you need.

How do I use houghlines in OpenCV?

Everything explained above is encapsulated in the OpenCV function, cv2.HoughLines(). It simply returns an array of (ρ,ϴ) values where ρ is measured in pixels and ϴ is measured in radians. Below is a program of line detection using openCV and hough line transform.

How to find circles using OpenCV?

Below is the code for finding circles using OpenCV on the above input image. # Read image. # Convert to grayscale. # Blur using 3 * 3 kernel. # Apply Hough transform on the blurred image. # Draw circles that are detected. # Convert the circle parameters a, b and r to integers. # Draw the circumference of the circle.


1 Answers

I added a func getLandmarks() it returns all the wholes. So here I assume that it will be counted as a hole if there are 2 corners in a radius of 30 pix

if abs(x1-x2)<=30 and abs(y1-y2)<=30:

This line defines the range.

import cv2
import numpy as np

def getLandmarks(corners):
    holes=[]
    for i in range(0,len(corners)):
        for j in range(i+1,len(corners)):
            x1,y1=corners[i].ravel()
            x2,y2=corners[j].ravel()
            if abs(x1-x2)<=30 and abs(y1-y2)<=30:
                holes.append((int((x1+x2)/2),int((y1+y2)/2)))
    return holes

# lodes in img

img = cv2.imread('img.png', cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

corners = cv2.goodFeaturesToTrack(img_gray, 200, 0.05, 10)

holes=getLandmarks(corners)
print len(holes)
for corner in holes:
    cv2.circle(img, (corner), 7, (255,255,0), -1)

cv2.imshow('img',img)
cv2.waitKey(0)

output

enter image description here

Now for the Start and end You can easily sort the corners in either X(if the Path is along left to right) or Y(If the path is along top to down) and the min and max will be your start and end!

like image 106
Arijit Avatar answered Oct 12 '22 10:10

Arijit