您的位置:首页 > 编程语言 > Python开发

OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并

2018-01-05 20:14 806 查看
<div class="article_content tracking-ad" id="article_content" data-mod="popu_307" data-dsm="post">
<h1 style="color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t0"></a><span style="font-size:32px">OpenCV之Python学习笔记</span></h1>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

一直都在用Python+OpenCV做一些算法的原型。本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段。现在看到一本国外的新书《OpenCV Computer Vision with Python》,于是就看一遍,顺便把自己掌握的东西整合一下,写成学习笔记了。更需要的朋友参考。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

阅读须知:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="font-family:KaiTi_GB2312; font-size:10px">        本文不是纯粹的译文,只是比较贴近原文的笔记;<br>

        请设法购买到出版社出版的书,支持正版。</span></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

       从书名就能看出来本书是介绍在Python中使用OpenCV,全书分为5章,两个附录:</p>

<ul style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<li>第一章<strong>OpenCV设置</strong>,介绍如何在Windows、Mac和Ubuntu上设置Pyhton、OpenCV和相关库的环境。还讨论了OpenCV社区、OpenCV文档以及官方的示例代码。</li><li>第二章<strong>处理文件、摄像头和GUI</strong>,讨论OpenCV的I/O功能,接着使用面向对象的设计编写一个主应用程序,用于显示摄像头实时场景、处理键盘输入、将摄像头写入视频文件和静态图像文件。</li><li>第三章<strong>图像过滤</strong>,介绍使用OpenCV、NumPy和SciPy来编写图像过滤器。过滤器可用于线性颜色操作、曲线颜色操作、模糊化、锐化和寻找边缘。本章修改第一章的主程序,将过滤器应用到实时摄像头场景中。</li><li>第四章<strong>使用Haar
Cascades追踪人脸</strong>,本章将编写一个层次化的人脸追踪器,使用OpenCV定位图像中的脸部、眼睛、鼻子和嘴巴。同时还编写了用于复制和改变图像中某块区域的大小。同样,本章也将修改之前的主应用程序,让其可以用于找到并处理摄像头场景中的人脸。</li><li>第五章<strong>检测前景/背景区域和深度</strong>。通过本章将了解有关OpenCV(在OpenNI和SensorKinect的支持下)从深度摄像头中获得的数据类型的信息。接着编写一些函数,使用这些数据对前景区域施加一些限制效果。最后将这些函数整合到主程序中,使得在处理人脸之前先进行细化操作。</li><li>附录A,<strong>与Pygame整合</strong>。修改主程序,用Pygame替换OpenCV来处理特定的I/O事件。(Pygame提供了更多样的事件处理函数。)</li><li>附录B,<strong>为自定义目标生成Haar
Cascades</strong>,允许我们检测一系列的OpenCV工具,来对任何类型的目标或模式构建跟踪器,而不仅仅是人脸。</li></ul>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

本书第一章是介绍在不同操作系统上对OpenCV、Python及相关库的配置,这里就不介绍了。下一篇文章将直接从第二章开始介绍。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<h1 style="color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t1"></a><span style="font-size:32px">OpenCV Python教程(1、图像的载入、显示和保存)</span></h1>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

本文是OpenCV  2 Computer Vision Application Programming Cookbook读书笔记的第一篇。在笔记中将以<a title="Python知识库" class="replace_word" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold" href="http://lib.csdn.net/base/11" target="_blank">Python</a>语言改写每章的代码。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

PythonOpenCV的配置这里就不介绍了。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

注意,现在OpenCV for Python就是通过NumPy进行绑定的。所以在使用时必须掌握一些NumPy的相关知识!</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

图像就是一个矩阵,在OpenCV for Python中,图像就是NumPy中的数组!<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

如果读取图像首先要导入OpenCV包,方法为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 1371px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_1" width="18" height="18" align="middle"
id="ZeroClipboardMovie_1" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=1&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:823px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border
23ff7
:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li></ol>

</div>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t2"></a>

