def detect_line(im, threshold, mode, ref=None, vote=7): if vote > 1: pass # print('vote: ', vote) # return im, [], None, None, None img = pil2cv(im) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = gray.copy() edges[gray <= threshold] = 255 edges[gray > threshold] = 0 lines = cv2.HoughLines(edges, 1, np.pi / 180, vote) if lines is None: return im, [], None, None, None, None remained_lines, angles, rmd_rhos, rmd_thetas, v_angles = _detect_line( lines, ref) if remained_lines is None and mode == 'red': return detect_line(im, threshold, mode, ref, vote + 1) # return cv2pil(img), angles, remained_lines color = (0, 0, 255) if mode == 'red' else (255, 0, 0) # print('Draw: ', end='') # print(remained_lines) if remained_lines is None: return im, [], None, None, None, None for l in remained_lines: (x1, y1), (x2, y2) = l cv2.line(img, (x1, y1), (x2, y2), color, 1) return cv2pil(img), angles, remained_lines, rmd_rhos, rmd_thetas, v_angles
def detect_line(im, threshold): img = pil2cv(im) gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) edges = gray.copy() edges[gray<threshold] = 255 edges[gray>threshold] = 0 # edges2 = gray.copy() # edges2[gray<170] = 255 # edges2[gray>170] = 0 lines = cv2.HoughLines(edges, 1, np.pi/180, 15) # lines2 = cv2.HoughLines(edges2, 1, np.pi/180, 1) if lines is None: return im, None remained_lines, most_comm_ele = _detect_line(lines) # print('red: ', end='') # print(remained_lines) for l in remained_lines: (x1,y1), (x2,y2) = l cv2.line(img, (x1,y1), (x2,y2),(0,0,255),1) # remained_lines2, most_comm_ele = _detect_line(lines2, ref_angle) # remained_lines2, most_comm_ele = _detect_line(lines2) # print('white: ', end='') # print(remained_lines2) # for l in remained_lines2: # (x1,y1), (x2,y2) = l # cv2.line(img, (x1,y1), (x2,y2),(255,0,0),1) # print(ref_angle) # print(most_comm_ele) return cv2pil(img), most_comm_ele
def HouLine(img, canny_img): lines = cv.HoughLines(canny_img, 1, np.pi / 180, 150) print(lines) for i in range(len(lines)): for rho, theta in lines[i]: 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)) cv.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv.imshow('houp', img)
def do_canny(frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) gray[:, :, 1] = 0 #gray[:, :, 2] = 0 cv2.imshow('gr', gray) cv2.waitKey() gray = cv2.cvtColor(gray, cv2.COLOR_HSV2RGB) gray = cv2.cvtColor(gray, cv2.COLOR_RGB2GRAY) _, gray = cv2.threshold(gray, 125, 255, cv2.THRESH_BINARY) gray = 255 - gray blur = cv2.GaussianBlur(gray, (3, 3), 10) cv2.imshow('blur', blur) cv2.waitKey() #kernel = np.ones((3, 3), np.uint8) kernel = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], np.uint8) erosion = cv2.dilate(gray, kernel, iterations=5) erosion = cv2.erode(erosion, kernel, iterations=3) cv2.imshow('erode', erosion) cv2.waitKey() canny = cv2.Canny(erosion, 50, 150) cv2.imshow("canny", canny) cv2.waitKey() lines = cv2.HoughLines(canny, 1, 1 * np.pi / 180, 120) 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(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.imshow('lines', frame) cv2.waitKey()
def line_detect(img, edges, vote=15): # edges = pil2cv(edges) img = pil2cv(edges) lines = cv2.HoughLines(edges, 1, np.pi / 180, vote) assert lines is not None angles = [] lines_rmd = [] for line in lines[:10]: rho, theta = line[0] if angle_in_range(math.degrees(theta), 45, verticle=True, diff=0.1): continue x1, y1, x2, y2 = calc_points(rho, theta) angle = calc_angle(x1, y1, x2, y2) angles.append(angle) lines_rmd.append(((x1, y1), (x2, y2))) return get_one_or_two_sets(angles), lines_rmd
def haugh_line_transform_demo(image): #霍夫直线变换,利用极坐标,需要添加画图语句 #dst = cv.pyrMeanShiftFiltering(image,10,50)霍夫变换对噪声敏感,可先降噪,再霍夫直线变换 gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) canny = cv.Canny(gray, 50, 150, apertureSize=3) lines = cv.HoughLines(canny, 1, np.pi / 180, 250) #返回值是极坐标下的ρ和θ print(lines) for line in lines: rho, theta = line[0] 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)) cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2) cv.imshow("result", image)
def keyboardYCoords(edged_img, rho = 1, theta = np.pi/180, threshold = None): if threshold is None: threshold = edged_img.shape[1]//2 # half the width lines = cv2.HoughLines(edged_img, rho, theta, threshold) y_cord = [] #the y-value of the lines generated from hough transform #iterating through lines for line in lines: rho_l, theta_l = line[0] a = np.cos(theta_l) b = np.sin(theta_l) x0 = a * rho_l y0 = b * rho_l y_cord.append(y0) #appending to list y_cord.sort(reverse=True) return y_cord[0:2]
def detect_line(im, threshold, mode, ref=None, vote=1): img = pil2cv(im) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = gray.copy() edges[gray < threshold] = 255 edges[gray > threshold] = 0 lines = cv2.HoughLines(edges, 1, np.pi / 180, vote) if lines is None: return im, [], None remained_lines, angles = _detect_line(lines, ref) if remained_lines is None and mode == 'red': return detect_line(im, threshold, mode, ref, vote + 1) for l in remained_lines: (x1, y1), (x2, y2) = l cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 1) # red line return cv2pil(img), angles, remained_lines
def getBorders(self): screen = np.array(ImageGrab.grab(bbox=(self.coordinate))) cv2.imshow("Line Detection", screen) gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY) smooth = ndi.filters.median_filter(gray, size=2) edges = smooth > 180 lines = cv2.HoughLines(edges.astype(np.uint8), 0.5, np.pi / 180, 120) print(len(lines)) for rho, theta in lines[0]: print(rho, theta) 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(screen, (x1, y1), (x2, y2), (0, 0, 255), 2) return cv2.imshow("Line Detection", screen)
def findlines(board, showImage=True): img = np.array(Image.open(board).convert("L")) original_img = img.copy() # Images that are too big yield far too many lines if img.shape[0] * img.shape[1] > 300000: # Keep width and height proportional scale_ratio = img.shape[1] / 512 new_width = int(np.round(img.shape[0] / scale_ratio)) img = cv2.resize(img, (512, new_width)) else: scale_ratio = 1 blur_gray = cv2.GaussianBlur(img, (5, 5), 0) edges = cv2.Canny(blur_gray, 60, 110) lines = cv2.HoughLines(edges, 1, np.pi / 180, 135) lines = lines.reshape((lines.shape[0], lines.shape[2])) vertical, horizontal = preProcessLines(lines, img) y_corners, x_corners = findintersect(vertical, horizontal) x_corners = np.round(x_corners * scale_ratio).astype(np.int) y_corners = np.round(y_corners * scale_ratio).astype(np.int) if showImage: for i in range(9): for j in range(9): x = x_corners[i, j] y = y_corners[i, j] original_img = cv2.circle(original_img, (x, y), 3, [255, 0, 0], 2) cv2.imshow('hough', original_img) cv2.waitKey(0) cv2.destroyAllWindows() return (x_corners, y_corners)
img = cv2.resize(img,(img.shape[1] // 8,img.shape[0] // 8)) # 模糊 blur_img = cv2.GaussianBlur(img,(3,3),0) # 找梯度值 g,angle = sobel(blur_img) # 最大值抑制 border = nms(g,angle) # 找有和強像素相連的弱像素 canvas = connected(border,100,200) # 貼簽名 paste_signature(canvas,signature) # hough transform lines = cv2.HoughLines(canvas,1,np.pi / 180,80) lines = np.squeeze(lines) black = np.zeros_like(img) for rho,theta in lines: 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(black,(x1,y1),(x2,y2),(255,0,0),1) paste_signature(black,signature) # cv2.imwrite('myCanny.jpg',canvas)
from cv2 import cv2 import numpy as np img = cv2.imread('image/dave.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 50, 150, apertureSize=3) # cv2.imshow('edge', edges) # cv2.waitKey() lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) for rho, theta in lines[0]: 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(img, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.imwrite('houghlines3.jpg', img)
img = cv2.resize(img, (img.shape[1] // 8, img.shape[0] // 8)) # 模糊 blur_img = cv2.GaussianBlur(img, (3, 3), 0) # 找梯度值 g, angle = sobel(blur_img) # 最大值抑制 border = nms(g, angle) # 找有和強像素相連的弱像素 canvas = connected(border, 100, 200) # 貼簽名 paste_signature(canvas, signature) # hough transform lines = cv2.HoughLines(canvas, 1, math.pi / 180.0, 120) if lines is not None: a, b, c = lines.shape for i in range(a): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0, y0 = a * rho, b * rho pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) # cv2.line( img2, pt1, pt2, ( 255, 0, 0 ), 1, cv2.LINE_AA ) cv2.line(img2, pt1, pt2, (255, 0, 0), 1) # cv2.imwrite('myCanny.jpg',canvas)
# convert to grayscale then black/white to binary image frame = cv.cvtColor(masked, cv.COLOR_BGR2GRAY) thresh = 200 frame = cv.threshold(frame, thresh, 255, cv.THRESH_BINARY)[1] cv.imshow("Black/White", frame) # blur image to help with edge detection blurred = cv.GaussianBlur(frame, (11, 11), 0) # cv.imshow("Blurred", blurred) # identify edges & show on screen edged = cv.Canny(blurred, 30, 150) cv.imshow("Edged", edged) # perform full Hough Transform to identify lane lines lines = cv.HoughLines(edged, 1, np.pi / 180, 25) # define arrays for left and right lanes rho_left = [] theta_left = [] rho_right = [] theta_right = [] # ensure cv.HoughLines found at least one line if lines is not None: # loop through all of the lines found by cv.HoughLines for i in range(0, len(lines)): # evaluate each row of cv.HoughLines output 'lines' for rho, theta in lines[i]:
or (edge[i - 1, j - 1] < 200) or (edge[i - 1, j + 1] < 200) or (edge[i + 1, j + 1] < 200) or (edge[i + 1, j - 1] < 200)): canvas[i, j] = 255 canvas = np.uint8(edge) # 去背簽名檔(size = 200*125) height = canvas.shape[0] weight = canvas.shape[1] canvas[height - 125:, weight - 200:] = cv2.bitwise_or(canvas[height - 125:, weight - 200:], signature) # Hough Transform img2 = canvas.copy() # 參數1 : 灰度圖、參數2與 : 分別是\rho和\theta的精確度、參數4:閾值T lines = cv2.HoughLines(img2, 1, math.pi / 180.0, 135) if lines is not None: a, b, c = lines.shape for i in range(a): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0, y0 = a * rho, b * rho # 由引數空間向實際座標點轉換 pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) cv2.line(img2, pt1, pt2, (255, 0, 0), 1) # cv2.imwrite('canny3.jpg',canvas) # cv2.imwrite('Hough3.jpg',img2)
def square_line(origin, edges, hough): ''' 输入:原始图片,黑白边线图,hough数组 输出:带坐标的原图,Table_2D ''' internal_calibration = get_camera_intrinsic() internal_calibration = np.array(internal_calibration, dtype='float32') distCoeffs = np.zeros((8, 1), dtype='float32') img = copy.copy(origin) lines = cv2.HoughLines(edges, hough[0], hough[1], hough[2]) # rho:ρ,图片左上角向直线所做垂线的长度 # theta:Θ,图片左上角向直线所做垂线与顶边夹角 # 垂足高于原点时,ρ为负,Θ为垂线关于原点的对称线与顶边的夹角 top_line_theta = [] top_line_rho = [] left_line_theta = [] left_line_rho = [] right_line_theta = [] right_line_rho = [] bottom_line_theta = [] bottom_line_rho = [] horizon = [] summ = 0 final_lines = np.zeros((4, 2)) if len(lines) < 4: print("\033[0;31m未检测到方桌!\033[0m") return edges else: for line in lines: for rho, theta in line: if (theta > math.pi / 3 and theta < math.pi * 2 / 3): # 横线 horizon.append(line) elif rho < 0: # 右边 right_line_rho.append(rho) right_line_theta.append(theta) else: # 左边 left_line_theta.append(theta) left_line_rho.append(rho) top, bottom = Cluster(horizon, 180, 360) # 将横线依据abs(rho)分为上下 for line in top: for rho, theta in line: top_line_rho.append(rho) top_line_theta.append(theta) for line in bottom: for rho, theta in line: bottom_line_rho.append(rho) bottom_line_theta.append(theta) for i in right_line_theta: summ += i right_line_theta_average = summ / len(right_line_theta) final_lines[0, 1] = right_line_theta_average summ = 0 for i in right_line_rho: summ += i right_line_rho_average = summ / len(right_line_rho) final_lines[0, 0] = right_line_rho_average summ = 0 for i in left_line_theta: summ += i left_line_theta_average = summ / len(left_line_theta) final_lines[1, 1] = left_line_theta_average summ = 0 for i in left_line_rho: summ += i left_line_rho_average = summ / len(left_line_rho) final_lines[1, 0] = left_line_rho_average summ = 0 for i in top_line_theta: summ += i top_line_theta_average = summ / len(top_line_theta) final_lines[2, 1] = top_line_theta_average summ = 0 for i in top_line_rho: summ += i top_line_rho_average = summ / len(top_line_rho) final_lines[2, 0] = top_line_rho_average summ = 0 for i in bottom_line_theta: summ += i bottom_line_theta_average = summ / len(bottom_line_theta) final_lines[3, 1] = bottom_line_theta_average summ = 0 for i in bottom_line_rho: summ += i bottom_line_rho_average = summ / len(bottom_line_rho) final_lines[3, 0] = bottom_line_rho_average summ = 0 # print(final_lines) final_lines = np.array(final_lines) for i in range(4): theta = final_lines[i, 1] rho = final_lines[i, 0] 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(img, (x1, y1), (x2, y2), (200, 135, 100), 2) # 标记直线编号 string = str(i) cv2.putText(img, string, (int(x0), int(y0)), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 0, 255), 1) left_top_point_x, left_top_point_y = getcrosspoint( left_line_rho_average, left_line_theta_average, top_line_rho_average, top_line_theta_average) left_bottom_point_x, left_bottom_point_y = getcrosspoint( left_line_rho_average, left_line_theta_average, bottom_line_rho_average, bottom_line_theta_average) right_top_point_x, right_top_point_y = getcrosspoint( right_line_rho_average, right_line_theta_average, top_line_rho_average, top_line_theta_average) right_bottom_point_x, right_bottom_point_y = getcrosspoint( right_line_rho_average, right_line_theta_average, bottom_line_rho_average, bottom_line_theta_average) Table_2D = [] Table_2D.append([left_top_point_x, left_top_point_y]) Table_2D.append([left_bottom_point_x, left_bottom_point_y]) Table_2D.append([right_top_point_x, right_top_point_y]) Table_2D.append([right_bottom_point_x, right_bottom_point_y]) cv2.circle(img, (left_top_point_x, left_top_point_y), 3, (255, 0, 0), -1) cv2.circle(img, (left_bottom_point_x, left_bottom_point_y), 3, (255, 0, 0), -1) cv2.circle(img, (right_top_point_x, right_top_point_y), 3, (255, 0, 0), -1) cv2.circle(img, (right_bottom_point_x, right_bottom_point_y), 3, (255, 0, 0), -1) Table_3D = [] Table_3D.append([0, 0, 0]) Table_3D.append([0, 55, 0]) Table_3D.append([55, 0, 0]) Table_3D.append([55, 55, 0]) Table_3D = np.array(Table_3D, dtype='float32') Table_2D = np.array(Table_2D, dtype='float32') _, rvector, tvector = cv2.solvePnP(Table_3D, Table_2D, internal_calibration, distCoeffs) axis = np.float32([[55, 0, 0], [0, 55, 0], [0, 0, -20]]).reshape(-1, 3) imgpts, _ = cv2.projectPoints( axis, rvector, tvector, internal_calibration, distCoeffs, ) lined = draw(img, (left_top_point_x, left_top_point_y), imgpts) return lined, Table_2D
y2, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA): cv2.line(img, (x1, y1), (x2, y2), color, thickness, lineType) # 1й Способ: HoughLines() - стандартное преобразование Хафа img = cv2.imread('6_2.png') img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) invers_img_gray = cv2.bitwise_not(img_gray) edges = cv2.Canny(img_gray, 75, 230) lines = cv2.HoughLines(image=edges, rho=1, theta=pi / 360, threshold=150) for line in lines: rho, theta = line[0] a = cos(theta) b = sin(theta) print("a, b: ", a, b) x0 = a * rho y0 = b * rho print("xo, yo: ", x0, y0) # 1200 - длина линии x1 = int(x0 + 1200 * (-b)) y1 = int(y0 + 1200 * (a)) x2 = int(x0 - 1200 * (-b)) y2 = int(y0 - 1200 * (a))
import pylab as pl from scipy.ndimage import filters from PIL import Image from cv2 import cv2 as cv im = cv.imread("/home/aaryen/Desktop/opencv-master/samples/data/sudoku.png") imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY) edges = cv.Canny(imgray, 50, 150, apertureSize=3) lines = cv.HoughLines(edges, 1, pl.pi / 180, 200) for _ in range(len(lines)): for rho, theta in lines[_]: a = pl.cos(theta) b = pl.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)) cv.line(im, (x1, y1), (x2, y2), (0, 255, 0), 2) cv.imshow("Window", im) cv.waitKey(0)