import cv2
import os
output_dir ='/home/pi/Desktop/py'
i = 1
cap = cv2.VideoCapture(0)
while 1:
ret, frame = cap.read()
cv2.imshow('cap', frame)
flag = cv2.waitKey(1)
if flag == 13:#按下回车键
output_path = os.path.join(output_dir, "%04d.jpg" % i)
cv2.imwrite(output_path, frame)
i += 1
if flag == 27:#按下ESC键
break
import cv2
import numpy as np
def nothing(x):
pass
cv2.namedWindow("Tracking")
#参数:1 Lower/Upper HSV 3 startValue 4 endValue
cv2.createTrackbar("LH","Tracking",35,255,nothing)
cv2.createTrackbar("LS","Tracking",43,255,nothing)
cv2.createTrackbar("LV","Tracking",46,255,nothing)
cv2.createTrackbar("UH","Tracking",77,255,nothing)
cv2.createTrackbar("US","Tracking",255,255,nothing)
cv2.createTrackbar("UV","Tracking",255,255,nothing)
while True:
frame = cv2.imread('/home/pi/Desktop/py/0002.jpg')#这里是你照片存放的路径
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
l_h = cv2.getTrackbarPos("LH","Tracking")
l_s = cv2.getTrackbarPos("LS","Tracking")
l_v = cv2.getTrackbarPos("LV","Tracking")
u_h = cv2.getTrackbarPos("UH","Tracking")
u_s = cv2.getTrackbarPos("US","Tracking")
u_v = cv2.getTrackbarPos("UV","Tracking")
l_g = np.array([l_h, l_s, l_v]) # lower green value
u_g = np.array([u_h,u_s,u_v])
mask = cv2.inRange(hsv,l_g,u_g)
res=cv2.bitwise_and(frame,frame,mask=mask) # src1,src2
cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
cv2.imshow("res", res)
key = cv2.waitKey(1)
if key == 27: # Esc
break
cv2.destroyAllWindows()
import cv2
import numpy as np
import imutils
from imutils import contours
#import RPi.GPIO as GPIO
#PIN5=5#引脚选用,选用GPIO5
#PIN6=6#选用GPIO6
#auto=0
#straight_line_speed=9 #电机最大的旋转速度,百分比值为#5.8。不转时的百分比值为10.5
#circle_speed=9
#servo_straight=8.2
#GPIO.setmode(GPIO.BCM)
#GPIO.setup(PIN5,GPIO.OUT)
#GPIO.setup(PIN6,GPIO.OUT)
#pwm_servo=GPIO.PWM(PIN5,58.8)#pin5用于舵机
#pwm_motor=GPIO.PWM(PIN6,58.8)#pin6用于电机
#pwm_servo.start(0)#初始占空比
#pwm_motor.start(0)
# 颜色阈值
lower = np.array([158,139,49])
upper = np.array([255,255,212])
# 内核
kernel = np.ones((5, 5), np.uint8)
# 打开摄像头
vc = cv2.VideoCapture(0)
if vc.isOpened():
flag, frame = vc.read()
# 翻转图像
# 这一步可以忽略,博主的摄像头是反着的
# 所以加上这句话可以让摄像头的图像正过来
#frame = imutils.rotate(frame, 180)
cv2.imshow("frame", frame)
else:
flag = False
''''def servo(a):
if auto:
pwm_servo.ChangeDutyCycle(a)
else:
GPIO.setup(PIN5,GPIO.IN,pull_up_down=GPIO.PUD_UP)#设置引脚5为输入引脚,并开启上拉电阻
def motor(a):
if auto:
pwm_motor.ChangeDutyCycle(a)
else:
GPIO.setup(PIN6,GPIO.IN,pull_up_down=GPIO.PUD_UP)#设置引脚6为输入引脚,并开启上拉电阻
''''
def find_color():
flag, frame = vc.read()
# 翻转图像
#frame = imutils.rotate(frame, 180)
draw_frame = frame.copy()
#if frame is None:
# break
if flag is True:
'''下面对摄像头读取到的图像进行处理,这个步骤是比较重要的'''
# 转换颜色空间HSV
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# 颜色识别
img = cv2.inRange(frame_hsv, lower, upper)
# 膨胀操作
dilation = cv2.dilate(img, kernel, iterations=2)
# 闭操作
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
# 高斯滤波
closing = cv2.GaussianBlur(closing, (5, 5), 0)
# 边缘检测
edges = cv2.Canny(closing, 10, 20)
'''上面进行那么多操作就是为了得到更好的目标图形,具体效果因环境而异'''
# 寻找轮廓
cnts, _ = cv2.findContours(
edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# 判断轮廓数量也就是判断是否寻找到轮廓,如果没有找到轮廓就不继续进行操作
if len(cnts) > 0:
# 存放轮廓面积的列表
s = []
# 存放最大轮廓的索引
max_index = 0
# 获得排序后的轮廓列表以及每个轮廓对应的外接矩形
(cnts, boundingRects) = contours.sort_contours(cnts)
# 寻找面积最大的轮廓的索引
for cnt in cnts:
s.append(cv2.contourArea(cnt))
max_index = s.index(max(s))
# 根据面积最大轮廓的索引找到它的外接矩形的信息
(x, y, w, h) = boundingRects[max_index]
# 画矩形
frame_out = cv2.rectangle(
draw_frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
'''' if 140=300:
#print("left")
servo(servo_straight+1.5)
motor(circle_speed)
else:
#print("find")
servo(servo_straight-1.5)
motor(circle_speed)
''''
cv2.imshow("frame", draw_frame)
while(True):
find_color()
if cv2.waitKey(10) == 27:
break
vc.release()
cv2.destroyAllWindows()
GPIO.cleanup()#清空引脚定义,设置为输入状态,防止接短路