1
votes

I am new to image processing, and started learning scikit-image. I am trying to detect the corners of a rectangle, and then cropping the whole image to it. But I am lost in the sheer amount of segmentation and detection algorithms and don't know which one I need and how to do it.

This code generates a sample image. I want to crop it to the green rectangle. What do I need to do?

from matplotlib import pyplot as pyplot

import numpy as np
import skimage
from skimage import draw

img = np.zeros((500, 500, 3), dtype=np.double)

poly = np.array([[25, 25],
                 [25, 75],
                 [75, 75],
                 [75, 25]])

rr, cc = draw.polygon(poly[:, 0], poly[:, 1], img.shape)

img[rr, cc, 1] = 1
plt.imshow(img)
plt.show()

The task would be to detect the edges of the rectangle (poly array) and crop the image to it.

I tried harris corner detection, canny edge detection and many others, but am totally confused. This seems like a simple task but I am not getting it.

Would this be easier to do with OpenCV? Help please!

1
Yes. You should use OpenCV, check this SO post.Bill

1 Answers

0
votes

If you are tied to a contour-finding approach, OpenCV is currently the best place to go. If you can identify the object in some other way, skimage makes it quite easy to identify the coordinates and crop the image. For example:

import numpy as np
import skimage
from skimage import draw, measure
import matplotlib.pyplot as plt

# --- Generate test data ---
img = np.zeros((500, 500, 3), dtype=np.double)

poly = np.array([[25, 25],
                 [25, 75],
                 [75, 75],
                 [75, 25]])

rr, cc = draw.polygon(poly[:, 0], poly[:, 1], img.shape)

img[rr, cc, 1] = 1
# --- End generate test data ---

# Generate a mask with all objects close to green
green = (0, 1, 0)
mask = np.sum((img - green)**2, axis=2) < 0.1

# Label the different objects in the image
label_img = measure.label(mask)

# Find all objects
regions = measure.regionprops(label_img)

# Find the first object (since we only have one, that's the rectangle!)
r = regions[0]

# We'll use the `bbox` property to select the bounding box
# # For a full list of properties, take a look at the docstring of
# measure.regionprops

print('The bounding box is {}'.format(r.bbox))

r0, c0, r1, c1 = r.bbox
img_crop = img[r0:r1, c0:c1]

f, (ax0, ax1) = plt.subplots(1, 2)
ax0.imshow(img)
ax1.imshow(img_crop)
plt.show()

enter image description here