【问题标题】:OpenCV - Cropping image after applying Hough line transformOpenCV - 应用霍夫线变换后裁剪图像
【发布时间】:2017-05-30 22:10:44
【问题描述】:

我在使用 OpenCV 和 Python 时遇到问题,我是这项技术的新手。只是一些问题,应用霍夫线变换后如何裁剪图像?

这是我的形象。我想用有红线的人裁剪图像。

这是我裁剪图像的代码,我知道有问题。

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]

我真的需要你们的帮助

【问题讨论】:

  • 可以上传原图吗??
  • 嗨@JeruLuke 继承人原始图像。 Original Image

标签: python image opencv image-processing transform


【解决方案1】:

我尽我所能,但这是我能做到的最接近的。

我使用单应性对图像进行如下变换:

#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)

####然后我表演了Canny edge:

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

使用cv2.HoughLines 执行后续的线路检测步骤可能会容易得多。如果不是,请更改代码中给出的cv2.Canny(grayw,50,255) 行中的阈值。

【讨论】:

  • 谢谢@JeruLuke。我会试试这个!
  • 无法按照您的代码@JeruLuke。我有一个未解决的对 (white.shape[1],white.shape[0]) 的引用
  • 在它之前添加这些行:white = np.zeros((575, 960, 3), np.uint8) 后跟white[:] = (255, 255, 255)
  • 好的,我会试试@JeruLuke。感谢指正
  • 嗨@JeruLuke。它现在就像魅力一样 :) 非常感谢!还有一个问题……我可以裁剪结果中的每一本书吗?或者有什么办法吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多