<a name="t0" style="color:rgb(51,102,153)" target="_blank"></a>读取并显示图像</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

在Python中不需要声明变量,所以也就不需要C++中的cv::Mat xxxxx了。只需这样:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 1510px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_2" width="18" height="18" align="middle"
id="ZeroClipboardMovie_2" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=2&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:976px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">img = cv2.imread(</span><span class="string" style="margin:0px; padding:0px; border:none;
color:blue; background-color:inherit">"D:\cat.jpg"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

OpenCV目前支持读取bmp、jpg、png、tiff等常用格式。更详细的请参考OpenCV的参考文档。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

接着创建一个窗口</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 1649px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_3" width="18" height="18" align="middle"
id="ZeroClipboardMovie_3" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=3&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1129px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">cv2.namedWindow(</span><span class="string" style="margin:0px; padding:0px; border:none; color:blue;
background-color:inherit">"Image"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

然后在窗口中显示图像</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 1762px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_4" width="18" height="18" align="middle"
id="ZeroClipboardMovie_4" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=4&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1242px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">cv2.imshow(</span><span class="string" style="margin:0px; padding:0px; border:none; color:blue;
background-color:inherit">"Image"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, img)  </span></span></li></ol>

</div>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

最后还要添上一句:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 1875px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_5" width="18" height="18" align="middle"
id="ZeroClipboardMovie_5" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=5&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1355px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">cv2.waitKey (</span><span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

如果不添最后一句,在IDLE中执行窗口直接无响应。在命令行中执行的话,则是一闪而过。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

完整的程序为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 2014px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_6" width="18" height="18" align="middle"
id="ZeroClipboardMovie_6" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=6&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1508px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2   </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:\\cat.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)   </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.namedWindow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Image"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">)   </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Image"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img)   </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey (<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">最后释放窗口是个好习惯!</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t3"></a>

<a name="t1" style="color:rgb(51,102,153)" target="_blank"></a>创建/复制图像</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

新的OpenCV的接口中没有CreateImage接口。即没有cv2.CreateImage这样的函数。如果要创建图像,需要使用numpy的函数(现在使用OpenCV-Python绑定,numpy是必装的)。如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 2339px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_7" width="18" height="18" align="middle"
id="ZeroClipboardMovie_7" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=7&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1861px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">emptyImage = np.zeros(img.shape, np.uint8)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在新的OpenCV-Python绑定中,图像使用NumPy数组的属性来表示图像的尺寸和通道信息。如果输出img.shape,将得到(500, 375, 3),这里是以OpenCV自带的cat.jpg为示例。最后的3表示这是一个RGB图像。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

也可以复制原有的图像来获得一副新图像。</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 2504px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_8" width="18" height="18" align="middle"
id="ZeroClipboardMovie_8" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=8&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2040px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">emptyImage2 = img.copy();  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">如果不怕麻烦,还可以用cvtColor获得原图像的副本。</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 2617px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_9" width="18" height="18" align="middle"
id="ZeroClipboardMovie_9" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=9&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2153px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  </span></span></li><li style="border-left-width:3px;
border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#emptyImage3[...]=0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">后面的emptyImage3[...]=0是将其转成空白的黑色图像。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t4"></a>

<a name="t2" style="color:rgb(51,102,153)" target="_blank"></a>保存图像 <br>

</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

保存图像很简单,直接用cv2.imwrite即可。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

cv2.imwrite("D:\\cat2.jpg", img)</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

第一个参数是保存的路径及文件名,第二个是图像矩阵。其中,imwrite()有个可选的第三个参数,如下:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

cv2.imwrite("D:\\cat2.jpg", img,[int(cv2.IMWRITE_JPEG_QUALITY), 5])</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

第三个参数针对特定的格式: 对于JPEG,其表示的是图像的质量,用0-100的整数表示,默认为95。 注意,cv2.IMWRITE_JPEG_QUALITY类型为Long,必须转换成int。下面是以不同质量存储的两幅图:<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130612174753359"><br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

