Created
January 9, 2020 21:21
-
-
Save sdhegde/87af1fd1c79d6a380bc53d23f4774cd5 to your computer and use it in GitHub Desktop.
line detection
This file contains 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 cv2 as cv | |
import numpy as np | |
from matplotlib import pyplot as plt | |
import glob | |
import imutils | |
img_rgb = cv.imread('/home/sdh/Downloads/multiscale-template-matching-master/images/newspaper3.jpg') | |
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY) | |
blank_image = np.zeros(shape=img_rgb.shape, dtype=np.uint8) | |
for i in glob.glob("templates" + "/*.png"): | |
template = cv.imread('/home/sdh/Downloads/multiscale-template-matching-master/'+i,0) | |
w, h = template.shape[::-1] | |
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED) | |
threshold = 0.8 | |
loc = np.where( res >= threshold) | |
for pt in zip(*loc[::-1]): | |
#cv.rectangle(blank_image, pt, (pt[0] + w, pt[1] + h), (0,0,255), -1) | |
cv.circle(blank_image, (int(pt[0]+w/2), int(pt[1]+h/2)), 1, (0, 0, 255), -1) | |
cv.imwrite('res.png',blank_image) | |
#resized = imutils.resize(blank_image, width = int(blank_image.shape[1] * 0.25)) | |
#resized = cv.resize(blank_image, (int(blank_image.shape[0]/4), int(blank_image.shape[1]/4)), interpolation = cv.INTER_AREA) | |
dst = cv.Canny(blank_image, 50, 200, None, 3) | |
linesP = cv.HoughLinesP(dst, 1, np.pi/180, 20, None, 20, 12) | |
newblank_image = np.zeros((dst.shape[0], dst.shape[1], 3), dtype=np.uint8) | |
if linesP is not None: | |
for i in range(0, len(linesP)): | |
l = linesP[i][0] | |
angle = np.arctan2(l[1] - l[3], l[0] - l[2]) | |
deg = np.degrees([angle]) | |
if(deg[0] == 90.0 or deg[0] == 180.0): | |
cv.line(img_rgb, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3) | |
cv.imshow("Source", img_rgb) | |
cv.waitKey() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment