#!/usr/bin/env python

import sys
import cv2
import numpy as np
import os
cmdargs = str(sys.argv)


file=str(sys.argv[1])
threshFile="/data/wsa/tmp/hough/thresh_"+os.path.basename(file)
houghFile="/data/wsa/tmp/hough/hough_"+os.path.basename(file)

#print threshFile,houghFile
threshold=str(sys.argv[2])
houghThresh=str(sys.argv[3])
maxLineGap=str(sys.argv[4])

img = cv2.imread(file)
ret,thresh1 = cv2.threshold(img,float(threshold),255,cv2.THRESH_BINARY_INV)

threshGray= cv2.cvtColor(thresh1,cv2.COLOR_BGR2GRAY)
cv2.imwrite(threshFile,threshGray)


#minLineLength = 200
#maxLineGap = 25


arr=np.empty(2)
arr[0]=20000
arr[1]=2500


#lines = cv2.HoughLinesP(threshGray,1,np.pi/180,150,arr)
#print int(maxLineGap)

lines = cv2.HoughLinesP(threshGray,1,np.pi/180,int(houghThresh),maxLineGap=int(maxLineGap), minLineLength=100)
print lines
if (lines is not None) and (len(lines[0]) != 0):
    print "lines found :",len(lines[0])

    for x1,y1,x2,y2 in lines[0]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

else:
    print "lines found : 0"


cv2.imwrite(houghFile,img)
