赞
踩
from collections import deque import numpy as np import cv2 import time redLower = np.array([0, 80, 50]) #设定红色阈值,HSV空间 redUpper = np.array([8, 255, 220]) mybuffer = 64 #初始化追踪点的列表 pts = deque(maxlen=mybuffer) #打开摄像头 if __name__ == '__main__': cv2.namedWindow("camera",1) video="http://192.168....:8081/" #自己IP摄像头的地址 camera =cv2.VideoCapture(video) time.sleep(3)#等待两秒 while True: #遍历每一帧,检测红色瓶盖 #读取帧 (ret, frame) = camera.read() if not ret: print ('No Camera') break hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #转到HSV空间 mask = cv2.inRange(hsv, redLower, redUpper) #根据阈值构建掩膜 mask = cv2.erode(mask, None, iterations=2) #腐蚀操作 mask = cv2.dilate(mask, None, iterations=2) #膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点 cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #轮廓检测 center = None #初始化瓶盖圆形轮廓质心 #如果存在轮廓 if len(cnts) > 0: #找到面积最大的轮廓 c = max(cnts, key = cv2.contourArea) #确定面积最大的轮廓的外接圆 ((center_x, center_y), radius) = cv2.minEnclosingCircle(c) #计算轮廓的矩 M = cv2.moments(c) #计算质心 cv2.circle(frame, (int(center_x), int(center_y)), int(radius), (0, 255, 255), 2) cv2.circle(frame, center, 5, (0, 0, 255), -1) print('红色色块的中心坐标',(int(center_x),int(center_y))) cv2.imshow('Frame', frame) k = cv2.waitKey(5)&0xFF #键盘检测,检测到esc键退出 if k == 27: break camera.release() #摄像头释放 cv2.destroyAllWindows()#销毁所有窗口
对于IP摄像头陌生的同学可以看一下我之前的一篇博客https://blog.csdn.net/weixin_45870610/article/details/104970299
from collections import deque import numpy as np import cv2 import time redLower = np.array([156, 43, 46]) #设定红色阈值,HSV空间 redUpper = np.array([180, 255, 255]) blueLower = np.array([100, 43, 46]) blueUpper = np.array([124, 255, 255]) mybuffer = 64 #初始化追踪点的列表 pts = deque(maxlen=mybuffer) if __name__ == '__main__': cv2.namedWindow("camera",1) video="http://192.168....:8081/" #这里要改成你自己的IP摄像头的地址 camera =cv2.VideoCapture(video) time.sleep(3)#等待两秒 while True: #遍历每一帧,检测红色瓶盖 #读取帧 (ret, frame) = camera.read() if not ret: print ('No Camera') break hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #转到HSV空间 mask1 = cv2.inRange(hsv, redLower, redUpper) #根据阈值构建掩膜 mask2 = cv2.inRange(hsv, blueLower, blueUpper) mask1 = cv2.erode(mask1, None, iterations=2) #腐蚀操作 mask2 = cv2.erode(mask2, None, iterations=2) mask1 = cv2.dilate(mask1, None, iterations=2)#膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点 mask2 = cv2.dilate(mask2, None, iterations=2) cnts1 = cv2.findContours(mask1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #轮廓检测 cnts2 = cv2.findContours(mask2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None #初始化瓶盖圆形轮廓质心 #如果存在轮廓 if len(cnts1) > 0: c = max(cnts1, key = cv2.contourArea) #找到面积最大的轮廓 ((red_x, red_y), radius) = cv2.minEnclosingCircle(c) #确定面积最大的轮廓的外接圆 M = cv2.moments(c) #计算轮廓的矩 cv2.circle(frame, (int(red_x), int(red_y)), int(radius), (0, 255, 255), 2) #计算质心 cv2.circle(frame, center, 5, (0, 0, 255), -1) print('红:',(int(red_x),int(red_y))) if len(cnts2) > 0: c = max(cnts2, key = cv2.contourArea) ((blue_x, blue_y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) cv2.circle(frame, (int(blue_x), int(blue_y)), int(radius), (0, 0, 255), 2) cv2.circle(frame, center, 5, (255, 0, 0), -1) print('蓝:',(int(blue_x),int(blue_y))) cv2.imshow('Frame', frame) k = cv2.waitKey(5)&0xFF #键盘检测,检测到esc键退出 if k == 27: break camera.release() #摄像头释放 cv2.destroyAllWindows()#销毁所有窗口 ''' 三种色块的:可以把这些加进去,道理是相同的 greenLower = np.array([35, 43, 46]) greenUpper = np.array([77, 255, 255]) mask3 = cv2.inRange(hsv, greenLower, greenUpper) mask3 = cv2.erode(mask3, None, iterations=2) mask3 = cv2.dilate(mask3, None, iterations=2) cnts3 = cv2.findContours(mask3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] if len(cnts3) > 0: c = max(cnts3, key = cv2.contourArea) ((green_x, green_y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) cv2.circle(frame, (int(green_x), int(green_y)), int(radius), (255, 0, 0), 2) cv2.circle(frame, center, 5, (0, 255, 0), -1) print('绿:',(int(green_x),int(green_y))) '''
这里大家要尽量找圆形的物体,家里实在找不到圆绿的了哈哈,不规则的物体的话得出的数据还是会有误差的;还有一种情况是没有办法识别出红色,但你觉得他就是红色的物体,那你可以适当调整一下HSV值,就没问题了
哈哈,一看就是初级菜鸟写的代码,希望对大家有所帮助,因为我最终的目标不是识别色块,所以还要进行更进一步的学习,所以这个博客之后还会更新,尽请期待吧。
有什么需不对的,或者需要改进的地方,还希望各位大神可以指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。