opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形
一、简介


二、外接矩形的查找绘制
#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)、轮廓查找与绘制(四)——正外接矩形的更多相关文章
- python学习之路网络编程篇(第四篇)
python学习之路网络编程篇(第四篇) 内容待补充
- opencv学习之路(23)、轮廓查找与绘制(二)——访问轮廓每个点
一.简介 二.画出每个轮廓的每个点 #include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src= ...
- opencv学习之路(22)、轮廓查找与绘制(一)
一.简介 图2 二.代码 #include"opencv2/opencv.hpp" #include<iostream> using namespace std; us ...
- opencv学习之路(29)、轮廓查找与绘制(八)——轮廓特征属性及应用
一.简介 HSV颜色空间(hue色调,saturation饱和度,value亮度) 二.HSV滑动条 #include "opencv2/opencv.hpp" #include ...
- opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形
一.简介 二.轮廓最小外接矩形的绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //轮廓最小外 ...
- opencv学习之路(37)、运动物体检测(二)
一.运动物体轮廓椭圆拟合及中心 #include "opencv2/opencv.hpp" #include<iostream> using namespace std ...
- opencv学习之路(36)、运动物体检测(一)
一.简介 二.背景减法 图片说明 #include "opencv2/opencv.hpp"using namespace cv; void main() { Mat img1 = ...
- opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配
一.点与轮廓的距离及位置关系 #include "opencv2/opencv.hpp" #include <iostream> using namespace std ...
- opencv学习之路(27)、轮廓查找与绘制(六)——外接圆、椭圆拟合、逼近多边形曲线、计算轮廓面积及长度、提取不规则轮廓
一.最小外接圆 #include "opencv2/opencv.hpp" #include<iostream> using namespace std; using ...
随机推荐
- PCB画板总结
最近几天完成了第一个PCB电路板.虽然器件不是很多,手动布线了4次才达到自己理想的效果. 但是还是有很多细节只有亲自拿到了自己做的板子,亲自焊接之后,才知道自己哪里不合适. 这是修改了4次之后的最终的 ...
- 1.9flask sqlalchemy和wtforms
2019-1-9 15:28:07 还有2天视频flask结束,然后到爬虫了 发现好快 学的东西好多! 到时候来个综合整理!!!! 越努力,越幸运!!! sqlalchemy 参考连接: https: ...
- codeforces-707 C. Pythagorean Triples
C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input standa ...
- prometheus + grafana安装部署(centos6.8)
官方网址:https://prometheus.io/ GitHub网址:https://github.com/prometheus/prometheus 软件下载地址:https://prometh ...
- Trait基础
Trait基础 在Scala中,Trait是一种特殊概念.首先,Trait可以被作为接口来使用,此时Trait与Java的接口非常类似.同时在Trait可以定义抽象方法,其与抽象类中的抽象方法一样,不 ...
- juqery 点击张三触发李四的方法 trigger(); 和 被选元素前插入指定的内容的方法 brfore();
$('.zc_fabu_img_1').on('click',function(){ $("#upImg img").trigger("click"); }) ...
- Oracle课程档案,第九天
lsnrctl status:查看监听状态 Oracle网络配置三部分组成:客户端,监听,数据库 配置文件:$ vi $ORACLE_HOME/network/admin/listener.ora v ...
- python全栈开发* 02 知识点汇总 * 180531
运算符和编码 一 格式化输出 1 .输入 name ,age , job , hobby. 输出 : --------------- info of Mary ------------ ...
- Linux-003-Resource temporarily unavailable
Jenkins构建任务向服务器发送war包时提示信息如下所示: 由上述信息可知通过SSH命令连接失败.通过Client连接服务器,提示信息如下: 提示信息说明资源暂时不可用. 原因一般是因为用户或应用 ...
- Java连接MySQL报出警告 WARN: Establishing SSL connection without server's identity verification is not recommended.
很多人使用JDBC连接MySQL时报出警告: WARN: Establishing SSL connection without server's identity verification is n ...