import cv2 import numpy as np import math def draw_mark_X(img, x, y, width=6, color=(255,255,0), penWid=2): cv2.line(img, (int(x - width), int(y - width)), (int(x + width), int(y + width)), color, penWid) cv2.line(img, (int(x - width), int(y + width)), (int(x + width), int(y - width)), color, penWid) # 使用测试视频 video_path = 0 # video_path = 0 # 使用本机摄像头 cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) while cap.isOpened(): ret, frame = cap.read() if ret == False: break # 设置HSV跟踪颜色范围(红色、绿色、蓝色) min_red_hsv = np.array([0, 100, 100]) max_red_hsv = np.array([10, 255, 255]) min_green_hsv = np.array([40, 50, 50]) max_green_hsv = np.array([90, 255, 255]) min_blue_hsv = np.array([90, 50, 50]) max_blue_hsv = np.array([130, 255, 255]) #将输入图像有bgr格式转化为hsv格式 img_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 提取颜色范围内图像区域 red_mask = cv2.inRange(img_hsv, min_red_hsv, max_red_hsv) green_mask = cv2.inRange(img_hsv, min_green_hsv, max_green_hsv) blue_mask = cv2.inRange(img_hsv, min_blue_hsv, max_blue_hsv) img_red = cv2.bitwise_and(frame, frame, mask=red_mask) img_green = cv2.bitwise_and(frame, frame, mask=green_mask) img_blue = cv2.bitwise_and(frame, frame, mask=blue_mask) # 二值化 kernel = np.ones((6, 6), np.uint8) img_kai_red = cv2.morphologyEx(img_red, cv2.MORPH_OPEN, kernel) img_kai_green = cv2.morphologyEx(img_green, cv2.MORPH_OPEN, kernel) img_kai_blue = cv2.morphologyEx(img_blue, cv2.MORPH_OPEN, kernel) img_kai_red_chv = img_kai_red[:, :, 2] img_kai_green_chv = img_kai_green[:, :, 2] img_kai_blue_chv = img_kai_blue[:, :, 2] thr_red, img_bin_red = cv2.threshold(img_kai_red_chv, 63, 255, cv2.THRESH_BINARY) thr_green, img_bin_green = cv2.threshold(img_kai_green_chv, 63, 255, cv2.THRESH_BINARY) thr_blue, img_bin_blue = cv2.threshold(img_kai_blue_chv, 63, 255, cv2.THRESH_BINARY) print('thr_red:', thr_red, 'thr_green:', thr_green, 'thr_blue:', thr_blue) # 提取轮廓 contours_red, hierarchy_red = cv2.findContours(img_bin_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours_green, hierarchy_green = cv2.findContours(img_bin_green, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours_blue, hierarchy_blue = cv2.findContours(img_bin_blue, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(contours_red) > 0: # There should be at least 5 points to fit the ellipse if len(contours_red[0]) > 8: # print('len(contours_red):', len(contours_red)) # print('Points', len(contours_red[0])) # print('contours_red:', contours_red) # print('hierarchy_red', hierarchy_red) # 画红色轮廓最小外接圆以及圆心X (x, y), radius = cv2.minEnclosingCircle(contours_red[0]) center = (int(x), int(y)) radius = int(radius) cv2.circle(frame, center, radius, (0, 0, 255), 1) draw_mark_X(frame, int(x), int(y)) # 计算最小外接圆面积 area = math.pi * radius * radius # 显示圆心X Y 坐标,半径 R, 和 圆面积 font = cv2.FONT_HERSHEY_SIMPLEX text_info = 'Red X:' + str(x * 10 // 1 / 10) + ' Y:' + str(y * 10 // 1 / 10) cv2.putText(frame, text_info, (10, 30), font, 1, (0, 0, 255), 1) text_info = 'R:' + str(radius * 10 // 1 / 10) + " Area:" + str(area * 100 // 1 / 100) cv2.putText(frame, text_info, (10, 60), font, 1, (0, 0, 255), 1) if len(contours_green) > 0: # There should be at least 5 points to fit the ellipse if len(contours_green[0]) > 8: # print('len(contours_green):', len(contours_green)) # print('Points', len(contours_green[0])) # print('contours_green:', contours_green) # print('hierarchy_green', hierarchy_green) # 画绿色轮廓最小外接圆以及圆心X (x, y), radius = cv2.minEnclosingCircle(contours_green[0]) center = (int(x), int(y)) radius = int(radius) cv2.circle(frame, center, radius, (0, 255, 0), 1) draw_mark_X(frame, int(x), int(y)) # 计算最小外接圆面积 area = math.pi * radius * radius # 显示圆心X Y 坐标,半径 R, 和 圆面积 font = cv2.FONT_HERSHEY_SIMPLEX text_info = 'Green X:' + str(x * 10 // 1 / 10) + ' Y:' + str(y * 10 // 1 / 10) cv2.putText(frame, text_info, (10, 90), font, 1, (0, 255, 0), 1) text_info = 'R:' + str(radius * 10 // 1 / 10) + " Area:" + str(area * 100 // 1 / 100) cv2.putText(frame, text_info, (10, 120), font, 1, (0, 255, 0), 1) if len(contours_blue) > 0: # There should be at least 5 points to fit the ellipse if len(contours_blue[0]) > 8: # print('len(contours_blue):', len(contours_blue)) # print('Points', len(contours_blue[0])) # print('contours_blue:', contours_blue) # print('hierarchy_blue', hierarchy_blue) # 画蓝色轮廓最小外接圆以及圆心X (x, y), radius = cv2.minEnclosingCircle(contours_blue[0]) center = (int(x), int(y)) radius = int(radius) cv2.circle(frame, center, radius, (255, 0, 0), 1) draw_mark_X(frame, int(x), int(y)) # 计算最小外接圆面积 area = math.pi * radius * radius # 显示圆心X Y 坐标,半径 R, 和 圆面积 font = cv2.FONT_HERSHEY_SIMPLEX text_info = 'Blue X:' + str(x * 10 // 1 / 10) + ' Y:' + str(y * 10 // 1 / 10) cv2.putText(frame, text_info, (10, 150), font, 1, (255, 0, 0), 1) text_info = 'R:' + str(radius * 10 // 1 / 10) + " Area:" + str(area * 100 // 1 / 100) cv2.putText(frame, text_info, (10, 180), font, 1, (255, 0, 0), 1) # 显示图像帧 cv2.imshow('ColorTrace', frame) c = cv2.waitKey(int(1000 / fps)) # c = cv2.waitKey(200) # 按ESC键退出 if c == 27: # ESC break cap.release() cv2.destroyAllWindows() print('Exit video read.')
honglvlanwuliaoshibie
于 2024-01-18 14:07:45 首次发布