<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>摄像头 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%91%84%E5%83%8F%E5%A4%B4/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Sun, 03 May 2020 12:29:29 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]在Raspberry Pi(树莓派)上用OpenCV来操纵摄像头拍照/Use OpenCV on Raspberry Pi to Controll a Webcam to Take Photos</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9c%a8raspberry-pi%e4%b8%8a%e7%94%a8opencv%e6%9d%a5%e6%93%8d%e7%ba%b5%e6%91%84%e5%83%8f%e5%a4%b4%e6%8b%8d%e7%85%a7use-opencv-on-raspberry-pi-to-controll-a-webcam-to-take-photos/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9c%a8raspberry-pi%e4%b8%8a%e7%94%a8opencv%e6%9d%a5%e6%93%8d%e7%ba%b5%e6%91%84%e5%83%8f%e5%a4%b4%e6%8b%8d%e7%85%a7use-opencv-on-raspberry-pi-to-controll-a-webcam-to-take-photos/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 18 Oct 2012 07:46:00 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Arch Linux ARM]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[OpenCV]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[webcam]]></category>
		<category><![CDATA[拍照]]></category>
		<category><![CDATA[摄像头]]></category>
		<category><![CDATA[教程]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=5014</guid>

					<description><![CDATA[<p>
<span style="background-color:#00ff00;">For non-Chinese readers:</span><br />
<span style="color:#0000ff;">I&#39;m a software engineer in Shanghai, China. Since I bought a Raspberry Pi, I spent some spare time on it and I&#39;m glad to share something with Pi users all over the world, so this article has two languages: Chinese &#38; English, and I try to translate the Chinese to English as&#160;accurately as possible, but, there might be some translation mistakes, so if you don&#39;t know what I mean in the article, just leave a comment, and I&#39;ll reply when I get an answer.</span>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9c%a8raspberry-pi%e4%b8%8a%e7%94%a8opencv%e6%9d%a5%e6%93%8d%e7%ba%b5%e6%91%84%e5%83%8f%e5%a4%b4%e6%8b%8d%e7%85%a7use-opencv-on-raspberry-pi-to-controll-a-webcam-to-take-photos/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<span style="background-color:#00ff00;">For non-Chinese readers:</span><br />
<span style="color:#0000ff;">I&#39;m a software engineer in Shanghai, China. Since I bought a Raspberry Pi, I spent some spare time on it and I&#39;m glad to share something with Pi users all over the world, so this article has two languages: Chinese &amp; English, and I try to translate the Chinese to English as&nbsp;accurately as possible, but, there might be some translation mistakes, so if you don&#39;t know what I mean in the article, just leave a comment, and I&#39;ll reply when I get an answer. Also welcome to contact me via <a href="http://www.codelast.com/?page_id=2" target="_blank" rel="noopener noreferrer">Email</a>.</span></p>
<p><a href="http://www.raspberrypi.org/" target="_blank" rel="noopener noreferrer">Raspberry Pi</a>是什么？简单地说，它就是一个基于ARM CPU的、信用卡那么大的迷你计算机。<br />
<span style="color:#0000ff;">In short, Pi is an ARM-based mini computer which has a credit card size.</span></p>
<p>本文主要内容：<br />
在Raspberry Pi上，通过调用OpenCV库，自己写一个简单的C程序来控制摄像头拍照，并保存图片。<br />
在阅读本文之前，请确保你已经阅读过我的另一篇<a href="http://www.codelast.com/?p=4945" target="_blank" rel="noopener noreferrer">配置Pi的文章</a>，因为本文是在它的基础上进行的。<br />
<span style="color:#0000ff;">The main content of this article:<br />
Write a simple C program by invoking the OpenCV library on Raspberry Pi to control a webcam to take photos &amp; save them.<br />
Before reading this article, please make sure that you&#39;ve read my another article about <a href="http://www.codelast.com/?p=4945" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">Pi configuration</span></a>, because this article is based on that.</span><br />
<span id="more-5014"></span></p>
<div style="text-align: center; ">
	<span style="background-color: rgb(0, 255, 0); "><img decoding="async" alt="my running Raspberry Pi" src="https://www.codelast.com/wp-content/uploads/2012/10/my_running_raspberry_pi.jpg" style="width: 400px; height: 538px; " /></span></div>
<div style="text-align: center; ">
	<span style="color:#a52a2a;">图：我的Pi正在运行中 / my running Raspberry Pi</span></div>
<p>
<span style="background-color: rgb(0, 255, 0); ">【1】</span>安装OpenCV <span style="color:#ff0000;">/</span> <span style="color:#0000ff;">Install OpenCV</span><br />
命令：<br />
<span style="color:#0000ff;">Command:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
pacman -S opencv opencv-samples
</pre>
<p>上面的命令连OpenCV的sample一起装上了，这样我们在安装好之后就可以编译几个sample来测试。<br />
<span style="color:#0000ff;">This command will install the samples of OpenCV, too. So we can compile &amp; run several samples to have a test after the installation.</span></p>
<p><span style="background-color: rgb(0, 255, 0); ">【2】</span>带动摄像头 <span style="color:#ff0000;">/ </span><span style="color:#0000ff;">Drive the webcam</span><br />
在驱动已经可用的情况下，把一个USB摄像头接在USB口上也不一定能让它工作，因为Pi的USB口的电流供应可能不足。请看<a href="http://eduardofv.com/read_post/189-DIY-Powered-USB-Cable-for-Raspberry-Pi" target="_blank" rel="noopener noreferrer">这个链接</a>的玩家自制的一根增强供电的USB线，如果你想简单安全，那么你可以把摄像头接在一个有外部供电的USB HUB上，再把Pi的USB接到USB HUB上，这样摄像头的供电就充足了。<br />
<span style="color:#0000ff;">Plug the USB webcam into the USB interface of Pi might not able to make it work, even the driver has been available, because the current provided by Pi&#39;s USB interface may not be&nbsp;sufficient to drive a camera. You can learn to DIY a current-enhancement USB cable via </span><a href="http://eduardofv.com/read_post/189-DIY-Powered-USB-Cable-for-Raspberry-Pi" target="_blank" rel="noopener noreferrer"><span style="color:#0000ff;"><span style="background-color:#ffa07a;">this link</span></span></a><span style="color:#0000ff;">, but the easier way may be buying a powered USB HUB and use it to connect the webcam &amp; Pi.</span></p>
<p><span style="background-color: rgb(0, 255, 0); ">【3】</span>让opencv的sample跑起来 <span style="color:#ff0000;">/</span> <span style="color:#0000ff;">Run a sample of OpenCV</span><br />
为了验证在Pi上安装的OpenCV是可用的，所以至少要让一个sample可以跑起来。<br />
为了不把安装目录弄得一团乱，首先把安装好的OpenCV的sample目录拷贝到其他地方：<br />
<span style="color:#0000ff;">To make sure that the installed OpenCV related packages are &quot;correct&quot;, I decided to run at least one of the OpenCV samples.<br />
Copy the installed OpenCV sample directory to other place for the sake of not messing up the installation directory:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
mkdir /root/opencv
cp -r /usr/share/OpenCV/samples/ /root/opencv/
</pre>
<p>我们看到里面有&ldquo;c&rdquo;，&ldquo;cpp&rdquo;和&ldquo;gpu&rdquo;三种sample，选最简单的，我们进入&ldquo;c&rdquo;，先为所有文件加上写权限：<br />
<span style="color:#0000ff;">There are 3 kinds of samples in it, &quot;c&quot;, &quot;cpp&quot; &amp; &quot;gpu&quot;, so I choose the&nbsp;simplest - &quot;c&quot;, and enter the &quot;c&quot; directory, add write&nbsp;permission to all files:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
chmod +w *
</pre>
<p>然后我们看到&ldquo;c&rdquo;目录里有编译脚本&nbsp;<span style="color:#b22222;">build_all.sh</span>，执行它，看到出了一堆相似的错误，没有一个程序可以成功编译，例如以下错误：<br />
<span style="color:#0000ff;">Execute the </span><span style="color:#b22222;">build_all.sh</span><span style="color:#0000ff;"> script to compile an sample, then I got lots of similar errors, e.g. :</span></p>
<blockquote>
<div>
		[root@alarmpi c]# ./build_all.sh contours.c&nbsp;</div>
<div>
		&nbsp;compiling contours</div>
<div>
		&nbsp;/usr/bin/ld: /tmp/cch4zRSn.o: undefined reference to symbol &#39;lrint@@GLIBC_2.4&#39;</div>
<div>
		&nbsp;/usr/bin/ld: note: &#39;lrint@@GLIBC_2.4&#39; is defined in DSO /usr/lib/libm.so.6 so try adding it to the linker command line</div>
<div>
		&nbsp;/usr/lib/libm.so.6: could not read symbols: Invalid operation</div>
<div>
		&nbsp;collect2: error: ld returned 1 exit status</div>
</blockquote>
<div>
	由 libm 可见，这是因为数学库没有被包含在编译命令中，于是我们编译&nbsp;<span style="color:#b22222;">build_all.sh</span>，为所有编译命令添加上&nbsp;<span style="color: rgb(255, 0, 0); ">-lm</span>&nbsp;编译参数：<br />
	<span style="color:#0000ff;">The &quot;libm&quot; told me that the math library was not included in the&nbsp;compiling command, so I edited&nbsp;</span><span style="color:#b22222;">build_all.sh</span><span style="color:#0000ff;">&nbsp;and add </span><span style="color:#b22222;">-lm</span><span style="color:#0000ff;"> to all compiling command:&nbsp;</span></div>
<div>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
#!/bin/sh                           

if [ $# -gt 0 ] ; then
        base=`basename $1 .c`
        echo &quot;compiling $base&quot;
        gcc -ggdb `pkg-config opencv --cflags --libs` $base.c -o $base -lm
else
        for i in *.c; do
            echo &quot;compiling $i&quot;
            gcc -ggdb `pkg-config --cflags opencv` -o `basename $i .c` $i `pkg-config --libs opencv` -lm;
        done
        for i in *.cpp; do
            echo &quot;compiling $i&quot;
            g++ -ggdb `pkg-config --cflags opencv` -o `basename $i .cpp` $i `pkg-config --libs opencv` -lm;
        done
fi
</pre>
</div>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
还忘了说一句，由于上面的脚本在编译时使用了pkg-config，所以在编译前，我们还需要先安装 pkg-config：<br />
<span style="color:#0000ff;">Sorry, I forget to mention that because of using pkg-config, we need to install pkg-config before compiling the sample:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
pacman -S pkg-config
</pre>
<p>然后再编译，成功。<br />
<span style="color:#0000ff;">Then the compiling will succeed.</span><br />
那么下面，我们再接着尝试把编译出的sample运行起来：<br />
<span style="color:#0000ff;">Then run the executable program we just compiled:</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
[root@alarmpi c]# ./contours 

This program creates an image to demonstrate the use of the &quot;c&quot; contour
functions: cvFindContours() and cvApproxPoly() along with the storage
functions cvCreateMemStorage() and cvDrawContours().
It also shows the use of a trackbar to control contour retrieval.
 
Usage :
./contours
 
(image:460): Gtk-WARNING **: cannot open display:
</pre>
<p>很明显，运行不起来。<br />
看样子是跟gtk相关，并且我在网上搜到这跟X server什么的可能有关系，于是安装gtk和X server：<br />
<span style="color: rgb(0, 0, 255); ">It failed,&nbsp;</span><span style="color:#0000ff;">unfortunately</span><span style="color: rgb(0, 0, 255); ">.</span><br />
<span style="color:#0000ff;">And Google told me that the error is related to X server, too, so I installed gtk &amp; X server:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
pacman -S gtk xorg-server
</pre>
<p>再修改配置文件&nbsp;/etc/ssh/sshd_config ：<br />
<span style="color:#0000ff;">Modify the config file /etc/ssh/sshd_config :</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
#AllowTcpForwarding yes
AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
X11UseLocalhost yes
</pre>
<p>注意，没有注释的那三行是我添加的。<br />
<span style="color:#0000ff;">Note that the 3&nbsp;uncommented lines are added by me.</span><br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
重启Pi的系统，运行X server：<br />
<span style="color:#0000ff;">Reboot Pi &amp; run X server:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
X
</pre>
<p>没错，命令就是这么简单。然后又出错了：<br />
<span style="color:#0000ff;">Yes, the command is just so simple, but it failed to start:</span></p>
<blockquote>
<div>
		[root@alarmpi c]# X &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>
		&nbsp;</div>
<div>
		&nbsp;X.Org X Server 1.13.0</div>
<div>
		&nbsp;Release Date: 2012-09-05</div>
<div>
		&nbsp;X Protocol Version 11, Revision 0</div>
<div>
		&nbsp;Build Operating System: Linux 3.0.15-1-ARCH+ armv6l&nbsp;</div>
<div>
		&nbsp;Current Operating System: Linux alarmpi 3.2.27-11-ARCH+ #1 PREEMPT Sat Oct 13 22:46:16 UTC 2012 armv6l</div>
<div>
		&nbsp;Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1920 bcm2708_fb.fbheight=1080 bcm2708.boardrev=0x3 bcm2708.serial=0x398ca41f smsc95xx.macaddr=B8:27:EB:8C:A4:1F vc_mem.mem_base=0xc000000 vc_mem.mem_size=0x10000000 &nbsp;sdhci-bcm2708.enable_llm=1 sdhci-bcm2708.sync_after_dma=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 loglevel=2 root=/dev/mmcblk0p2 init=/bin/systemd rootfstype=ext4 rootwait</div>
<div>
		&nbsp;Build Date: 14 October 2012 &nbsp;10:05:22PM</div>
<div>
		&nbsp;&nbsp;</div>
<div>
		&nbsp;Current version of pixman: 0.26.2</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>Before reporting problems, check http://wiki.x.org</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>to make sure that you have the latest version.</div>
<div>
		&nbsp;Markers: (--) probed, (**) from config file, (==) default setting,</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>(++) from command line, (!!) notice, (II) informational,</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>(WW) warning, (EE) error, (NI) not implemented, (??) unknown.</div>
<div>
		&nbsp;(==) Log file: &quot;/var/log/Xorg.0.log&quot;, Time: Mon Oct 15 14:30:03 2012</div>
<div>
		&nbsp;(==) Using config directory: &quot;/etc/X11/xorg.conf.d&quot;</div>
<div>
		&nbsp;Initializing built-in extension Generic Event Extension</div>
<div>
		&nbsp;Initializing built-in extension SHAPE</div>
<div>
		&nbsp;Initializing built-in extension MIT-SHM</div>
<div>
		&nbsp;Initializing built-in extension XInputExtension</div>
<div>
		&nbsp;Initializing built-in extension XTEST</div>
<div>
		&nbsp;Initializing built-in extension BIG-REQUESTS</div>
<div>
		&nbsp;Initializing built-in extension SYNC</div>
<div>
		&nbsp;Initializing built-in extension XKEYBOARD</div>
<div>
		&nbsp;Initializing built-in extension XC-MISC</div>
<div>
		&nbsp;Initializing built-in extension SECURITY</div>
<div>
		&nbsp;Initializing built-in extension XINERAMA</div>
<div>
		&nbsp;Initializing built-in extension XFIXES</div>
<div>
		&nbsp;Initializing built-in extension RENDER</div>
<div>
		&nbsp;Initializing built-in extension RANDR</div>
<div>
		&nbsp;Initializing built-in extension COMPOSITE</div>
<div>
		&nbsp;Initializing built-in extension DAMAGE</div>
<div>
		&nbsp;Initializing built-in extension MIT-SCREEN-SAVER</div>
<div>
		&nbsp;Initializing built-in extension DOUBLE-BUFFER</div>
<div>
		&nbsp;Initializing built-in extension RECORD</div>
<div>
		&nbsp;Initializing built-in extension DPMS</div>
<div>
		&nbsp;Initializing built-in extension X-Resource</div>
<div>
		&nbsp;Initializing built-in extension XVideo</div>
<div>
		&nbsp;Initializing built-in extension XVideo-MotionCompensation</div>
<div>
		&nbsp;Initializing built-in extension XFree86-VidModeExtension</div>
<div>
		&nbsp;Initializing built-in extension XFree86-DGA</div>
<div>
		&nbsp;Initializing built-in extension XFree86-DRI</div>
<div>
		&nbsp;Initializing built-in extension DRI2</div>
<div>
		&nbsp;Loading extension GLX</div>
<div>
		&nbsp;</div>
<div>
		&nbsp;Fatal server error:</div>
<div>
		&nbsp;no screens found</div>
<div>
		&nbsp;(EE)&nbsp;</div>
<div>
		&nbsp;Please consult the The X.Org Foundation support&nbsp;</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>&nbsp;at http://wiki.x.org</div>
<div>
		&nbsp; for help.&nbsp;</div>
<div>
		&nbsp;(EE) Please also check the log file at &quot;/var/log/Xorg.0.log&quot; for additional information.</div>
<div>
		&nbsp;(EE)&nbsp;</div>
<div>
		&nbsp;Server terminated with error (1). Closing log file.</div>
</blockquote>
<p>既然它让我看日志文件&nbsp;/var/log/Xorg.0.log，那我就看。在日志文件的最后，发现提示找不到 fbdev 模块/库，于是翻遍Google，找到一个答案：应该安装&nbsp;xf86-video-fbdev 包：<br />
<span style="color:#0000ff;">Since it told me to check log file&nbsp;/var/log/Xorg.0.log, then I did. At the last few lines of the log file, I found an error message which indicated a fbdev module/lib missing, so I ask Google again for a while, and I realized that I should install package&nbsp;xf86-video-fbdev:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
pacman -S xf86-video-fbdev
</pre>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
再用&ldquo;X&rdquo;命令启动X server，成功了：<br />
<span style="color:#0000ff;">Then run &quot;X&quot; again, it succeeded:</span></p>
<blockquote>
<div>
		[root@alarmpi ~]# X</div>
<div>
		&nbsp;</div>
<div>
		&nbsp;X.Org X Server 1.13.0</div>
<div>
		&nbsp;Release Date: 2012-09-05</div>
<div>
		&nbsp;X Protocol Version 11, Revision 0</div>
<div>
		&nbsp;Build Operating System: Linux 3.0.15-1-ARCH+ armv6l&nbsp;</div>
<div>
		&nbsp;Current Operating System: Linux alarmpi 3.2.27-11-ARCH+ #1 PREEMPT Sat Oct 13 22:46:16 UTC 2012 armv6l</div>
<div>
		&nbsp;Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1920 bcm2708_fb.fbheight=1080 bcm2708.boardrev=0x3 bcm2708.serial=0x398ca41f smsc95xx.macaddr=B8:27:EB:8C:A4:1F vc_mem.mem_base=0xc000000 vc_mem.mem_size=0x10000000 &nbsp;sdhci-bcm2708.enable_llm=1 sdhci-bcm2708.sync_after_dma=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 loglevel=2 root=/dev/mmcblk0p2 init=/bin/systemd rootfstype=ext4 rootwait</div>
<div>
		&nbsp;Build Date: 14 October 2012 &nbsp;10:05:22PM</div>
<div>
		&nbsp;&nbsp;</div>
<div>
		&nbsp;Current version of pixman: 0.26.2</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>Before reporting problems, check http://wiki.x.org</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>to make sure that you have the latest version.</div>
<div>
		&nbsp;Markers: (--) probed, (**) from config file, (==) default setting,</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>(++) from command line, (!!) notice, (II) informational,</div>
<div>
		&nbsp;<span class="Apple-tab-span" style="white-space: pre; "> </span>(WW) warning, (EE) error, (NI) not implemented, (??) unknown.</div>
<div>
		&nbsp;(==) Log file: &quot;/var/log/Xorg.0.log&quot;, Time: Mon Oct 15 14:32:34 2012</div>
<div>
		&nbsp;(==) Using config directory: &quot;/etc/X11/xorg.conf.d&quot;</div>
<div>
		&nbsp;Initializing built-in extension Generic Event Extension</div>
<div>
		&nbsp;Initializing built-in extension SHAPE</div>
<div>
		&nbsp;Initializing built-in extension MIT-SHM</div>
<div>
		&nbsp;Initializing built-in extension XInputExtension</div>
<div>
		&nbsp;Initializing built-in extension XTEST</div>
<div>
		&nbsp;Initializing built-in extension BIG-REQUESTS</div>
<div>
		&nbsp;Initializing built-in extension SYNC</div>
<div>
		&nbsp;Initializing built-in extension XKEYBOARD</div>
<div>
		&nbsp;Initializing built-in extension XC-MISC</div>
<div>
		&nbsp;Initializing built-in extension SECURITY</div>
<div>
		&nbsp;Initializing built-in extension XINERAMA</div>
<div>
		&nbsp;Initializing built-in extension XFIXES</div>
<div>
		&nbsp;Initializing built-in extension RENDER</div>
<div>
		&nbsp;Initializing built-in extension RANDR</div>
<div>
		&nbsp;Initializing built-in extension COMPOSITE</div>
<div>
		&nbsp;Initializing built-in extension DAMAGE</div>
<div>
		&nbsp;Initializing built-in extension MIT-SCREEN-SAVER</div>
<div>
		&nbsp;Initializing built-in extension DOUBLE-BUFFER</div>
<div>
		&nbsp;Initializing built-in extension RECORD</div>
<div>
		&nbsp;Initializing built-in extension DPMS</div>
<div>
		&nbsp;Initializing built-in extension X-Resource</div>
<div>
		&nbsp;Initializing built-in extension XVideo</div>
<div>
		&nbsp;Initializing built-in extension XVideo-MotionCompensation</div>
<div>
		&nbsp;Initializing built-in extension XFree86-VidModeExtension</div>
<div>
		&nbsp;Initializing built-in extension XFree86-DGA</div>
<div>
		&nbsp;Initializing built-in extension XFree86-DRI</div>
<div>
		&nbsp;Initializing built-in extension DRI2</div>
<div>
		&nbsp;Loading extension GLX</div>
</blockquote>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
再试着运行OpenCV的sample，依然不行，Google搜索到一篇<a href="http://homebrew.jp/show?page=1440" target="_blank" rel="noopener noreferrer">在Pi上运行OpenCV的文章</a>，作者做了这样一件事情：<br />
<span style="color:#0000ff;">Then I tried to run the OpenCV sample again, still the same error(&quot;cannot open display&quot;), so I Googled </span><a href="http://homebrew.jp/show?page=1440" target="_blank" rel="noopener noreferrer"><span style="color:#0000ff;"><span style="background-color:#ffa07a;">another article about using OpenCV on Pi</span></span></a><span style="color:#0000ff;">&nbsp;and found that the author did one thing:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;" style="width: 729px; ">
export DISPLAY=192.168.3.65:0.0
</pre>
<p>其中，192.168.3.65是Pi的IP地址。这样设置之后，再运行，程序不再报错了！再一看相同的目录下，生成了一个&nbsp;contours.xml 文件，说明程序执行成功了。<br />
<span style="color:#0000ff;">In which the &quot;192.168.3.65&quot; is your Pi&#39;s IP address. After setting this, the sample could run without any error&nbsp;occurred, and a contours.xml file was generated under the same directory, which indicated that we succeeded.</span></p>
<p><span style="background-color:#00ff00;">【4】</span>写一个简单的OpenCV C程序 / Write a simple OpenCV C program<br />
现在该写一个简单的OpenCV C程序来控制摄像头了。<br />
<span style="color:#0000ff;">Now we should write a simple C program to control the webcam by using OpenCV.</span></p>
<p>直接看程序：<br />
<span style="color:#0000ff;">Let&#39;s check the C code:</span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;highgui.h&quot;
#include &quot;opencv.hpp&quot;

int main(int argc, char **argv) {
	/* init camera */
	CvCapture* pCapture = cvCreateCameraCapture(0);
	cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 320); 
	cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 240);
	//cvSetCaptureProperty(pCapture, CV_CAP_PROP_FPS, 5);

	IplImage *pFrame = 0;

	if (NULL == pCapture) {
		fprintf(stderr, &quot;Can&#39;t initialize webcam!\n&quot;);
		return 1;
	}

	pFrame = cvQueryFrame(pCapture);	// query a frame 

	if(NULL == pFrame) {
		fprintf(stderr, &quot;Can&#39;t get a frame!\n&quot; );
		return 1;
	}

	const char *pImageFileName = &quot;webcam.jpg&quot;;
	cvSaveImage(pImageFileName, pFrame);

	cvReleaseCapture(&amp;pCapture);	// free memory

	return 0;
}
</pre>
<p>此程序做的事情非常简单：初始化摄像头，抓取一帧，然后保存为名为&ldquo;webcam.jpg&rdquo;的图片。<br />
<span style="color:#0000ff;">This program does a very simple job: initialize the camera, capture a frame &amp; save it to a image file named &quot;webcam.jpg&quot;.</span><br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
编译自己的webcam程序：<br />
<span style="color:#0000ff;">Then compile the webcam program:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
g++ -I/usr/include/opencv/ -I/usr/include/opencv2/  `pkg-config --cflags opencv --libs opencv` webcam.c -o webcam
</pre>
<p>会生成一个可执行程序&ldquo;webcam&rdquo;，运行它：<br />
<span style="color:#0000ff;">This will generate an executable bin &quot;webcam&quot;, just run it:</span></p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
./webcam
</pre>
<p>然后在相同目录下，会看到生成了一个&ldquo;webcam.jpg&rdquo;图片，正是摄像头拍的照。<br />
<span style="color:#0000ff;">Then we&#39;ll get a &quot;webcam.jpg&quot; image file under the same directory, which is just the photo took by the camera.</span></p>
<p>这样，本文的主要目的就达成了。可以在此基础上继续开发其他的程序，来实现更为有用的功能，例如像<a href="http://www.codelast.com/?p=4341" target="_blank" rel="noopener noreferrer">摄像头娘</a>一样的运动检测功能。<br />
<span style="color:#0000ff;">So the main&nbsp;purpose of this article has been reached. We can develop more useful programs base on it, e.g. a program similar to the </span><a href="http://www.codelast.com/?p=4341" target="_blank" rel="noopener noreferrer"><span style="color:#0000ff;"><span style="background-color:#ffa07a;">Camera Girl</span></span></a><span style="color:#0000ff;">, which implements the motion detection&nbsp;functionality.</span><br />
<span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">【5】</span>遗留问题 <span style="color:#ff0000;">/</span>&nbsp;<span style="color:#0000ff;">Unsolved problems</span><br />
在上述过程中，其实我还遇到了一些未解决的问题，暂时没有时间深究，在此先记录如下。<br />
<span style="color:#0000ff;">In the above process I met some unsolved problems and have no time to explore them yet, so I just write them down here for further study.</span></p>
<p><span style="background-color:#ee82ee;">①</span>拍照时使用稍高的分辨率就导致程序崩溃 <span style="color:#ff0000;">/</span> <span style="color:#0000ff;">Use a little high resolution will cause the program crash</span><br />
在上面的程序中，如下两句是用于设置分辨率的（宽*高）：<br />
<span style="color:#0000ff;">In the above codes, the two lines below are used to set the resolution:</span></p>
<pre class="brush:cpp;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH, 320); 
cvSetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT, 240);
</pre>
<p>根据我的试验，160*120的低分辨率比较少出问题，尽管320*240的分辨率对摄像头来说也根本不算高的，但是就算是使用320*240的分辨率，程序还是偶尔会崩溃的，报错如下：<br />
<span style="color:#0000ff;">According to my experiments, the 160*120 low resolution cause less crashes than higher resolutions. Even 320*240 is not a high resolution for a webcam nowadays, it still made the program crashed some times, and the error messages are:</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
[root@alarmpi c]# ./webcam 
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_REQBUFS: Cannot allocate memory
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
Unable to stop the stream.: Bad file descriptor
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
munmap: Invalid argument
HIGHGUI ERROR: V4L: Initial Capture Error: Unable to load initial memory buffers.
Can&#39;t get a frame!
</pre>
<p><span style="color: rgb(255, 255, 255); ">文章来源：</span><a href="http://www.codelast.com/" target="_blank" rel="noopener noreferrer"><span style="color: rgb(255, 255, 255); ">http://www.codelast.com/</span></a><br />
或者：<br />
<span style="color:#0000ff;">Or:</span></p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
VIDIOC_REQBUFS: Cannot allocate memory
HIGHGUI ERROR: V4L/V4L2: Could not obtain specifics of capture window.

Unable to stop the stream.: Bad file descriptor
VIDIOC_QBUF: Bad file descriptor
Can&#39;t get a frame!
</pre>
<p>我不确定这是跟Pi的CPU计算能力较弱有关呢，还是跟Pi的RAM太小有关呢，还是跟摄像头的供电不足有关呢（我没有使用带电源的USB HUB来接摄像头），或者是跟摄像头的驱动有关呢？<br />
如果你知道解决办法，请赐教，非常感谢。<br />
<span style="color:#0000ff;">I&#39;m not sure whether the problem is caused by the weak computation capability of Pi&#39;s CPU, or the too-small RAM on Pi, or the&nbsp;insufficient current supply of the webcam(I didn&#39;t use a powered USB HUB to connect the webcam to Pi), or the webcam&#39;s Linux driver...<br />
If you know something, please tell me and thank you&nbsp;in advance.</span></p>
<p><span style="background-color: rgb(238, 130, 238);">2015-02-19更新：</span><br />
在树莓派model B+上，使用OpenCV 2.4.10版的上述程序，已经没有了上面所说的问题，经我测试，每隔2秒一次，连续抓取了将近30分钟的图像，一次都没有问题。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
<div id="KSFIND_MASK" style="background-color: rgb(0, 0, 0); opacity: 0.22; position: absolute !important; left: 0px !important; top: 0px !important; border: 0px none !important; padding: 0px !important; z-index: 1000000 !important; height: 0px; width: 0px; display: none; ">
	&nbsp;</div>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushCpp.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e5%9c%a8raspberry-pi%e4%b8%8a%e7%94%a8opencv%e6%9d%a5%e6%93%8d%e7%ba%b5%e6%91%84%e5%83%8f%e5%a4%b4%e6%8b%8d%e7%85%a7use-opencv-on-raspberry-pi-to-controll-a-webcam-to-take-photos/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
