5

I am having trouble working with OpenCV and Python, I am new to the technology. Just some questions, how to crop the image after applying Hough line transform?

Here is my image. I want to crop the image with the ones who are having the red lines.

enter image description here

Here is my code for cropping image, and I know there is something wrong.

minLineLength = 100
maxLineGap = 10
rho = 1
theta = np.pi/180
threshold = 190
lines = cv2.HoughLines(opened, 1, np.pi/180, threshold)
for line in lines:
    for rho,theta in line:
        a = np.cos(theta)
        b = np.sin(theta)
        x0 = a*rho
        y0 = b*rho
        x1 = int(x0 + 1000*(-b))
        y1 = int(y0 + 1000*(a))
        x2 = int(x0 - 1000*(-b))
        y2 = int(y0 - 1000*(a))
        cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 2)
        cropped = image[100:200, 500:640]

I really need your help guys

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
rbo13
  • 365
  • 3
  • 5
  • 14

1 Answers1

5

I tried everything I could, but this is the closest I could get to.

I used homography to transform the image as follows:

#4 corner points of the image:
pts_src = np.array([[40.0, 124.0],[1017.0, 169.0],[960.0, 712.0],[60.0,697.0]])

#4 corner points of the white background to be transformed on:
pts_dst = np.array([[0.0, 0.0],[960.0, 0.0],[960.0, 575.0],[0.0, 575.0]])

#Obtain the homography matrix 'h':
h, status = cv2.findHomography(pts_src, pts_dst)

#Performing warp transform:
im_out = cv2.warpPerspective(img, h, (white.shape[1],white.shape[0]))
cv2.imwrite("Warped Source Image.jpg", im_out)

enter image description here

####Then I performed Canny edge:

grayw = cv2.cvtColor(im_out,cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(grayw,50,255)
cv2.imwrite("canny Image.jpg", canny)

enter image description here

Carrying out your subsequent steps of line detection using cv2.HoughLines might be a lot easier. If not, change the threshold value in the cv2.Canny(grayw,50,255) line given in the code.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • Thanks @JeruLuke. I will try this one out! – rbo13 Jan 22 '17 at 10:00
  • Having trouble following your code @JeruLuke. I got an unresolved reference to `(white.shape[1],white.shape[0])` – rbo13 Jan 24 '17 at 01:20
  • Add these lines before it: `white = np.zeros((575, 960, 3), np.uint8)` followed by `white[:] = (255, 255, 255)` – Jeru Luke Jan 24 '17 at 04:03
  • Okay I will try @JeruLuke. Thanks for the correction – rbo13 Jan 24 '17 at 08:13
  • Hi @JeruLuke. It works like charm now :) thank you so much! Just one more question... Can I crop every single books in the result? Or is there any way? – rbo13 Jan 26 '17 at 02:39
  • I have been thinking about it but still have no clue. One of my friends suggested trying **color clustering**, where each color(book) is obtained separately – Jeru Luke Jan 26 '17 at 03:00