设置摄像头参数(picameratest.py)

转自博客:https://www.cnblogs.com/wangshuyi/p/9138951.html
首先安装CSI摄像头的驱动:
sudo pip install picamera
代码见picamera.py,全部内容如下:
在这里插入图片描述

import the necessary packages

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

initialize the camera and grab a reference to the raw camera capture

camera = PiCamera()
camera.saturation = 80 # 设置图像视频的饱和度
camera.brightness = 50 # 设置图像的亮度(50表示白平衡的状态)
camera.shutter_speed = 100000 # 相机快门速度
camera.iso = 400 # ISO标准实际上就是来自胶片工业的标准称谓,ISO是衡量胶片对光线敏感程度的标准。如50 ISO, 64 ISO, 100 ISO表示在曝光感应速度上要比高数值的来得慢,高数值ISO是指超过200以上的标准,如200 ISO, 400 ISO
camera.framerate = 32 #这里可能用的Fraction是一个分数模块来存储分数1/6,保证分数运算的精度(记得调用模块:from fractions import Fraction)
camera.hflip = Ture # 是否进行水平翻转
camera.vflip = False #是否进行垂直翻转
camera.rotation = 0 #是否对图像进行旋转
#camera.resolution = (280,160) #设置图像的width和height
camera.resolution = (640, 480)
camera.framerate = 32
camera.hflip = True
camera.vflip = True
rawCapture = PiRGBArray(camera, size=(640, 480))

allow the camera to warmup

time.sleep(0.1)

capture frames from the camera

