我在使用 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, …Run Code Online (Sandbox Code Playgroud) 有什么方法可以使用 OpenCV 和 Python 来拉直这个图像吗?我正在使用不同的转换来解决它,但我无法得到它。
这是我的代码:
rows, cols, h = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
Run Code Online (Sandbox Code Playgroud)
然后我应用仿射变换。
dst = cv2.warpAffine(roi, M, (cols, rows))
Run Code Online (Sandbox Code Playgroud)
我仍然无法获得所需的图像输出来拉直。现在挠我的头快一个小时了。任何人都可以帮助我吗?
opencv image-processing image-rotation affinetransform python-2.7
我有一个像图像 这样
我想从书架上剪下每本书。我用这段代码开始了它。
thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)
cv2.imshow("Gray", gray)
cv2.waitKey(0)
cv2.imshow("Blurred", blur)
cv2.waitKey(0)
# detect edges in the image
edged = cv2.Canny(img, 10, 250)
cv2.imshow("Edged", edged)
cv2.waitKey(0)
# construct and apply a closing kernel to 'close' gaps between 'white'
# pixels
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 6))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel)
cv2.imshow("Closed", closed)
cv2.waitKey(0)
# loop over the contours
for contour in contours:
# peri = cv2.arcLength(contour, True)
# approx = cv2.approxPolyDP(contour, 0.02 * peri, True) …Run Code Online (Sandbox Code Playgroud)