一、简介

二、外接矩形的查找绘制

 #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. 【CF666C】Codeword 结论题+暴力

    [CF666C]Codeword 题意:一开始有一个字符串s,有m个事件,每个事件形如: 1.用一个新的字符串t来替换s2.给出n,问有多少个长度为n的小写字母组成的字符串满足包含s作为其一个子序列? ...

  2. mssql for xml path使用

    准备工作: CREATE TABLE [dbo].[Students]( [id] [int] IDENTITY(1,1) NOT NULL, [names] [varchar](50) NULL, ...

  3. 队列Queue、栈LifoQueue、优先级队列PriorityQueue

    队列:队列是先进先出. import queue q = queue.Queue() q.put(1) q.put(2) q.put(3) q.put(4) print(q.get()) print( ...

  4. XML5个转义符:<,>,&,”,©;的转义字符分别如下: &lt; &gt;&amp; &quot; &apos;

    XML5个转义符:<,>,&,”,©;的转义字符分别如下: < >& " &apos;             $search = array ...

  5. mybatis12--一级缓存

    验证一级缓存的存在 对应的实体类 /** *学生对应的实体类 */ public class Student { private Integer sId; private String sName; ...

  6. 19. vue的原理

    vue:原理1 => Object.defineProperty 当你把一个普通的 JavaScript 对象传给 Vue 实例的 data 选项,Vue 将遍历此对象所有的属性,并使用 Obj ...

  7. linux命令sync,shutdown

    1.数据同步写入磁盘: sync 输入sync,那举在内存中尚未被更新的数据,就会被写入硬盘中 hling@hling:~$ sync   2.惯用的关机指令:shutdown 实例:

  8. PAT甲级1135 Is It A Red-Black Tree?【dfs】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805346063728640 题意: 给定一棵二叉搜索树的先序遍历结 ...

  9. ML.NET 0.10特性简介

    IDataView被单独作为一个类库包 IDataView组件为表格式数据提供了非常高效的处理方式,尤其是用于机器学习和高级分析应用.它被设计为可以高效地处理高维数据和大型数据集.并且也适合处理属于更 ...

  10. Codeforces 455A - Boredom - [DP]

    题目链接:https://codeforces.com/problemset/problem/455/A 题意: 给出一个 $n$ 个数字的整数序列 $a[1 \sim n]$,每次你可以选择一个 $ ...