# draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0, 255, 0), 3) cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 5, True) for ctr in contours] heptagons = [ctr for ctr in contours if len(ctr) == 7] arrows = [hepta for hepta in heptagons if isArrow(hepta)] #tips = [ tip(a) for a in arrows ] #contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # ============================================ # draw on the frame #cv2.drawContours(frame, heptagons, -1, (0,255,0), 3) cv2.drawContours(frame, arrows, -1, (255, 0, 0), -1) # draw tips #for t in tips: # cv2.circle(frame, tuple(t), 5, (0, 0, 255), -1) return frame if __name__ == "__main__": webcam_gui(imgproc, 1)
# convert image to black and white and show it thresh1, thresh = cv2.threshold(edge, 60, 120, cv2.THRESH_BINARY) cv2.imshow('thresh', thresh) # find contours! contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0, 255, 0), 3) cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 5, True) for ctr in contours] contours = [ctr for ctr in contours if len(ctr) == 4] contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # ============================================ # draw on the frame cv2.drawContours(frame, contours, -1, (0, 255, 0), 3) return frame if __name__ == "__main__": webcam_gui(imgproc, 0)
# convert image to black and white and show it thresh1, thresh = cv2.threshold(edge, 60, 255, cv2.THRESH_BINARY) cv2.imshow('thresh', thresh) # find contours! contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0,255,0), 3) cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 5 , True) for ctr in contours] contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # ============================================ # draw on the frame cv2.drawContours(frame, contours, -1, (0,255,0), 3) return frame if __name__ == "__main__": webcam_gui(imgproc, video_src=1)
if len(ctr) < 5: return False max_x = ctr[0][0][0] max_y = ctr[0][0][1] min_x = ctr[0][0][0] min_y = ctr[0][0][1] for pt in ctr: if max_x < pt[0][0]: max_x = pt[0][0] if max_y < pt[0][1]: max_y = pt[0][1] if min_x > pt[0][0]: min_x = pt[0][0] if min_y > pt[0][1]: min_y = pt[0][1] ratio = 1.0 * (max_x - min_x) / (max_y - min_y) if ratio > 1.1 or 1 / ratio > 1.1: return False x = (max_x + min_x) / 2 y = (max_y + min_y) / 2 r = ((max_x - min_x) + (max_y - min_y)) / 4 for pt in ctr: ratio = (1.0 * ((pt[0][0] - x)**2 + (pt[0][1] - y)**2) / r**2)**0.5 if ratio > 1.1 or 1 / ratio > 1.1: return False return True if __name__ == "__main__": webcam_gui(imgproc, video_src=0)
# convert image to black and white and show it thresh1, thresh = cv2.threshold(edge, 60, 120, cv2.THRESH_BINARY) cv2.imshow('thresh', thresh) # find contours! contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0, 255, 0), 3) cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 5, True) for ctr in contours] contours = [ctr for ctr in contours if len(ctr) == 3] contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # ============================================ # draw on the frame cv2.drawContours(frame, contours, -1, (0, 255, 0), 3) return frame if __name__ == "__main__": webcam_gui(imgproc, 0)
if debug: cv2.imshow('blured edge', edge) # convert image to black and white and show it thresh1, thresh = cv2.threshold(edge, 60, 255, cv2.THRESH_BINARY) if debug: cv2.imshow('thresh', thresh) # find contours! contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0,255,0), 3) if debug: cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need parallelograms = find_parallelograms(contours) # ============================================ # draw on the frame cv2.drawContours(frame, parallelograms, -1, (0,255,0), 3) return frame if __name__ == "__main__": webcam_gui(contour_proc)
def imgproc(frame): # convert color to gray scale and show it gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('gray', gray) # convert image to black and white and show it thresh1, thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY) cv2.imshow('thresh', thresh) # find contours! contours, hry = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0,255,0), 3) cv2.imshow('cpframe', cpframe) # do various tests and modification contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 30 , True) for ctr in contours] contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # draw on the frame cv2.drawContours(frame, contours, -1, (0,255,0), 3) return frame if __name__ == "__main__": webcam_gui(imgproc)
# draw all the contours cpframe = frame.copy() cv2.drawContours(cpframe, contours, -1, (0,255,0), 3) cv2.imshow('cpframe', cpframe) # ================== TODO =================== # Modify these code to suit your need contours = [ctr for ctr in contours if cv2.contourArea(ctr) > 100] contours = [cv2.approxPolyDP(ctr, 5, True) for ctr in contours] heptagons = [ctr for ctr in contours if len(ctr) == 7] arrows = [hepta for hepta in heptagons if isArrow(hepta)] #tips = [ tip(a) for a in arrows ] #contours = [ctr for ctr in contours if cv2.isContourConvex(ctr)] # ============================================ # draw on the frame #cv2.drawContours(frame, heptagons, -1, (0,255,0), 3) cv2.drawContours(frame, arrows, -1, (255, 0, 0), -1) # draw tips #for t in tips: # cv2.circle(frame, tuple(t), 5, (0, 0, 255), -1) return frame if __name__ == "__main__": webcam_gui(imgproc, 1)
frame_gray = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY) #thresh = threshold(frame_gray,110) thresh = adap_threshold(frame_gray) frame_blur = cv2.blur(thresh, (3, 3)) #cv2.imshow('Threhold',thresh) contours, hry = cv2.findContours(frame_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #identify squares squares = [] for cnt in contours: # Calculate the perimeter cnt_len = cv2.arcLength(cnt, True) # Simpler Contour approximation cnt = cv2.approxPolyDP(cnt, 0.01 * cnt_len, True) if cv2.contourArea(cnt) > 10 and len(cnt) == 4 and cv2.isContourConvex( cnt): cnt = cnt.reshape(-1, 2) # cnt is divided into two column max_cos = np.max([ angle_cos(cnt[i], cnt[(i + 1) % 4], cnt[(i + 2) % 4]) for i in xrange(4) ]) if max_cos < 0.1 and equal(cnt[0], cnt[1], cnt[2], 0.2): squares.append(cnt) cv2.drawContours(frame_out, squares, -1, (0, 255, 0), 2) return frame_out if __name__ == "__main__": webcam_gui(find_square)
def imgproc(frame): if __name__ == "__main__": webcam_gui()
return False def find_square(frame_in): frame_out = frame_in.copy() frame_gray = cv2.cvtColor(frame_in, cv2.COLOR_BGR2GRAY) # thresh = threshold(frame_gray,110) thresh = adap_threshold(frame_gray) frame_blur = cv2.blur(thresh, (3, 3)) # cv2.imshow('Threhold',thresh) contours, hry = cv2.findContours(frame_blur, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # identify squares squares = [] for cnt in contours: # Calculate the perimeter cnt_len = cv2.arcLength(cnt, True) # Simpler Contour approximation cnt = cv2.approxPolyDP(cnt, 0.01 * cnt_len, True) if cv2.contourArea(cnt) > 10 and len(cnt) == 4 and cv2.isContourConvex(cnt): cnt = cnt.reshape(-1, 2) # cnt is divided into two column max_cos = np.max([angle_cos(cnt[i], cnt[(i + 1) % 4], cnt[(i + 2) % 4]) for i in xrange(4)]) if max_cos < 0.1 and equal(cnt[0], cnt[1], cnt[2], 0.2): squares.append(cnt) cv2.drawContours(frame_out, squares, -1, (0, 255, 0), 2) return frame_out if __name__ == "__main__": webcam_gui(find_square)