一、简介

二、外接矩形的查找绘制

 #include "opencv2/opencv.hpp"
using namespace cv;
void main()
{
//外接矩形的查找绘制
Mat srcImg =imread("E://12.jpg");
imshow("src",srcImg);
Mat dstImg = srcImg.clone(); //原图备份
cvtColor(srcImg, srcImg, CV_BGR2GRAY); //转为灰度图
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化 vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
//drawContours(dstImg, contours, -1, Scalar(0, 0, 255), 2, 8); //绘制轮廓
int x0=, y0=, w0=, h0=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形
drawContours(dstImg, contours, i, Scalar(, , ), , ); //绘制轮廓
x0 = boundRect[i].x; //获得第i个外接矩形的左上角的x坐标
y0 = boundRect[i].y; //获得第i个外接矩形的左上角的y坐标
w0 = boundRect[i].width; //获得第i个外接矩形的宽度
h0 = boundRect[i].height; //获得第i个外接矩形的高度
rectangle(dstImg, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(, , ), , ); //绘制第i个外接矩形
}
imshow("boundRect", dstImg);
waitKey();
}

三、分割硬币轮廓并计数

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace cv;
using namespace std;
void main()
{
//分割硬币轮廓
Mat srcImg =imread("E://33.png");
imshow("src", srcImg);
Mat dstImg = srcImg.clone(); //原图备份
cvtColor(srcImg, srcImg, CV_BGR2GRAY); //转为灰度图
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化
Mat element = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -)); //获得结构元素
dilate(srcImg, srcImg, element); //膨胀操作
imshow("dilate",srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
int x0=, y0=, w0=, h0=,num=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形
drawContours(dstImg, contours, i, Scalar(, , ), , ); //绘制轮廓
x0 = boundRect[i].x;
y0 = boundRect[i].y;
w0 = boundRect[i].width;
h0 = boundRect[i].height;
if(w0> && h0>)//筛选
{
rectangle(dstImg, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(, , ), , ); //绘制第i个外接矩形
num++;
}
}
cout<<"硬币数量:"<<num;
imshow("boundRect", dstImg);
waitKey();
}

四、简单车牌字符分隔

 #include "opencv2/opencv.hpp"
using namespace cv;
void main()
{
//---简单车牌字符分隔
Mat srcImg =imread("E://Car2.jpg");
Mat dstImg = srcImg.clone(); //原图备份
medianBlur(srcImg, srcImg, ); //中值滤波
cvtColor(srcImg, srcImg, CV_BGR2GRAY); //转为灰度图
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化
imshow("threshold", srcImg);
imwrite("F://car0.jpg", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CHAIN_APPROX_NONE); //查找所有轮廓
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
int x0=, y0=, w0=, h0=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形
x0 = boundRect[i].x;
y0 = boundRect[i].y;
w0 = boundRect[i].width;
h0 = boundRect[i].height;
if(w0>srcImg.cols/ && w0<srcImg.cols/ && h0>srcImg.rows/ && h0<srcImg.rows*/)
{
char pic_name[];
sprintf(pic_name, "F:\\%d.bmp", i);
Mat ROI = dstImg(Rect(x0, y0, w0, h0));
imwrite(pic_name, ROI);
rectangle(dstImg, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(, , ), , ); //绘制第i个外接矩形
}
}
imshow("boundRect", dstImg);
waitKey();
}

opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形的更多相关文章

  1. python学习之路网络编程篇(第四篇)

    python学习之路网络编程篇(第四篇) 内容待补充

  2. opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点

    一.简介 二.画出每个轮廓的每个点 #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src= ...

  3. opencv学习之路(22)、轮廓查找与绘制(一)

    一.简介 图2 二.代码 #include"opencv2/opencv.hpp" #include<iostream> using namespace std; us ...

  4. opencv学习之路(29)、轮廓查找与绘制(八)——轮廓特征属性及应用

    一.简介 HSV颜色空间(hue色调,saturation饱和度,value亮度) 二.HSV滑动条 #include "opencv2/opencv.hpp" #include ...

  5. opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形

    一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...

  6. opencv学习之路(37)、运动物体检测(二)

    一.运动物体轮廓椭圆拟合及中心 #include "opencv2/opencv.hpp" #include<iostream> using namespace std ...

  7. opencv学习之路(36)、运动物体检测(一)

    一.简介 二.背景减法 图片说明 #include "opencv2/opencv.hpp"using namespace cv; void main() { Mat img1 = ...

  8. opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

    一.点与轮廓的距离及位置关系 #include "opencv2/opencv.hpp" #include <iostream> using namespace std ...

  9. opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓

    一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...

随机推荐

  1. python中的pymongo连接脚本

    author: headsen chen date: 2019-04-12  17:39:12 先安装python3,pymongo [root@localhost mnt]# cat /root/p ...

  2. 面向对象编程之Java多态

    我相信从学习计算机面向对象编程起就很多人背下了继承.封装.多态三个特性,可是多态并不是那么好理解的.通常做几道题,背下几次多态的动态绑定规律,可是依旧在一段时间后忘记了多态的存在,为什么要多态,这个程 ...

  3. vue路由动态加载

    注意:是动态加载不是动态路由 解决的问题: 动态配置菜单栏的路由参数--实现菜单级的权限控制 问题成因: 在vue实例化的时候vuex.vue-router 就需要加载完毕,无法使用异步的方式从服务器 ...

  4. 实现Linux下的ls和ls-l

    ls的C语言代码实现 #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #includ ...

  5. python2,python3同时安装时,python3可以安装并升级pip库,python2报错的解决办法

    最近在使用pip安装包的的时候出现下面错误 UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' 查询资料后发现原因是p ...

  6. mysql 远程登录

    mysql -h 192.168.5.116 -P 3306 -u root -p123456

  7. int bool 字符串 列表 字典 集合

    1.int和bool 输出i的最大二进制位数inti = 1000 print(i.bit_length()) 2. str int bool list set dict  tuple 相互转换 pr ...

  8. oracle 新建用户后赋予的权限语句

    grant create session,resource to itsys; grant create table to itsys;grant resource to itsys;grant cr ...

  9. break,continue的区别

    break 终止循环, continue 跳出本次循环,进入下一次循环 username = 'Loker'passwd = '123456' for i in range(3): user = in ...

  10. iPhone屏蔽IOS更新、iPhone系统更新的提示(免越狱,有效期更新至2021年)

    iPhone屏蔽IOS更新.iPhone系统更新的提示(免越狱,有效期更新至2021年) 1.在Safari浏览器中粘贴如下链接,按提示打开链接. 输入http://apt.dataage.pub 2 ...