for frame in camera.capture_continuous(rawCapture, format=“bgr”, use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
# show the frame
cv2.imshow(“Frame”, image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the q key was pressed, break from the loop
if key == ord(“q”):
break

picamera包提供了一个python接口来操作树莓派摄像头。
重点关注API - The PiCamera Class,这个API中有各个操作树莓派摄像头的函数以及可设置的拍照参数。以下为各个参数及解释。
属性 解释 备注
analog_gain 模拟增益
annotate_foreground 注释文字的颜色
annotate_background 注释文字背景颜色
annotate_frame_num 是否将帧编号写为注释 默认为False
annotate_text 检索/设置文本注释 默认为""
annotate_text_size 注释的文字大小 默认为32
awb_gains 白平衡增益 awb_mode为off时才起作用
awb_mode 白平衡模式 默认为自动
brightness 亮度 默认为50
clock_mode 检索或设置相机时钟的模式 默认为reset
closed close()函数被调用的时候返回True
color_effects 检索或设置摄像机应用的当前颜色效果,如黑白模式
contrast 对比度 默认为0
crop 检索或设置应用于相机输入的缩放
digital_gain 检索相机的当前数字增益。
drc_strength 检索或设置摄像机的动态范围压缩强度 默认为off
exif_tags Exif标记
exposure_compensation 检索或设置摄像机的曝光补偿级别 默认为0
exposure_mode 曝光模式 默认为自动
exposure_speed 检索相机的当前快门速度 只读属性
flash_mode 闪光模式 默认为off
frame 检索有关从相机记录的当前帧的信息
framerate 检索或设置帧速率 默认值为30
framerate_delta 帧速率微调 默认为0
framerate_range 检索或设置允许相机的帧速率浮动的范围
hflip 检索或设置相机的输出是否水平翻转 默认为False
image_denoise 检索或设置是否将降噪算法应用于图像捕获 默认为True
image_effect 检索或设置摄像机应用的当前图像效果 默认值none
image_effect_params 检索或设置当前效果的参数
iso 检索或设置相机的ISO(感光度) 默认值为0,表自动
led 通过GPIO设置摄像机LED的状态 目前,无法在Pi 3上控制相机的LED
meter_mode 检索或设置摄像机的测光模式 默认值为’average’
overlays 检测所有叠层
preview 检测显示相机预览
preview_alpha 检索或设置预览窗口的不透明度
preview_fullscreen 检索或设置预览窗口的全屏
preview_layer 检索或设置预览窗口的图层
preview_window 检索或设置预览窗口的大小
previewing 如果start_preview()方法被调用则返回True
raw_format 检索或设置摄像机端口的原始格式
recording 如果start_recording()方法被调用则返回True
resolution 检索或设置捕获图像捕获,视频录制和预览的分辨率 默认为1280x720
revision 返回表示Pi相机模块修订版的字符串
rotation 检索或设置摄像机图像的当前旋转 默认为0
saturation 检索或设置摄像机的饱和度 默认为0
sensor_mode 检索或设置摄像机传感器的输入模式 默认为0
sharpness 检索或设置摄像机的清晰度设置 默认为0
shutter_speed 以微秒为单位检索或设置相机的快门速度 默认为0,自动
still_stats 检索或设置是否从静止帧或先前预览帧计算统计数据 默认为False
timestamp 件检索系统时间。
vflip 检索或设置相机的输出是否垂直翻转 默认为False
video_denoise 检索或设置是否将降噪算法应用于视频录制 默认为True
video_stabilization 检索或设置摄像机的视频稳定模式 默认为False
zoom 检索或设置相机输入的缩放

作者:引力.
来源:CSDN
原文:https://blog.csdn.net/m0_37509650/article/details/80282646
版权声明:本文为博主原创文章,转载请附上博文链接!

摄像头预览
2.1 打开python3,建立名为camera.py的新文件,注意,千万不能命名为picamera.py
2.2 在文件中写入一下代码,运行即可,注意:远程连接(例如:SSH和VNC)时不允许访问摄像头
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview()
sleep(10)
camera.stop_preview()
通过改变alpha的值,可以修改摄像头拍摄图像的透明度alpha的取值范围为0-255
from picamera import PiCamera
from time import sleep
camera = PiCamera()
camera.start_preview(alpha=200)
sleep(10)
camera.stop_preview()

拍照片
3.1 修改代码:减少sleep并添加camera.capture()一行
camera.start_preview()
sleep(5)
camera.capture(’/home/pi/Desktop/image.jpg’)
camera.stop_preview()
捕获图片前,至少要给传感器两秒钟时间感光
3.2 增加一个循环就能实现连拍,每隔5秒拍一张,拍完后预览关闭,桌面上就有五张图了
camera.start_preview()
for i in range(5):
sleep(5)
camera.capture(’/home/pi/Desktop/image%s.jpg’ % i)
camera.stop_preview()

拍视屏
4.1 修改代码:用start_recording()和stop_recording()代替capture()
camera.start_preview()
camera.start_recording(’/home/pi/video.h264’)
sleep(10)
camera.stop_recording()
camera.stop_preview()
运行程序,将拍摄10秒的视屏,然后关闭预览
播放视屏需要执行如下指令:
omxplayer video.h264
播放时可能会出现播放速度比实际速度快的情况,这是omxplayer的快速帧速率导致的
进阶教程
摄像头的分辨率可以自己设定,但要记住,照片的最大分辨率时2592x1944,视频的最大分辨率为1920x1080,可以通过一下代码设定分辨率,为了配合最大分辨率,需要设置帧速率为15
camera.resolution = (2592, 1944)
camera.framerate = 15
camera.start_preview()
sleep(5)
camera.capture(’/home/pi/Desktop/max.jpg’)
camera.stop_preview()
最小分辨率允许设为64x64
用annotate_text很方便地为图像添加文字
camera.start_preview()
camera.annotate_text = “Hello world!”
sleep(5)
camera.capture(’/home/pi/Desktop/text.jpg’)
camera.stop_preview()
改变亮度设置,范围是0-100,预设为50
camera.start_preview()
camera.brightness = 70
sleep(5)
camera.capture(’/home/pi/Desktop/bright.jpg’)
camera.stop_preview()
在循环中调整亮度,并标注亮度等级
camera.start_preview()
for i in range(100):
camera.annotate_text = “Brightness: %s” % i
camera.brightness = i
sleep(0.1)
camera.stop_preview()
调节文字大小,6-160,预设为32
camera.annotate_text_size = 50
改变文字的颜色,首先要引入Color,然后就能用如下代码实现
from picamera import PiCamera, Color

camera.start_preview()
camera.annotate_background = Color(‘blue’)
camera.annotate_foreground = Color(‘yellow’)
camera.annotate_text = " Hello world "
sleep(5)
camera.stop_preview()
camera.image_effect为图片添加特殊效果,可选择的参数有:none,negative,solarize,sketch,denoise,emboss,oilpaint,hatch,gpen,pastel,watercolor,film,blur,saturation,colorswap,washedout,posterise,colorpoint,colorbalance,cartoon,deinterlace1和deinterlace2。预设为none。
用循环显示不同效果的预览
camera.start_preview()
for effect in camera.IMAGE_EFFECTS:
camera.image_effect = effect
camera.annotate_text = “Effect: %s” % effect
sleep(5)
camera.stop_preview()
camera.awb_mode可以设置白平衡,可选参数有:off, auto, sunlight, cloudy, shade, tungsten, fluorescent, incandescent, flash和horizon,预设为auto
camera.start_preview()
camera.awb_mode = ‘sunlight’
sleep(5)
camera.capture(’/home/pi/Desktop/sunlight.jpg’)
camera.stop_preview()

作者:小坏wz
来源:CSDN
原文:https://blog.csdn.net/qq_32384313/article/details/89297424
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值