对于PNG,第三个参数表示的是压缩级别。cv2.IMWRITE_PNG_COMPRESSION,从0到9,压缩级别越高,图像尺寸越小。默认级别为3:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 3520px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_10" width="18" height="18" align="middle"
id="ZeroClipboardMovie_10" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=10&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3091px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">cv2.imwrite(</span><span class="string" style="margin:0px; padding:0px; border:none; color:blue;
background-color:inherit">"./cat.png"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, img, [int(cv2.IMWRITE_PNG_COMPRESSION), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">])   </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imwrite(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat2.png"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img, [int(cv2.IMWRITE_PNG_COMPRESSION), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">9</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">保存的图像尺寸如下:</span><img style="border:none; max-width:100%; color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px" alt="" src="https://img-blog.csdn.net/20130612174818046"><span
style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

还有一种支持的图像,一般不常用。</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

完整的代码为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9057415#" target="_blank">copy</a><div style="left: 487px; top: 3935px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_11" width="18" height="18" align="middle"
id="ZeroClipboardMovie_11" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=11&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3497px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">emptyImage = np.zeros(img.shape, np.uint8)  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">emptyImage2 = img.copy()  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">emptyImage3=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#emptyImage3[...]=0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"EmptyImage"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, emptyImage)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Image"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"EmptyImage2"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, emptyImage2)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"EmptyImage3"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, emptyImage3)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imwrite(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat2.jpg"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img, [int(cv2.IMWRITE_JPEG_QUALITY), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">5</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imwrite(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat3.jpg"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img, [int(cv2.IMWRITE_JPEG_QUALITY), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">100</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imwrite(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat.png"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img, [int(cv2.IMWRITE_PNG_COMPRESSION), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imwrite(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"./cat2.png"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, img, [int(cv2.IMWRITE_PNG_COMPRESSION), </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">9</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey (<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">参考资料:</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">《OpenCV References Manuel》</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">《OpenCV  2 Computer Vision Application Programming Cookbook》</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">《OpenCV Computer Vision with Python》</span><br>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br>

</span></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br>

</span></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br>

</span></p>

<h1 style="color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t5"></a><span style="color:rgb(51,51,51); font-family:Arial; line-height:26px"><span style="font-size:32px">OpenCV Python教程(2、图像元素的访问、通道分离与合并)</span></span></h1>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span></p>

<h1 align="center" style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t6"></a>

OpenCV Python教程之图像元素的访问、通道分离与合并<br>

</h1>

<p></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

转载请详细注明原作者及出处,谢谢!<br>

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t7"></a>

<a name="t1" style="color:rgb(51,102,153)" target="_blank"></a>访问像素</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

像素的访问和访问numpy中ndarray的方法完全一样,灰度图为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 4746px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_12" width="18" height="18" align="middle"
id="ZeroClipboardMovie_12" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=12&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:743px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">img[j,i] = </span><span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其中j,i分别表示图像的行和列。对于BGR图像,为:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 4859px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_13" width="18" height="18" align="middle"
id="ZeroClipboardMovie_13" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=13&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:856px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">img[j,i,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img[j,i,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img[j,i,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">第三个数表示通道。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

下面通过对图像添加人工的椒盐现象来进一步说明OpenCV <a title="Python知识库" class="replace_word" style="color:rgb(223,52,52); text-decoration:none; font-weight:bold" href="http://lib.csdn.net/base/11" target="_blank">Python</a>中需要注意的一些问题。完整代码如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 5034px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_14" width="18" height="18" align="middle"
id="ZeroClipboardMovie_14" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=14&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:1045px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">def</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> salt(img, n):  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">for</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> k </span><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">in</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> range(n):  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        i = int(np.random.random() * img.shape[<span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]);  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        j = int(np.random.random() * img.shape[<span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]);  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">if</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> img.ndim == </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">:   </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            img[j,i] = <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">elif</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> img.ndim == </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">:   </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            img[j,i,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            img[j,i,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">            img[j,i,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">]= </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">255</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">return</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> img  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">if</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> __name__ == </span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">'__main__'</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">:  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"图像路径"</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    saltImage = salt(img, <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">500</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Salt"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, saltImage)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">    cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">处理后能得到类似下面这样带有模拟椒盐现象的图片:</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<img style="border:none; max-width:100%" alt="" src="https://img-blog.csdn.net/20130614163545218"><br>

<br>

上面的代码需要注意几点:</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

1、与C++不同,在Python中灰度图的img.ndim = 2,而C++中灰度图图像的通道数img.channel() =1<br>

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

2、为什么使用np.random.random()?<br>

这里使用了numpy的随机数,Python自身也有一个随机数生成函数。这里只是一种习惯,np.random模块中拥有更多的方法,而Python自带的random只是一个轻量级的模块。不过需要注意的是np.random.seed()不是线程安全的,而Python自带的random.seed()是线程安全的。如果使用随机数时需要用到多线程,建议使用Python自带的random()和random.seed(),或者构建一个本地的np.random.Random类的实例。<br>

</p>

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t8"></a>

<a name="t2" style="color:rgb(51,102,153)" target="_blank"></a>分离、合并通道</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

由于OpenCV Python和NumPy结合的很紧,所以即可以使用OpenCV自带的split函数,也可以直接操作numpy数组来分离通道。直接法为:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 6241px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_15" width="18" height="18" align="middle"
id="ZeroClipboardMovie_15" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=15&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2374px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/cat.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">b, g, r = cv2.split(img)  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Blue"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, r)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Red"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, g)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Green"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, b)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其中split返回RGB三个通道,如果只想返回其中一个通道,可以这样:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 6516px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_16" width="18" height="18" align="middle"
id="ZeroClipboardMovie_16" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=16&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2649px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">b = cv2.split(img)[</span><span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">g = cv2.split(img)[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">]  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">r = cv2.split(img)[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">]  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">最后的索引指出所需要的通道。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

也可以直接操作NumPy数组来达到这一目的:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 6691px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_17" width="18" height="18" align="middle"
id="ZeroClipboardMovie_17" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=17&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:2838px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/cat.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">b = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">g = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">r = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">b[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">g[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">r[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Blue"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, r)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Red"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, g)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Green"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, b)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">注意先要开辟一个相同大小的图片出来。这是由于numpy中数组的复制有些需要注意的地方,具体事例如下:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 7110px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_18" width="18" height="18" align="middle"
id="ZeroClipboardMovie_18" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=18&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3257px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">>>> c= np.zeros(img.shape, dtype=img.dtype)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> c[:,:,:] = img[:,:,:]  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> d[:,:,:] = img[:,:,:]  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> c <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">is</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> a  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="special" style="margin:0px; padding:0px; border:none; background-color:inherit">False</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> d <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">is</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> a  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="special" style="margin:0px; padding:0px; border:none; background-color:inherit">False</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> c.base <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">is</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> a  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="special" style="margin:0px; padding:0px; border:none; background-color:inherit">False</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> d.base <span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">is</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit"> a </span><span class="comment" style="margin:0px; padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#注意这里!!!</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="special" style="margin:0px; padding:0px; border:none; background-color:inherit">True</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">这里,d只是a的镜像,具体请参考《</span><a style="color:rgb(51,102,153); text-decoration:none; font-family:Arial; font-size:14px; line-height:26px" href="http://blog.csdn.net/sunny2038/article/details/8907736"
target="_blank">NumPy简明教程(二,数组3</a><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">)》中的“复制和镜像”一节。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<h2 style="margin:0px; padding:0px; color:rgb(51,51,51); font-family:Arial; line-height:26px"><a name="t9"></a>

<a name="t3" style="color:rgb(51,102,153)" target="_blank"></a>通道合并</h2>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

同样,通道合并也有两种方法。第一种是OpenCV自带的merge函数,如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 7481px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_19" width="18" height="18" align="middle"
id="ZeroClipboardMovie_19" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=19&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3642px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">merged = cv2.merge([b,g,r]) </span><span class="comment" style="margin:0px;
padding:0px; border:none; color:rgb(0,130,0); background-color:inherit">#前面分离出来的三个通道</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">接着是NumPy的方法:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 7594px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_20" width="18" height="18" align="middle"
id="ZeroClipboardMovie_20" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=20&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3755px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">mergedByNp = np.dstack([b,g,r])   </span&
1e31d
gt;</span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(255,0,0)">注意:这里只是演示,实际使用时请用OpenCV自带的merge函数!</span></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">用NumPy组合的结果不能在OpenCV中其他函数使用,因为其组合方式与OpenCV自带的不一样,如下:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 7733px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_21" width="18" height="18" align="middle"
id="ZeroClipboardMovie_21" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=21&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:3894px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">merged = cv2.merge([b,g,r])  </span></span></li><li style="border-left-width:3px;
border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> </span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Merge by OpenCV"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">   </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> merged.strides  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">mergedByNp = np.dstack([b,g,r])   </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> </span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Merge by NumPy "</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">   </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> mergedByNp.strides  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">结果为:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 7954px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_22" width="18" height="18" align="middle"
id="ZeroClipboardMovie_22" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=22&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:4115px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">Merge by OpenCV  </span></span></li><li style="border-left-width:3px;
border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1125</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">Merge by NumPy  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">500</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">187500</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">NumPy数组的strides属性表示的是在每个维数上以字节计算的步长。这怎么理解呢,看下面这个简单点的例子:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 8121px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_23" width="18" height="18" align="middle"
id="ZeroClipboardMovie_23" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=23&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:4282px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">>>> a = np.arange(</span><span class="number" style="margin:0px; padding:0px;
border:none; background-color:inherit">6</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> a  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">array([<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">5</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">])  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> a.strides  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">a数组中每个元素都是NumPy中的整数类型,占4个字节,所以第一维中相邻元素之间的步长为4(个字节)。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

同样,2维数组如下:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 8332px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_24" width="18" height="18" align="middle"
id="ZeroClipboardMovie_24" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=24&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:4507px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">>>> b = np.arange(</span><span class="number" style="margin:0px; padding:0px;
border:none; background-color:inherit">12</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">).reshape(</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> b  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">array([[ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">],  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       [ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">5</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">6</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none;
background-color:inherit">7</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       [ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">8</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">9</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">10</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">11</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">]])  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">>>> b.strides  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">16</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">从里面开始看,里面是一个4个元素的一维整数数组,所以步长应该为4。外面是一个含有3个元素,每个元素的长度是4×4=16。所以步长为16。</span>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

</p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

下面来看下3维数组:</p>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 8579px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_25" width="18" height="18" align="middle"
id="ZeroClipboardMovie_25" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=25&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:4794px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">>>> c = np.arange(</span><span class="number" style="margin:0px; padding:0px;
border:none; background-color:inherit">27</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">).reshape(</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,</span><span class="number"
style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其结果为:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 8692px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_26" width="18" height="18" align="middle"
id="ZeroClipboardMovie_26" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=26&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:4907px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">array([[[ </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">3</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">5</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">6</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">,  </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">7</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">,  </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">8</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]],  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       [[ <span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">9</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">10</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">11</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">12</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">13</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">14</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">15</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">16</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">17</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]],  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">       [[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">18</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">19</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">20</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">21</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">22</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">23</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">],  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none
none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">        [<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">24</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">25</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">26</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]]])  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">根据前面了解的,推断下这个数组的步长。从里面开始算,应该为(3×4×3,3×4,4)。验证一下:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 8985px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_27" width="18" height="18" align="middle"
id="ZeroClipboardMovie_27" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=27&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:5200px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span style="margin:0px; padding:0px; border:none; background-color:inherit">>>> c.strides  </span></span></li><li style="border-left-width:3px;
border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">36</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span
class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">12</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">, </span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">4</span><span
style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li></ol>

</div>

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">完整的代码为:</span>

<div class="dp-highlighter bg_python" style="font-family:Consolas,'Courier New',Courier,mono,serif; width:700.906px; overflow:auto; padding-top:1px; color:rgb(51,51,51); line-height:26px; margin:18px 0px!important; background-color:rgb(231,229,220)">

<div class="bar" style="padding-left:45px">

<div class="tools" style="padding:3px 8px 10px 10px; font-size:9px; line-height:normal; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; color:silver; border-left-width:3px; border-left-style:solid; border-left-color:rgb(108,226,108); background-color:rgb(248,248,248)">

<strong>[python]</strong> <a title="view plain" class="ViewSource" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px;
background-color:inherit" href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">view

plain</a><span> <a title="copy" class="CopyToClipboard" style="color:rgb(160,160,160); text-decoration:none; border:none; padding:1px; margin:0px 10px 0px 0px; font-size:9px; display:inline-block; width:16px; height:16px; text-indent:-2000px; background-color:inherit"
href="http://blog.csdn.net/sunny2038/article/details/9080047#" target="_blank">copy</a><div style="left: 487px; top: 9116px; width: 18px; height: 18px; position: absolute; z-index: 99;"><embed name="ZeroClipboardMovie_28" width="18" height="18" align="middle"
id="ZeroClipboardMovie_28" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash" wmode="transparent" flashvars="id=28&width=18&height=18"
allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false"></div></span>

<div style="position:absolute; left:587px; top:5331px; width:18px; height:18px; z-index:99">

</div>

<span></span></div>

</div>

<ol class="dp-py" style="padding:0px; border:none; color:rgb(92,92,92); margin:0px 0px 1px 45px!important; background-color:rgb(255,255,255)" start="1">

<li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> cv2  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">import</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> numpy as np  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">img = cv2.imread(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"D:/cat.jpg"</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit">)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">b = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">g = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">r = np.zeros((img.shape[<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">],img.shape[</span><span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">]), dtype=img.dtype)  </span></span></li><li
class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">b[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">g[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">1</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">r[:,:] = img[:,:,<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">2</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">]  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">merged = cv2.merge([b,g,r])  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> </span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Merge by OpenCV"</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">   </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> merged.strides  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> merged  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero
outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">mergedByNp = np.dstack([b,g,r])   </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108);
list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> </span><span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Merge by NumPy "</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">   </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> mergedByNp.strides  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit"><span class="keyword" style="margin:0px; padding:0px; border:none; color:rgb(0,102,153); font-weight:bold; background-color:inherit">print</span><span style="margin:0px;
padding:0px; border:none; background-color:inherit"> mergedByNp  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">  </span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Merged"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, merged)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit;
line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"MergedByNp"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, merged)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important;
padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Blue"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, b)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Red"</span><span style="margin:0px; padding:0px; border:none;
background-color:inherit">, r)  </span></span></li><li style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px
3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.imshow(<span class="string" style="margin:0px; padding:0px; border:none; color:blue; background-color:inherit">"Green"</span><span style="margin:0px; padding:0px;
border:none; background-color:inherit">, g)  </span></span></li><li class="alt" style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px;
margin:0px!important; padding:0px 3px 0px 10px!important">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.waitKey(<span class="number" style="margin:0px; padding:0px; border:none; background-color:inherit">0</span><span style="margin:0px; padding:0px; border:none; background-color:inherit">)  </span></span></li><li
style="border-left-width:3px; border-style:none none none solid; border-left-color:rgb(108,226,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important; background-color:rgb(248,248,248)">

<span style="margin:0px; padding:0px; border:none; color:black; background-color:inherit">cv2.destroyAllWindows()  </span></li></ol>

</div>

<br>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br>

</span></p>

<p style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">

<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">from: http://blog.csdn.net/sunny2038/article/category/904451</span></p>
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