Commit a0c4908a authored by Gihan Jayatilaka's avatar Gihan Jayatilaka

screen-detection-4

parent 0c1dd7ff
......@@ -6,7 +6,7 @@ This is the python port of Hilight screen detector
#ling np.zeros(shape=(2,1),dtype=np.float32)
DEBUG=True
RESIZE_RATIO=0.3
RESIZE_RATIO=0.5
import numpy as np
import cv2
import sys
......@@ -48,7 +48,9 @@ slopeVertical=PI/48*5
slopeHorizontal=PI/48*19
def resizedFrame(frame,ratio=RESIZE_RATIO):
def resizedFrame(frame,ratio=RESIZE_RATIO,newSize=None):
if newSize!=None:
return cv2.resize(frame,(newSize[0],newSize[1]))
return cv2.resize(frame,(int(frame.shape[1]*ratio),int(frame.shape[0]*ratio)))
......@@ -64,6 +66,8 @@ def intersectionPoints(p1,p2,p3,p4):
return intersectionLines(line1,line2)
def intersectionLines(line1,line2):
if DEBUG:
print("DEBUG: Input for intersection finder line1 {} line2 {}".format(line1,line2))
X=0
Y=1
l1p1=line1[0,:]
......@@ -117,6 +121,9 @@ def intersectionLines(line1,line2):
b1=l1p1[Y] - a1*l1p1[X]
inter[Y]=a1*inter[X] + b1
print("DEBUG: Inter ans",inter)
# if (intersection.x>1280){
# intersection.x = 0;
......@@ -128,17 +135,26 @@ def intersectionLines(line1,line2):
if inter[Y]>720:
inter[Y]=0
print("DEBUG: Inter ans",inter)
return inter
def processFrame(inputFrame):
def processFrame(frame):
ans=np.zeros((4,2),dtype=np.float32)
processFrameUtil(inputFrame,ans[0],ans[1],ans[2],ans[3])
frame=resizedFrame(frame,newSize=(1280,720))
processFrameUtil(frame,ans[0],ans[1],ans[2],ans[3])
return ans
def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
X=0
Y=1
# bool ScreenDetector::processFrame(cv::Mat& inputFrame, Point& Top_left, Point& Top_right, Point& Bottom_left, Point& Bottom_right)
# getGray_screenDetector(inputFrame, grayImage);
grayFrame=cv2.cvtColor(inputFrame, cv2.COLOR_BGR2GRAY)
......@@ -198,7 +214,7 @@ def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
# //line detection
# cv::HoughLinesP(edges, hougnLines, m_rho, m_theta, m_threshold, m_minLineLength, m_maxLineGap);
#
if DEBUG: print("DENUG: lines before reshape",lines)
# if DEBUG: print("DENUG: lines before reshape",lines)
lines=np.reshape(lines,newshape=(lines.shape[0],4))
if DEBUG: print("DEBUG: lines ",np.shape(lines))
......@@ -236,11 +252,15 @@ def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
# }
# double dist = sqrt(pow(y_2-y_1, 2)+pow(x_2-x_1, 2));
dist=np.sqrt(np.power(y2-y1,2)+np.power(x2-x2,2))
dist=np.sqrt(np.power(y2-y1,2)+np.power(x1-x2,2))
# double tan_value = (y_2-y_1)/(x_2-x_1);
tanVal=(y2-y1)/(x2-x2)
# double atan_value = atan(tan_value);
atanVal=np.arctan(tanVal)
tanVal=np.Inf
atanVal=np.pi/2
if x2!=x1:
tanVal=(y2-y1)/(x2-x1)
# double atan_value = atan(tan_value);
atanVal=np.arctan(tanVal)
# double slope = std::abs(atan_value);
slope=np.abs(atanVal)
# if (check){
......@@ -310,10 +330,7 @@ def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
# }
# if (lines_top_1.x == 0){
cv2.imshow("Original",resizedFrame(inputFrame))
cv2.imshow("Edges",resizedFrame(edges))
cv2.imshow("Lines",resizedFrame(lineFrame))
cv2.waitKey(1000)
......@@ -438,17 +455,23 @@ def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
else:
linesRight2[Y]=linesBottom2[Y]
if DEBUG:
print("DEBUG: (Top lines)",linesTop1,linesTop2)
#
# Top_left = GetIntersection(lines_top_1, lines_top_2, lines_left_1, lines_left_2);
topLeft=intersectionPoints(linesTop1,linesTop2,linesLeft1,linesLeft2)
topLeft.__iadd__(intersectionPoints(linesTop1,linesTop2,linesLeft1,linesLeft2))
# Top_right = GetIntersection(lines_top_1, lines_top_2, lines_right_1, lines_right_2);
topRight=intersectionPoints(linesTop1,linesTop2,linesRight1,linesRight2)
topRight.__iadd__(intersectionPoints(linesTop1,linesTop2,linesRight1,linesRight2))
# Bottom_left = GetIntersection(lines_bottom_1, lines_bottom_2, lines_left_1, lines_left_2);
bottomLeft=intersectionPoints(linesBottom1,linesBottom2,linesLeft1,linesLeft2)
bottomLeft.__iadd__(intersectionPoints(linesBottom1,linesBottom2,linesLeft1,linesLeft2))
# Bottom_right = GetIntersection(lines_bottom_1, lines_bottom_2, lines_right_1, lines_right_2);
bottomRight=intersectionPoints(linesBottom1,linesBottom2,linesRight1,linesRight2)
bottomRight.__iadd__(intersectionPoints(linesBottom1,linesBottom2,linesRight1,linesRight2))
if DEBUG:
print("topL {} topR {} bottomL {} bottomR {}".format(topLeft,topRight,bottomLeft,bottomRight))
# if (Top_left.x == 0 || Top_left.y == 0 || Top_right.x == 0 || Top_right.y == 0|| Bottom_left.x == 0 || Bottom_left.y == 0 || Bottom_right.x == 0 || Bottom_right.y == 0){
# return false;
# }else{
......@@ -456,6 +479,29 @@ def processFrameUtil(inputFrame,topLeft,topRight,bottomLeft,bottomRight):
# }
# }
try:
for j in [lineFrame,inputFrame,edges]:
cv2.circle(j,(topLeft[0],topLeft[1]),5,[255,0,0],thickness=2)
cv2.circle(j,(topRight[0],topRight[1]),5,[255,0,0],thickness=2)
cv2.circle(j,(bottomLeft[0],bottomLeft[1]),5,[255,0,0],thickness=2)
cv2.circle(j,(bottomRight[0],bottomRight[1]),5,[255,0,0],thickness=2)
cv2.line(j,(topLeft[0],topLeft[1]),(topRight[0],topRight[1]),[255,0,0],thickness=2)
cv2.line(j,(topLeft[0],topLeft[1]),(bottomLeft[0],bottomLeft[1]),[255,0,0],thickness=2)
cv2.line(j,(bottomRight[0],bottomRight[1]),(topRight[0],topRight[1]),[255,0,0],thickness=2)
cv2.line(j,(bottomRight[0],bottomRight[1]),(bottomLeft[0],bottomLeft[1]),[255,0,0],thickness=2)
except:
if DEBUG: print("DEBUG: Vertex out of bounds")
cv2.imshow("Original",resizedFrame(inputFrame))
cv2.waitKey(1)
cv2.imshow("Edges",resizedFrame(edges))
cv2.waitKey(1)
cv2.imshow("Lines",resizedFrame(lineFrame))
cv2.waitKey(1000)
if __name__ == '__main__':
......@@ -479,4 +525,4 @@ if __name__ == '__main__':
cv2.destroyAllWindows()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment