Changeset - 6f867d8eac54
[Not reviewed]
default
0 0 1
Laman - 6 years ago 2019-01-07 00:53:24

exp: detecting grid with Hough transform
1 file changed with 92 insertions and 0 deletions:
0 comments (0 inline, 0 general)
exp/hough.py
Show inline comments
 
new file 100644
 
import os
 
import sys
 

	
 
import numpy as np
 
import cv2 as cv
 

	
 
from annotations import DataFile,computeBoundingBox
 

	
 

	
 
def show(img,filename="x"):
 
	cv.imshow(filename,img)
 
	cv.waitKey(0)
 
	cv.destroyAllWindows()
 

	
 

	
 
def filterVert(edges):
 
	# !! cv.morphologyEx()
 
	kernel = np.array([[1,0,1],[1,0,1],[1,0,1]],np.uint8)
 
	edges = cv.erode(edges,kernel)
 
	kernel=np.array([[0,1,0],[0,1,0],[0,1,0]],np.uint8)
 
	edges=cv.dilate(edges,kernel)
 
	return edges
 

	
 
def filterHor(edges):
 
	kernel = np.array([[1,1,1],[0,0,0],[1,1,1]],np.uint8)
 
	edges = cv.erode(edges,kernel)
 
	kernel=np.array([[0,0,0],[1,1,1],[0,0,0]],np.uint8)
 
	edges=cv.dilate(edges,kernel)
 
	return edges
 

	
 
def filterDiag(edges):
 
	kernel = np.array([[0,0,1],[1,0,0],[0,1,0]],np.uint8)
 
	edges1 = cv.erode(edges,kernel)
 
	kernel=np.array([[1,0,0],[0,1,0],[0,0,1]],np.uint8)
 
	edges1=cv.dilate(edges1,kernel)
 

	
 
	kernel = np.array([[0,1,0],[1,0,0],[0,0,1]],np.uint8)
 
	edges2 = cv.erode(edges,kernel)
 
	kernel=np.array([[0,0,1],[0,1,0],[1,0,0]],np.uint8)
 
	edges2=cv.dilate(edges2,kernel)
 

	
 
	return edges1+edges2
 

	
 

	
 
i=sys.argv[1]
 
annotations=DataFile("/home/laman/Projekty/python/oneEye/images/annotations.json.gz")
 
filename="{0}.jpg".format(i)
 
img=cv.imread(os.path.join("/home/laman/Projekty/python/oneEye/images/",filename))
 
(x1,y1,x2,y2)=computeBoundingBox(annotations[filename][0])
 
img=img[y1:y2, x1:x2, :]
 
# blurred=cv.GaussianBlur(img,(5,5),0)
 
# small=cv.resize(img,None,fx=0.5,fy=0.5,interpolation=cv.INTER_AREA)
 
small=img
 
clahe = cv.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
 
gray=cv.cvtColor(small,cv.COLOR_BGR2GRAY)
 
# gray=clahe.apply(gray)
 
show(gray)
 
edges=cv.Canny(gray,70,130)
 
show(edges)
 
edges=filterHor(edges)+filterVert(edges)+filterDiag(edges)
 
show(edges)
 

	
 

	
 
# kernel = np.ones((2,2),np.uint8)
 
# edges = cv.morphologyEx(edges, cv.MORPH_DILATE, kernel)
 
# show(edges)
 
# edges=cv.morphologyEx(edges,cv.MORPH_ERODE,kernel)
 
# show(edges)
 
colorEdges=cv.cvtColor(edges,cv.COLOR_GRAY2BGR)
 
# show(blurred)
 
# show(small)
 

	
 
# lines = cv.HoughLines(edges,1,np.pi/180,200)
 
# if lines is None: 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(colorEdges,(x1,y1),(x2,y2),(0,0,255),1)
 
lines = cv.HoughLinesP(edges,1,np.pi/180,80,minLineLength=50,maxLineGap=20)
 
if lines is None: lines=[]
 
for line in lines:
 
	x1,y1,x2,y2 = line[0]
 
	cv.line(colorEdges,(x1,y1),(x2,y2),(0,255,0),1)
 

	
 
show(colorEdges)
0 comments (0 inline, 0 general)