lb8820265 发表于 2020-9-13 20:08

【树莓派4B测评】树莓派4摄像头Opencv与picamera测试

<div class='showpostmsg'> 本帖最后由 lb8820265 于 2020-9-13 20:07 编辑

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">树莓派4支持摄像头,接口叫做SCI(Camera Serial Interface),树莓派有摄像头配件,使用的索尼IMX219芯片,价格昂贵,某宝上有OV5647芯片的摄像头,同样可用,价格感人。</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">用树莓派玩摄像头绝不单单是将他当作一个聊天摄像头,一定是用来监控,或者图像识别啥的。下面来使用Opencv与picamer两种方法调用摄像头进行基础操作。</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><b style="font-size: 16px; font-family: Arial;"><span class="17">使能摄像头</span></b></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">方法一:</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">开始--&gt;首选项--&gt;Raspberry Pi Configure--&gt;Interfaces,使能Camera,然后重启。</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">方法二:</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">打开终端输入:<span style="letter-spacing:0.0000pt"><span style="font-weight:normal"><span style="font-style:normal">sudo raspi-config--&gt;</span></span>选择<span style="font-weight:normal"><span style="font-style:normal">Interfacing options,</span></span></span>使能<span style="font-weight:normal"><span style="font-style:normal">Camera</span></span>,然后重启。</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><b style="font-size: 16px; font-family: Arial;"><span class="17"><span style="letter-spacing:0.0000pt">摄像头测试</span></span></b></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;"><span style="letter-spacing:0.0000pt">打开终端输入:</span></span></span></p>

<pre>
<code>raspistill -o new.jpg</code></pre>

<p style="text-align:left"><span style="font-family:Arial;"><span style="font-size:16px;"><span style="letter-spacing:0.0000pt">功能为预览<span style="font-weight:normal"><span style="font-style:normal">5</span></span>秒,然后拍摄一张图片,名称为<span style="font-weight:normal"><span style="font-style:normal">new.jpg</span></span>,保存地址为根目录。</span></span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><b style="font-size: 16px; font-family: Arial;"><span class="17"><span style="letter-spacing:0.0000pt">picamera库的使用</span></span></b></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;"><span style="letter-spacing:0.0000pt">用树莓派玩摄像头绝不单单是将他当作一个聊天摄像头,一定是用来监控,或者图像识别啥的。用代码来操作才是正道。</span></span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;"><span style="letter-spacing:0.0000pt">树莓派自带了Python和picamera库,可以用来操作摄像头。picamera官网文档:</span></span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;"><a href="https://picamera.readthedocs.io/en/release-1.13/index.html"><u><span class="15"><span style="color:#000080"><span style="letter-spacing:0.0000pt"><span style="font-weight:normal"><span style="text-decoration:underline"><span style="font-style:normal">https://picamera.readthedocs.io/en/release-1.13/index.html</span></span></span></span></span></span></u></a></span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">Raspberry自带了Thonny,开始&rarr;编程&rarr;Thonny Phthon IDE,这个IDE十分轻便,用起来也不错。输入如下代码:</span></span></p>

<pre>
<code class="language-python">from time import sleepfrom picamera import PiCamera
camera = PiCamera()#获取摄像头对象
camera.start_preview()#开启预览
sleep(5)#休眠5秒,这期间在预览摄像头
camera.capture('foo.jpg')#保存图片
camera.close()#关闭摄像头,否则只有关机了</code></pre>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">效果就是预览摄像头5秒,然后拍摄一张图片并保存</span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;"><b><span class="17"><span style="font-weight:bold">Opencv库的使用</span></span></b></span></span></p>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">Opencv鼎鼎大名,是图像处理玩家的必备库,先下载库,终端中输入如下代码:</span></span></p>

<pre>
<code>sudo apt-get install -y libopencv-dev python3-opencv</code></pre>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">在Thonny中输入如下:</span></span></p>

<pre>
<code class="language-python">import cv2
cap=cv2.VideoCapture(0) #调用摄像头‘0'一般是打开电脑自带摄像头,‘1'是打开外部摄像头while True:
  ret,frame=cap.read()#读取图像(frame就是读取的视频帧,对frame处理就是对整个视频的处理)
  cv2.imshow("frame",frame)  
  input=cv2.waitKey(20)
  if input==ord('q'):#如过输入的是q就break,结束图像显示,鼠标点击视频画面输入字符
    break
  cv2.imwrite('img.jpg',frame)
cap.release()#释放摄像头
cv2.destroyAllWindows()#销毁窗口</code></pre>

<p class="p" style="text-align:left; margin-top:7px; margin-bottom:7px"><span style="font-family:Arial;"><span style="font-size:16px;">效果就是预览摄像头,然后按下键盘Q键拍摄一张照片并退出。</span></span></p>

<p><span style="font-family:Arial;"><span style="font-size:16px;"></span></span></p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

okhxyyo 发表于 2020-9-14 11:34

<p><a href="https://bbs.eeworld.com.cn/thread-1137107-1-1.html" target="_blank">树莓派4B测评</a></p>

<p>汇总:<a href="https://bbs.eeworld.com.cn/thread-1137107-1-1.html">https://bbs.eeworld.com.cn/thread-1137107-1-1.html</a></p>

tinnu 发表于 2020-9-13 23:41

<p>有没有兴趣跑一跑基本的opencv算法,像blur、canny之类的,看看提升有多大?我之前给3B跑过&nbsp;</p>

<p>我迟点用我的NANOPC-T4看看跟树莓派4B孰优孰劣</p>

宋元浩 发表于 2020-9-14 09:40

<p>看国外评测,树莓派4的性能好像不比jeston nano差</p>

freebsder 发表于 2020-9-15 16:08

宋元浩 发表于 2020-9-14 09:40
看国外评测,树莓派4的性能好像不比jeston nano差

<p>国外评测的肯定是傻逼。jn 好歹有GPU,都怼CPU测试当然差别没那么明显。</p>
页: [1]
查看完整版本: 【树莓派4B测评】树莓派4摄像头Opencv与picamera测试