说我有一个像这样的图像:
我想要图像矩阵中黑条的起点和终点的位置。
我已经尝试了多种方法,例如Python OpenCV中的水平线检测,并想出了以下代码,使我的行突出显示:
import cv2
import numpy as np
from numpy import array
from matplotlib import pyplot as plt
import math
img = cv2.imread('caption.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150, apertureSize = 3)
lines = cv2.HoughLinesP(edges, 1,np.pi/180,350);
for line in lines[0]:
pt1 = (line[0],line[1])
pt2 = (line[2],line[3])
cv2.line(img, pt1, pt2, (0,0,255))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape
lines = cv2.HoughLines(edges,1,np.pi/180,350)
for rho,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
if int(b) == 1: #only horizontal lines with cos theta(theta = 0) = 1
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(img,(x1,y1),(x2,y2),(0,0,255),2)
cv2.imshow('edges', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
如果我尝试print x1, y1, x2, y2
我会
-1000 781 999 782 -1000 712 999 713
因此,这些显然不是像x为负的图像矩阵中的位置点。
这些行的起点和终点在图像矩阵中的位置是什么?我需要对该区域中的像素执行一些点操作,因此需要起点和终点。
这些行将始终返回-1000 +原始点
x1 = int(x0 + 1000*(-b))
x2 = int(x0 - 1000*(-b))
因为你只会进入这个循环,如果 int(b) == 1:
这意味着您需要直接打印x0,因为上述行将始终为(x0 + (-1000))
,在这种情况下,x0为0,因为它是从图像的左侧开始的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句