[英]OpenCV - Cropping image after applying Hough line transform
我在使用 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]
我真的需要你們的幫助
我盡我所能,但這是我能得到的最接近的。
我使用單應性來轉換圖像如下:
#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.HoughLines
。 如果不是,請更改代碼中給出的cv2.Canny(grayw,50,255)
行中的閾值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.