VoodooChild92
VoodooChild92

Reputation: 2053

Python image processing : Help needed for corner detection in preferably PIL or any relevant module

I'm new to image processing and got to do corner detection for this image: enter image description here

In this image, I need to extract the starting and end points of each line segment or the coordinates of the corners. This is just a small part in my project and I'm stuck on this because I've no experience in image processing.

Upvotes: 8

Views: 4555

Answers (5)

Jeru Luke
Jeru Luke

Reputation: 21203

The accepted answer does not find ALL the corners in the image.

Using Harris corner detection, one can find all the possible corners. Since OP's choice of modules is not restricted, I chose to perform the following using the OpenCV library.

RESULT:

enter image description here

Every single corner present in the image is identified correctly.

THIS PAGE provides details of the algorithm and the code.

Upvotes: 2

Stefan van der Walt
Stefan van der Walt

Reputation: 7253

Here's a solution, using scikit-image:

from skimage import io, color, morphology
from scipy.signal import convolve2d
import numpy as np
import matplotlib.pyplot as plt

img = color.rgb2gray(io.imread('6EnOn.png'))

# Reduce all lines to one pixel thickness
snakes = morphology.skeletonize(img < 1)

# Find pixels with only one neighbor
corners = convolve2d(snakes, [[1, 1, 1],
                              [1, 0, 1],
                              [1, 1, 1]], mode='same') == 1
corners = corners & snakes

# Those are the start and end positions of the segments
y, x = np.where(corners)

plt.imshow(img, cmap=plt.cm.gray, interpolation='nearest')
plt.scatter(x, y)
plt.axis('off')
plt.show()

corners of line segments

Upvotes: 28

jminardi
jminardi

Reputation: 1066

I would recommend using OpenCV, which comes with both the Harris corner detector and the Shi-Tomasi corner detector.

Upvotes: 2

Demian Brecht
Demian Brecht

Reputation: 21368

This is a bit of a shot in the dark, but I'd assume that you could parse the png header to retrieve the width/height of the image (take a look at this RFC for png details). Combine this with the bit depth of the image and you should be able to determine where each corner is with simple math. Once you've found the corner, you should be able to follow the line using a simple algorithm checking neighboring pixel data.

This also sounds like homework. If so, you should tag it as such.

Upvotes: 0

louis.luo
louis.luo

Reputation: 2971

I don't know whether I understand the question correctly, but I think a low efficiency way to do it is to scan through each pixel and check the 4 directions around that pixel. If 2 non-opposite directions (i.e up & left or up & right etc.) are coloured, then it is a corner.

Hope this helps.

Upvotes: 1

Related Questions