Created
October 18, 2018 05:34
-
-
Save tatesuke/d66cb78140bb35de1261b8ccd3b6644e to your computer and use it in GitHub Desktop.
ハフ変換
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import math | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import cv2 | |
| import math | |
| img = cv2.imread("test.bmp", 0) | |
| rhoMax = int(math.hypot(img.shape[0], img.shape[1])) | |
| hough = np.zeros((90, rhoMax), np.uint8) | |
| poss = np.where(img == 0) | |
| for i in range(len(poss[0])): | |
| y = poss[0][i] | |
| x = poss[1][i] | |
| for degree in range(90): | |
| theta = math.radians(degree) | |
| rho = int(x * math.cos(theta) + y * math.sin(theta)) | |
| hough[degree][rho] += 1 | |
| argmax = hough.argmax() | |
| degree = int(argmax / rhoMax) | |
| radian = math.radians(degree) | |
| rho = argmax % rhoMax | |
| out = cv2.imread("test.bmp") | |
| x1 = 0 | |
| y1 = (rho - x1 * math.cos(radian)) / math.sin(radian) | |
| x2 = out.shape[1] | |
| y2 = (rho - x2 * math.cos(radian)) / math.sin(radian) | |
| cv2.line(out, (int(x1), int(y1)), (int(x2), int(y2)), (0,255,0), 2) | |
| cv2.imwrite("out.png", out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment