本文整理匯總了Python中cv2.HoughLinesP方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.HoughLinesP方法的具體用法?Python cv2.HoughLinesP怎麽用?Python cv2.HoughLinesP使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.HoughLinesP方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sim_noise
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def sim_noise(self, depthFile, rgbFile):
img=depthFile
imgcol=rgbFile
edges = cv2.Canny(img,100,200,apertureSize = 3)
edgescol = cv2.Canny(imgcol,100,200,apertureSize = 3)
edges += edgescol
mask=img.copy()
mask.fill(0)
minLineLength = 10
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,20,100,10)
if lines is not None:
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(mask,(x1,y1),(x2,y2),255,1)
for i in range(480):
for j in range(640):
if mask[i][j]>0:
cv2.circle(img,(j,i),2, (0,0,0), -1)
if random.random()>0.8:
cv2.circle(img,(j,i), random.randint(2,6), (0,0,0), -1)
return img
示例2: FindInternalBox
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def FindInternalBox(bw):
"""Finds where the puzzle card is located.
Detects all vertical and horizontal lines, and returns the largest
contour that bounds them"""
# Invert colors. HoughLines searches white lines on black background
target = 255 - bw.copy()
DebugShow(target)
lines = cv2.HoughLinesP(target, 1, np.pi / 180, 100, 100, 10)
if lines is None:
logging.debug("HoughLinesP failed")
return None
logging.debug("Found {} lines using HoughLinesP".format(len(lines)))
lines_image = np.zeros_like(target)
for line in lines:
for x1, y1, x2, y2 in line:
if abs(x1 - x2) < 20:
# vertical line
x = min(x1, x2)
cv2.line(lines_image, (x, y1), (x, y2), 255, 0)
if abs(y1 - y2) < 20:
y = min(y1, y2)
cv2.line(lines_image, (x1, y), (x2, y), 255, 0)
kernel = np.ones((5, 5), np.uint8)
lines_image = cv2.dilate(lines_image, kernel, iterations=2)
DebugShow(lines_image)
return FindExternalContour(lines_image)
示例3: lines
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def lines(self):
lines = cv2.HoughLinesP(self.image, 1, np.pi / 2, 6, None, 50, 10)
for line in lines[0]:
pt1 = (line[0], line[1])
pt2 = (line[2], line[3])
cv2.line(self.image, pt1, pt2, (0, 0, 255), 2)
示例4: line_detect_possible_demo
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def line_detect_possible_demo(image):
global img
# 提取圖片中黃色區域(hsv空間)
lower_blue = np.array([26, 43, 46])
upper_blue = np.array([34, 255, 255])
frame = image
# cv2.imshow('Capture', frame)
# change to hsv model
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# get mask
mask = cv2.inRange(hsv, lower_blue, upper_blue)
# detect red
res = cv2.bitwise_and(frame, frame, mask=mask)
ret, binary = cv2.threshold(res, 200, 255, cv2.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
# 圖像進行膨脹處理
dilation = cv2.dilate(binary, kernel)
# 圖像腐蝕處理
erosion = cv2.erode(dilation, kernel)
erosion = cv2.cvtColor(erosion, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(erosion, 100, 150, apertureSize=3)
#edges = cv2.Canny(binary, 50, 150, apertureSize=3) # apertureSize是sobel算子大小,隻能為1,3,5,7
# cv2.imshow('1',edges)
# cv2.waitKey(0)
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=0,maxLineGap=10) #函數將通過步長為1的半徑和步長為π/180的角來搜索所有可能的直線
list= []
for i in range(len(lines)):
x1 = lines[i][0][0]
y1 = lines[i][0][1]
x2 = lines[i][0][2]
y2 = lines[i][0][3]
list.append((x1,y1,x2,y2))
cv2.line(image,(x1,y1),(x2,y2),(0,0,255),2)
return list
示例5: crop_point_hough
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def crop_point_hough(crop_points):
height = len(crop_points)
width = len(crop_points[0])
#crop_line_data = cv2.HoughLinesP(crop_points, 1, math.pi/180, 2, 10, 10)
crop_line_data = cv2.HoughLines(crop_points, HOUGH_RHO, HOUGH_ANGLE, HOUGH_THRESH)
crop_lines = np.zeros((height, width, 3), dtype=np.uint8)
if crop_line_data != None:
crop_line_data = crop_line_data[0]
#print(crop_line_data)
if len(crop_line_data[0]) == 2:
for [rho, theta] in crop_line_data:
#print(rho, theta)
if (theta <= ANGLE_THRESH) or (theta >= math.pi-ANGLE_THRESH):
a = math.cos(theta)
b = math.sin(theta)
x0 = a*rho
y0 = b*rho
point1 = (int(round(x0+1000*(-b))), int(round(y0+1000*(a))))
point2 = (int(round(x0-1000*(-b))), int(round(y0-1000*(a))))
cv2.line(crop_lines, point1, point2, (0, 0, 255), 2)
elif len(crop_line_data[0]) == 4:
for [x0, y0, x1, y1] in crop_line_data:
cv2.line(crop_lines, (x0, y0), (x1, y1), (0, 0, 255), 2)
else:
print("No lines found")
return crop_lines
示例6: process_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def process_img(image):
original_image = image
# convert to gray
processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# edge detection
processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img,(5,5),0)
vertices = np.array([[10,500],[10,300],[300,200],[500,200],[800,300],[800,500],
], np.int32)
processed_img = roi(processed_img, [vertices])
# more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
# rho theta thresh min length, max gap:
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
try:
l1, l2 = draw_lanes(original_image,lines)
cv2.line(original_image, (l1[0], l1[1]), (l1[2], l1[3]), [0,255,0], 30)
cv2.line(original_image, (l2[0], l2[1]), (l2[2], l2[3]), [0,255,0], 30)
except Exception as e:
print(str(e))
pass
try:
for coords in lines:
coords = coords[0]
try:
cv2.line(processed_img, (coords[0], coords[1]), (coords[2], coords[3]), [255,0,0], 3)
except Exception as e:
print(str(e))
except Exception as e:
pass
return processed_img,original_image
示例7: process_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def process_img(image):
original_image = image
# convert to gray
processed_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# edge detection
processed_img = cv2.Canny(processed_img, threshold1 = 200, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img,(5,5),0)
vertices = np.array([[10,500],[10,300],[300,200],[500,200],[800,300],[800,500],
], np.int32)
processed_img = roi(processed_img, [vertices])
# more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
# rho theta thresh min length, max gap:
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
m1 = 0
m2 = 0
try:
l1, l2, m1,m2 = draw_lanes(original_image,lines)
cv2.line(original_image, (l1[0], l1[1]), (l1[2], l1[3]), [0,255,0], 30)
cv2.line(original_image, (l2[0], l2[1]), (l2[2], l2[3]), [0,255,0], 30)
except Exception as e:
print(str(e))
pass
try:
for coords in lines:
coords = coords[0]
try:
cv2.line(processed_img, (coords[0], coords[1]), (coords[2], coords[3]), [255,0,0], 3)
except Exception as e:
print(str(e))
except Exception as e:
pass
return processed_img,original_image, m1, m2
示例8: process_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def process_img(original_image):
processed_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
processed_img = cv2.Canny(processed_img, threshold1=200, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img, (3,3), 0 )
vertices = np.array([[10,500],[10,300], [300,200], [500,200], [800,300], [800,500]], np.int32)
processed_img = roi(processed_img, [vertices])
# edges
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
draw_lines(processed_img,lines)
return processed_img
示例9: process_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def process_img(image):
original_image = image
# edge detection
processed_img = cv2.Canny(image, threshold1 = 200, threshold2=300)
processed_img = cv2.GaussianBlur(processed_img,(5,5),0)
vertices = np.array([[10,500],[10,300],[300,200],[500,200],[800,300],[800,500],
], np.int32)
processed_img = roi(processed_img, [vertices])
# more info: http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
# rho theta thresh min length, max gap:
lines = cv2.HoughLinesP(processed_img, 1, np.pi/180, 180, 20, 15)
m1 = 0
m2 = 0
try:
l1, l2, m1,m2 = draw_lanes(original_image,lines)
cv2.line(original_image, (l1[0], l1[1]), (l1[2], l1[3]), [0,255,0], 30)
cv2.line(original_image, (l2[0], l2[1]), (l2[2], l2[3]), [0,255,0], 30)
except Exception as e:
print(str(e))
pass
try:
for coords in lines:
coords = coords[0]
try:
cv2.line(processed_img, (coords[0], coords[1]), (coords[2], coords[3]), [255,0,0], 3)
except Exception as e:
print(str(e))
except Exception as e:
pass
return processed_img,original_image, m1, m2
示例10: remove_line
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def remove_line(img):
gray = img.copy()
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 5
maxLineGap = 3
lines = cv2.HoughLinesP(edges,1,np.pi/180,15,minLineLength,maxLineGap)
mask = np.ones(img.shape[:2], dtype="uint8") * 255
if lines is not None:
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(mask,(x1,y1),(x2,y2),(0,0,0),2)
return cv2.bitwise_and(img, img, mask=mask)
示例11: get_avg_angle
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def get_avg_angle(a, draw=False):
lines = cv.HoughLinesP(a, 1, np.pi/180, 1, minLineLength=a.shape[1]*.30, maxLineGap=50)
angles = []
if lines is not None:
for line in lines[0]:
if draw:
cv.line(a, tuple(line[0:2]), tuple(line[2:]), 1, thickness=2)
angle = np.arctan2(line[3]-line[1], line[2]-line[0])
angles.append(angle)
return np.mean(angles)*degree_scaler
else:
return 0
示例12: draw_hough_outline
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def draw_hough_outline(self, arr):
arr = invert_bw(arr)
# import Image
# Image.fromarray(arr*255).show()
# h = cv.HoughLinesP(arr, 2, np.pi/4, 5, minLineLength=arr.shape[0]*.10)
h = cv.HoughLinesP(arr, 2, np.pi/4, 1, minLineLength=arr.shape[0]*.15, maxLineGap=5) #This
# h = cv.HoughLinesP(arr, 2, np.pi/4, 1, minLineLength=arr.shape[0]*.15, maxLineGap=1)
# h = cv.HoughLinesP(arr, 2, np.pi/4, 1, minLineLength=arr.shape[0]*.15)
PI_O4 = np.pi/4
# if h and h.any():
# if self._page_type == 'pecha':
# color = 1
# thickness = 10
# else: # Attempt to erase horizontal lines if page_type == book.
# # Why? Horizontal lines can break LineCluster if they are broken
# # e.g. couldn't be filtered out prior to line_breaker.py
# color = 0
# thickness = 10
if h is not None:
for line in h[0]:
new = (line[2]-line[0], line[3] - line[1])
val = (new[0]/np.sqrt(np.dot(new, new)))
theta = np.arccos(val)
if theta >= PI_O4: # Vertical line
# print line[1] - line[3]
# cv.line(arr, (line[0], 0), (line[0], arr.shape[0]), 1, thickness=10)
if line[0] < .5*arr.shape[1]:
arr[:,:line[0]+12] = 0
else:
arr[:,line[0]-12:] = 0
else: # horizontal line
if line[2] - line[0] >= .15 * arr.shape[1]:
# cv.line(arr, (0, line[1]), (arr.shape[1], line[1]), 1, thickness=50)
if line[1] < .5 *arr.shape[0]:
arr[:line[1]+17, :] = 0
else:
arr[line[1]-5:,:] = 0
return ((arr*-1)+1).astype(np.uint8)
示例13: get_lane_lines
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def get_lane_lines(inframe):
frame = inframe.copy()
ret_frame = np.zeros(frame.shape, np.uint8)
# We converted it into RGB when we normalized it
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
gray = get_median_blur(gray)
canny = get_canny(gray)
# Hough lines
# threshold = number of 'votes' before hough algorithm considers it a line
lines = cv2.HoughLinesP(canny, 1, np.pi/180, threshold=25, minLineLength=40, maxLineGap=100)
try:
r = lines.shape[0]
except AttributeError:
r = 0
for i in range(0):
for x1, y1, x2, y2 in lines[i]:
# Degrees as its easier for me to conceptualize
angle = math.atan2(y1-y2, x1-x2)*180/np.pi
# If it looks like a left or right lane
# Draw it onto the new image
if 100 < angle < 170 or -170 < angle < -100:
cv2.line(ret_frame, (x1, y1), (x2, y2), (255, 255, 255), 10)
return ret_frame
示例14: hough_lines
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def hough_lines(image):
"""
Creates hough line
Note that: `image` should be the output of a Canny transform.
:param image: |Image| camera frame
:return: hough lines (not the image with lines)
"""
return cv2.HoughLinesP(image, 2, np.pi / 180, 50, maxLineGap=50)
示例15: houghLines
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import HoughLinesP [as 別名]
def houghLines(self, edges, image, debug=True):
"""
Detects Hough lines
"""
# Detect hough lines
lines = cv2.HoughLinesP(edges, rho=1, theta=1 * np.pi / 180, threshold=40, minLineLength=100, maxLineGap=50)
N = lines.shape[0]
# Draw lines on image
New = []
for i in range(N):
x1 = lines[i][0][0]
y1 = lines[i][0][1]
x2 = lines[i][0][2]
y2 = lines[i][0][3]
New.append([x1,y1,x2,y2])
lines = [Line(x1=New[i][0],y1= New[i][1], x2= New[i][2], y2=New[i][3]) for i in range(len(New))]
# Categorise the lines into horizontal or vertical
horizontal, vertical = self.categoriseLines(lines)
# Filter out close lines based to achieve 9
# STANDARD THRESHOLD SHOULD BE 20
ver = filterClose(vertical, horizontal=False, threshold=20)
hor = filterClose(horizontal, horizontal=True, threshold=20)
#print(len(ver))
#print(len(hor))
# DEBUG TO SHOW LINES
if debug:
debugImg = image.copy()
self.drawLines(debugImg, ver)
self.drawLines(debugImg, hor)
#cv2.imshow("2 Hough Lines Found", debugImg)
cv2.imwrite("2HoughLinesFound.jpeg", debugImg)
return hor, ver