def main(argv): default_file = 'images/board.JPEG' filename = argv[0] if len(argv) > 0 else default_file # Loads an image src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE) # Check if image is loaded fine if src is None: print('Error opening image!') print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n') return -1 dst = cv.Canny(src, 50, 200, None, 3) # Copy edges to the images that will display the results in BGR cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR) cdstP = np.copy(cdst) lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0) if lines is not None: for i in range(0, len(lines)): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA) linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA) y = int(round(1536 / 3)) x = int(round(2048 / 3)) src = cv.resize(cdst, (x, y)) # Resize image cdst = cv.resize(cdst, (x, y)) # Resize image cdstP = cv.resize(cdstP, (x, y)) # Resize image cv.imshow("Source", src) cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst) cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP) cv.imwrite("./houghLines.png", cdstP) cv.waitKey() return 0
def track_x(x): cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img)
cv.imshow('window,img) def track_y(y): cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img) file = 'messi.jpg' # img = cv.imread(file, cv.IMREAD_GRAYSCALE) img0 = cv.imread(file, cv.IMREAD_COLOR) cv.imshow('window,img) w, h = 800, 600 x, y = 100, 100 d = 1 cv.createTrackbar('x', 'window, x, w, track_x) cv.createTrackbar('y', 'window, y, h, track_y) cv.line(img, (x, 0), (x, h), RED, d) cv.line(img, (0, y), (w, y), RED, d) cv.imshow('window,img) k = cv.waitKey(0) print('key', k) cv.imwrite('messigray.png',img) cv.destroyAllWindows()
import numpy as np import cv # Create a black image #img = np.zeros((512,512,3), np.uint8) img = cv.imread('great.jpg', 1) # Draw a diagonal blue line with thickness of 5 px img = cv.line(img,(0,0),(511,511),(255,0,0),5) cv.imwrite('blessed.jpg', img)