def draw_final(self, frame, paper_detection, hand_detection): hand_masked = image_analysis.apply_hist_mask(frame, hand_detection.hand_hist) paper_hand = paper_detection.paper_copy() self.plot_word_boxes(paper_hand, paper_detection.words) contours = image_analysis.contours(hand_masked) if contours is not None and len(contours) > 0: max_contour = image_analysis.max_contour(contours) hull = image_analysis.hull(max_contour) centroid = image_analysis.centroid(max_contour) defects = image_analysis.defects(max_contour) if centroid is not None and defects is not None and len( defects) > 0: farthest_point = image_analysis.farthest_point( defects, max_contour, centroid) if farthest_point is not None: self.plot_farthest_point(frame, farthest_point) self.plot_hull(frame, hull) # self.plot_contours(frame, contours) # self.plot_defects(frame, defects, max_contour) # self.plot_centroid(frame, (cx,cy)) self.plot_farthest_point(paper_hand, farthest_point) point = self.original_point(farthest_point) paper_detection.update_pointed_locations(point) self.text = paper_detection.get_most_common_word() self.plot_text(paper_hand, self.text) frame_final = np.vstack([paper_hand, frame]) return frame_final
def draw_final(self, frame, paper_detection, hand_detection): hand_masked = image_analysis.apply_hist_mask(frame, hand_detection.hand_hist) paper_hand = paper_detection.paper_copy() self.plot_word_boxes(paper_hand, paper_detection.words) contours = image_analysis.contours(hand_masked) if contours is not None and len(contours) > 0: max_contour = image_analysis.max_contour(contours) hull = image_analysis.hull(max_contour) centroid = image_analysis.centroid(max_contour) defects = image_analysis.defects(max_contour) if centroid is not None and defects is not None and len(defects) > 0: farthest_point = image_analysis.farthest_point(defects, max_contour, centroid) if farthest_point is not None: self.plot_farthest_point(frame, farthest_point) self.plot_hull(frame, hull) # self.plot_contours(frame, contours) # self.plot_defects(frame, defects, max_contour) # self.plot_centroid(frame, (cx,cy)) self.plot_farthest_point(paper_hand, farthest_point) point = self.original_point(farthest_point) paper_detection.update_pointed_locations(point) self.text = paper_detection.get_most_common_word() self.plot_text(paper_hand, self.text) frame_final = np.vstack([paper_hand, frame]) return frame_final
def draw_final(self, frame, paper_detection, hand_detection): hand_masked = image_analysis.apply_hist_mask(frame, hand_detection.hand_hist) # paper_hand = paper_detection.paper_copy() # self.plot_word_boxes(paper_hand, paper_detection.words) contours = image_analysis.contours(hand_masked) if contours is not None and len(contours) > 0: max_contour = image_analysis.max_contour(contours) hull = image_analysis.hull(max_contour) centroid = image_analysis.centroid(max_contour) defects = image_analysis.defects(max_contour) if centroid is not None and defects is not None and len(defects) > 0: farthest_point = image_analysis.farthest_point(defects, max_contour, centroid) if farthest_point is not None: self.plot_farthest_point(frame, farthest_point) self.plot_hull(frame, hull) point = self.original_point(farthest_point) self.farthest_point = farthest_point # print(farthest_point) x, y = farthest_point y -= 400 # a,b are the top-left coordinate of the rectangle and (c,d) be its width and height. # to judge a point(x0,y0) is in the rectangle, just to check # if a < x0 < a+c and b < y0 < b + d for i in range(9): #if farthest_point is in any of the rectangles #then we add that to string #self.hand_col_nw[i], self.hand_row_nw[i] a, b #self.hand_col_se[i],self.hand_row_se[i] c, d # # if i == 0: # print(i) # print(self.hand_col_nw[i], self.hand_row_nw[i]) # print(self.hand_col_se[i],self.hand_row_se[i]) # print(x, y) # print("=================") if self.hand_col_nw[i] < x and x < self.hand_col_se[i] and self.hand_row_nw[i] < y and y < self.hand_row_se[i]: print self.pw, i if len(self.pw) < 1 or self.pw[-1] != str(i): self.pw += str(i) frame_final = frame return frame_final
def draw_final(self, hand_masked): #On détecte les contours de la main. contours = image_analysis.contours(hand_masked) centroid = None # Si on contours n'est pas None et s'il y a au moins un contour détecté. if contours is not None and len(contours) > 0: #On détermine le plus grand contour. max_contour = image_analysis.max_contour(contours) #On détermine l'enveloppe convexe du plus grand contour. hull = image_analysis.hull(max_contour) #On trouve le centre de gravité de cette enveloppe convexe. centroid = image_analysis.centroid(hull) if centroid is not None: return (centroid) return(None)
save_file_name ='{}/cor_hist_{}.png'.format(to_dir_path,file) plt.savefig(save_file_name ) plt.close() ''' ret, thresh1 = cv2.threshold(gray_image, 254, 255, cv2.THRESH_BINARY) find_line_frame = thresh1.copy() find_line_frame = cv2.cvtColor(find_line_frame, cv2.COLOR_GRAY2RGB) #去雜訊 kernel = np.ones((3, 3), np.uint8) opening = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel) _, contours, hierarchy = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) max_contour = image_analysis.max_contour(contours) spine_contour = np.zeros(gray_image.shape, np.uint8) cv2.drawContours(spine_contour, [max_contour], 0, (255, 255, 255), -1) #找主骨幹 kernel = np.ones((11, 3), np.uint8) main_spine = cv2.erode(opening, kernel, iterations=3) main_spine = cv2.dilate(main_spine, kernel, iterations=3) _, contours, hierarchy = cv2.findContours(main_spine, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) max_contour = image_analysis.max_contour(contours) main_spine_contour = np.zeros(gray_image.shape, np.uint8) cv2.drawContours(main_spine_contour, [max_contour], 0, (255, 255, 255), -1) #找影像最上黑邊
def get_paper(self, frame): paper_masked = image_analysis.apply_hist_mask(frame, self.paper_hist) contours = image_analysis.contours(paper_masked) max_contour = image_analysis.max_contour(contours) paper = image_analysis.contour_interior(frame, max_contour) return paper