cv图像识别

安卓opencv全分辨率找图


connectedComponentsWithStats_Point 获取连通域坐标

<p>[TOC]</p> <h5>简要描述</h5> <p>检查轮廓、边缘连通域。在物体形状、特征检测领域有很大作用 connectedComponentsWithStats方法可以获得连通域位置、宽高、中心,而本方法connectedComponentsWithStats_Point 可以获得连通域的坐标。(由于数据量过大,所以分成了2个方法) <strong>V 2.1.2 以及以上版本可用</strong></p> <h5>参数</h5> <table> <thead> <tr> <th style="text-align: left;">参数名</th> <th style="text-align: left;">必选</th> <th style="text-align: left;">类型</th> <th>说明</th> </tr> </thead> <tbody> <tr> <td style="text-align: left;">mat</td> <td style="text-align: left;">是</td> <td style="text-align: left;">mat</td> <td>欲检测的mat对象, 必须8uc1 格式,</td> </tr> <tr> <td style="text-align: left;">connectivity</td> <td style="text-align: left;">(可选)</td> <td style="text-align: left;">int</td> <td>标记连通域时使用的邻域种类,默认=8. 4:四邻域,8:八邻域。</td> </tr> <tr> <td style="text-align: left;">ltype</td> <td style="text-align: left;">(可选)</td> <td style="text-align: left;">int</td> <td>输出图像的数据类型,默认=4。目前支持CV_32S=4和CV_16U=2两种数据类型。</td> </tr> <tr> <td style="text-align: left;">ccltype</td> <td style="text-align: left;">(可选)</td> <td style="text-align: left;">int</td> <td>算法类型。 默认=0。可选值: 0、-1、1</td> </tr> </tbody> </table> <h5>返回参数说明</h5> <p>jsonArr | null</p> <p>返回值样例:(二维数组,每一行中,保护一个连通域中所有坐标) [ [{&quot;x&quot;:381,&quot;y&quot;:42},{&quot;x&quot;:382,&quot;y&quot;:42},{&quot;x&quot;:383,&quot;y&quot;:42},{&quot;x&quot;:384,&quot;y&quot;:42}], [{&quot;x&quot;:381,&quot;y&quot;:42},{&quot;x&quot;:382,&quot;y&quot;:42},{&quot;x&quot;:383,&quot;y&quot;:42},{&quot;x&quot;:384,&quot;y&quot;:42}], [{&quot;x&quot;:381,&quot;y&quot;:42},{&quot;x&quot;:382,&quot;y&quot;:42},{&quot;x&quot;:383,&quot;y&quot;:42},{&quot;x&quot;:384,&quot;y&quot;:42}] ]</p> <h5>连通域着色</h5> <pre><code class="language-java"> //载入图 var mat_1=cvImg.loadFromFile('/sdcard/Pictures/t1.png',1); //转到灰度 var mat_gray=cvImg.toGray(mat_1); //二值化 var mat_two=cvImg.threshold(mat_gray,0,100,255); //连通域检测,返回所有连通的坐标点。 var arr=cvImg.connectedComponentsWithStats_Point(mat_two); //在原图上画出这些点 for(var i=0;i&lt;arr.length;i++){ var list_sub=arr[i]; var color=[random(100,255),random(100,255),random(100,255),255];//随机一种颜色 for(var n=0;n&lt;list_sub.length;n++){ var x=list_sub[n].x; var y=list_sub[n].y; cvImg.setColor(mat_1,x,y,color)//给连通域坐标描点 } } //保存到文件,查看效果 cvImg.toFile(mat_1,"连通域效果输出.png")</code></pre> <p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=bb3ceea75b3f2c107caff0fad53896b4&amp;file=file.png" alt="" /></p> <p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=bced436ba6e59cc577c20652e3771979&amp;file=file.png" alt="" /></p> <h5>连通域字符画</h5> <pre><code class="language-java"> //载入图 var mat_1=cvImg.loadFromFile('/sdcard/Pictures/t1.png',1); //转到灰度 var mat_gray=cvImg.toGray(mat_1); //二值化 var mat_two=cvImg.threshold(mat_gray,0,100,255); //连通域检测,返回所有连通的坐标点。 var arr=cvImg.connectedComponentsWithStats_Point(mat_two); //定义字符画数组,图像宽*图像高 二维数组,全部赋值为:0 var arrStr=new Array(); for(var y=0;y&lt;cvImg.getHeight(mat_1);y++){ arrStr[y]=new Array(); for(var x=0;x&lt;cvImg.getWidth(mat_1);x++){ arrStr[y][x]=0; } } //在字符画数组上,标记连通域的坐标点 for(var i=0;i&lt;arr.length;i++){ var list_sub=arr[i]; var v=i+1;//填充的值 for(var n=0;n&lt;list_sub.length;n++){ var x=list_sub[n].x; var y=list_sub[n].y; arrStr[y][x]=v; } } //输出字符画 var strOut=""; for(var y=0;y&lt;cvImg.getHeight(mat_1);y++){ var str_tem=""; for(var x=0;x&lt;cvImg.getWidth(mat_1);x++){ str_tem+= arrStr[y][x]+''; } strOut+=str_tem; strOut+='\n'; } //保存到文件 file.writeFile(strOut,'/sdcard/Pictures/strimg.txt')</code></pre> <p>字符画细节 <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=550262a967cec919b8c137e495506450&amp;file=file.png" alt="" /></p> <p>字符画宏观 <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=33ec03d308ea895765b555e9dd82800a&amp;file=file.png" alt="" /></p> <p>通过以上例子,也许你应该已经明白连通域的使用了吧。至于它有什么作用?这需要你动脑筋自己想想啦。~~</p>

页面列表

ITEM_HTML