def applyModelToImage(self,img,model,point): img_hsv = cv.CreateMat(img.height, img.width, cv.CV_8UC3) img_out = cv.CreateMat(img.height, img.width, cv.CV_8UC1) img_bin = cv.CreateMat(img.height, img.width, cv.CV_8UC1) img_ero = cv.CreateMat(img.height, img.width, cv.CV_8UC1) img_fill = cv.CreateMat(img.height, img.width, cv.CV_8UC1) img_smooth = cv.CreateMat(img.height, img.width, cv.CV_8UC1) cv.CvtColor(img,img_hsv,cv.CV_BGR2HSV) Logger.addImage(img_hsv, "img_hsv") pv.addColor(img_hsv,"hsv") h_plane=cv.CreateMat(img.height, img.width, cv.CV_8UC1) s_plane=cv.CreateMat(img.height, img.width, cv.CV_8UC1) cv.Split(img_hsv, h_plane, s_plane, None, None) planes = [h_plane, s_plane] cv.CalcBackProject([cv.GetImage(i) for i in planes],img_out,model) cv.Threshold(img_out, img_bin, 10, 255.0 , cv.CV_THRESH_BINARY) Logger.addImage(img_bin, "img_binary") pv.addBinary(img_bin,"binary") cv.Erode(img_bin,img_ero,iterations=2) Logger.addImage(img_ero, "img_eroded") cv.Smooth(img_ero,img_smooth,smoothtype=cv.CV_MEDIAN,param1=7) Logger.addImage(img_smooth, "img_median") pv.addBinary(img_smooth,"median") return img_smooth
def getAngle(self, image): img_tmp = cv.CreateMat(image.height, image.width, cv.CV_8UC1) cv.Copy(image, img_tmp) img_cont = cv.CreateMat(image.height, image.width, cv.CV_8UC3) img_hull = cv.CreateMat(image.height, image.width, cv.CV_8UC3) cv.Zero(img_hull) cv.CvtColor(image, img_cont, cv.CV_GRAY2BGR) contour = cv.FindContours( img_tmp, cv.CreateMemStorage(), mode=cv.CV_RETR_EXTERNAL, method=cv.CV_CHAIN_APPROX_NONE ) cv.DrawContours(img_cont, contour, (255, 0, 0), (0, 255, 0), 0, thickness=2) cv.FillConvexPoly(img_cont, contour, (255, 255, 255), lineType=8, shift=0) rect = cv.BoundingRect(contour) cv.Rectangle( img_cont, (int(rect[0]), int(rect[1])), (int(rect[0] + rect[2]), int(rect[1] + rect[3])), (0, 255, 0) ) line = cv.FitLine(contour, cv.CV_DIST_L2, 0, 0.01, 0.01) v0, v1, x0, x1 = line pt0 = int(x0 + 200 * v0), int(x1 + 200 * v1) pt1 = int(x0 - 200 * v0), int(x1 - 200 * v1) u0 = 0 u1 = 1 u = (u0, u1) pt2 = int(x0 + 200 * u0), int(x1 + 200 * u1) pt3 = int(x0 - 200 * u0), int(x1 - 200 * u1) v = (v0, v1) c = np.dot(u, v) / np.linalg.norm(u) / np.linalg.norm(v) angle = np.arccos(c) angle *= 180 / np.pi cv.Line(img_cont, pt0, pt1, (0, 0, 255), thickness=2, lineType=8, shift=0) cv.Line(img_cont, pt2, pt3, (0, 140, 255), thickness=2, lineType=8, shift=0) font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1.0, thickness=1) cv.PutText(img_cont, "Legend:", (10, 20), font, (255, 255, 255)) font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7, thickness=1) cv.PutText(img_cont, "Contour", (10, 50), font, (255, 0, 0)) cv.PutText(img_cont, "Bounding Rectangle", (10, 70), font, (0, 255, 0)) cv.PutText(img_cont, "Fitted Line", (10, 90), font, (0, 0, 255)) cv.PutText(img_cont, "Reference Line", (10, 110), font, (0, 140, 255)) Logger.addImage(img_cont, "Geometry") pv.addColor(img_cont, "geometry") return img_cont, angle
def showResult(self,image,angle): if angle < 20: mood = "Amazing!!!" elif angle < 67: mood = "Good!" elif angle < 112: mood = "Meh .." elif angle < 155: mood = "Bad." else: mood = "Catastrophic!!" font = cv.InitFont(cv.CV_FONT_HERSHEY_DUPLEX,1.0,1.0,thickness=2) cv.PutText(image, mood, (50,50) , font, (0,255,255)) Logger.addImage(image, "Result") pv.addColor(image,"result")