一、简介

二、轮廓最小外接矩形的绘制

 #include "opencv2/opencv.hpp"
using namespace cv; void main()
{
//轮廓最小外接矩形的绘制
Mat srcImg = imread("E://00.png");
Mat dstImg = srcImg.clone();
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY); //二值化
imshow("threshold", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
vector<RotatedRect> box(contours.size()); //定义最小外接矩形集合
Point2f rect[];
for(int i=; i<contours.size(); i++)
{
box[i] = minAreaRect(Mat(contours[i])); //计算每个轮廓最小外接矩形
boundRect[i] = boundingRect(Mat(contours[i]));
circle(dstImg, Point(box[i].center.x, box[i].center.y), , Scalar(, , ), -, ); //绘制最小外接矩形的中心点
box[i].points(rect); //把最小外接矩形四个端点复制给rect数组
rectangle(dstImg, Point(boundRect[i].x, boundRect[i].y), Point(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height), Scalar(, , ), , );
for(int j=; j<; j++)
{
line(dstImg, rect[j], rect[(j+)%], Scalar(, , ), , ); //绘制最小外接矩形每条边
}
}
imshow("dst", dstImg);
waitKey();
}

三、粗略计算物体像素长宽

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("E://cup.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
medianBlur(srcImg, srcImg, );
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY_INV); //INV是因为背景白色,物体黑色,需要反转一下
imshow("threshold", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy; findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cout<<"num="<<contours.size()<<endl;
vector<Rect> boundRect(contours.size());
vector<RotatedRect> box(contours.size());
Point2f rect[];
for(int i=; i<contours.size(); i++)
{
box[i] = minAreaRect(Mat(contours[i]));
boundRect[i] = boundingRect(Mat(contours[i]));
cout<<box[i].angle<<endl;
cout<<box[i].center<<endl;
cout<<box[i].size.width<<endl;
cout<<box[i].size.height<<endl;
circle(dstImg, Point(box[i].center.x, box[i].center.y), , Scalar(, , ), -, ); //绘制外接矩形和 最小外接矩形(for循环)
rectangle(dstImg, Point(boundRect[i].x, boundRect[i].y), Point(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height), Scalar(, , ), , );
box[i].points(rect);//把最小外接矩形四个端点复制给rect数组
for(int j=; j<; j++)
{
line(dstImg, rect[j], rect[(j+)%], Scalar(, , ), , );
} char width[], height[];
sprintf(width, "width=%0.2f", box[i].size.width);
sprintf(height, "height=%0.2f", box[i].size.height);
putText(dstImg, width, Point(, ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.85, Scalar(, , ));
putText(dstImg, height, Point(, ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.85, Scalar(, , )); }
imshow("dst", dstImg);
waitKey();
}

四、倾斜物体矫正提取

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("E://qrcode.jpg");
imshow("src", srcImg);
Mat dstImg = srcImg.clone();
GaussianBlur(srcImg, srcImg, Size(, ), , );
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
Canny(srcImg, srcImg, , );//因为原图比较复杂,所以需要将canny的值调大,去除不想要的成分
//threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY_INV); //二值化也可以实现canny效果,不过在本例中杂絮较多
imshow("canny", srcImg);
Mat element = getStructuringElement(MORPH_RECT, Size(, ), Point(-, -)); //定义结构元素
dilate(srcImg, srcImg, element); //膨胀
imshow("dilate", srcImg);
erode(srcImg, srcImg, element);
imshow("erode", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
vector<Rect> boundRect(contours.size());
vector<RotatedRect> box(contours.size());
Point2f rect[];
for(int i=; i<contours.size(); i++)
{
box[i] = minAreaRect(Mat(contours[i]));
boundRect[i] = boundingRect(Mat(contours[i])); if(box[i].size.width < || box[i].size.height<)//筛选
continue;
rectangle(dstImg, Point(boundRect[i].x, boundRect[i].y), Point(boundRect[i].x + boundRect[i].width, boundRect[i].y + boundRect[i].height), Scalar(, , ), , );
circle(dstImg, Point(box[i].center.x, box[i].center.y), , Scalar(, , ), -, );
box[i].points(rect);
for(int j=; j<; j++)
{
line(dstImg, rect[j], rect[(j+)%], Scalar(, , ), , );
} float angle;
cout<<"angle="<<box[i].angle<<endl;
angle = box[i].angle;
char width[], height[];
sprintf(width, "width=%0.2f", box[i].size.width);
sprintf(height, "height=%0.2f", box[i].size.height);
putText(dstImg, width, Point(, ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.85, Scalar(, , ));
putText(dstImg, height, Point(, ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.85, Scalar(, , ));
imshow("temp", dstImg); //利用仿射变换进行旋转 另一种方法,透视变换
if (< abs(angle) && abs(angle)<=)
angle = angle;//负数,顺时针旋转
else if (< abs(angle) && abs(angle)<)
angle = - abs(angle);//正数,逆时针旋转
Point2f center = box[i].center; //定义旋转中心坐标
double angle0 = angle;
double scale = ;
Mat roateM = getRotationMatrix2D(center, angle0, scale); //获得旋转矩阵,顺时针为负,逆时针为正
warpAffine(dstImg, dstImg, roateM, dstImg.size()); //仿射变换 //保存二维码
int x0=, y0=, w0=, h0=;
x0 = boundRect[i].x;
y0 = boundRect[i].y;
w0 = boundRect[i].width;
h0 = boundRect[i].height;
Mat ROI = dstImg(Rect(x0, y0, w0, h0));
imwrite("F://1.jpg", ROI);
}
imshow("dst", dstImg);
waitKey();
}

opencv学习之路(26)、轮廓查找与绘制(五)——最小外接矩形的更多相关文章

  1. Opencv绘制最小外接矩形、最小外接圆

    Opencv中求点集的最小外结矩使用方法minAreaRect,求点集的最小外接圆使用方法minEnclosingCircle. minAreaRect方法原型: RotatedRect minAre ...

  2. Opencv 最小外接矩形合并拼接

    前一篇画出了最小外接矩形,但是有时候画出来的矩形由于中间像素干扰或者是其他原因矩形框并不是真正想要的 如图1是一个信号的雨图,被矩形框分割成了多个小框: 需要合并矩形框达到的效果: 主要思想: 扫描两 ...

  3. Opencv 图片边缘检测和最小外接矩形

    #include "core/core.hpp" #include "highgui/highgui.hpp" #include "imgproc/i ...

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 1.9flask sqlalchemy和wtforms

    2019-1-9 15:28:07 还有2天视频flask结束,然后到爬虫了 发现好快 学的东西好多! 到时候来个综合整理!!!! 越努力,越幸运!!! sqlalchemy 参考连接: https: ...

  2. 第三天 Linux简单命令

    2018-5-22 15:21:59 使用 atom 可以在windows环境下同步代码与linux (汉化配置好就可以啦) 2018-4-13 18:09:31  该看32节啦 1.man +陌生命 ...

  3. js substr和substring

    substr(start[,end]) 字符串截取 start从那里裁,end裁切为数 substring(start[,end = str.length]) // start和end会先处理数值较大 ...

  4. js 判断当前操作系统是ios还是android还是电脑端

    js判断客户端是否是IOS或者是Android //如果返回true 则说明是Android function is_weixin() { var ua = window.navigator.user ...

  5. python 3 往Excel 中的写入内容但不覆盖原内容

    EXCEL 写入数据保持原样式 import xlwt import xlrd import xlutils import xlutils.copy class ExcelHandle(): def ...

  6. python全栈开发 * 03 基本数据类型 * 180601

    python基本数据类型  ( int , bool , str ) 一  python基本数据类型    (一)int ==> 整数.进行数学运算     (二)str ==> 字符串. ...

  7. Codeforces 1090A - Company Merging - [签到水题][2018-2019 Russia Open High School Programming Contest Problem A]

    题目链接:https://codeforces.com/contest/1090/problem/A A conglomerate consists of n companies. To make m ...

  8. Luogu 1068 - 分数线划定 - [快速排序]

    题目链接:https://www.luogu.org/problemnew/show/P1068 题目描述世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对所有报名的选手 ...

  9. postgresql 9源码安装

    安装过程: 1.configuration --prefix=PREFIX install all files under the directory PREFIX instead of usr/lo ...

  10. webpack介绍 安装 常用命令

    Webpack是一款用户打包前端模块的工具,它将根据模块的依赖关系进行静态分析,然后将这些模块按照指定的规则生成对应的静态资源.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑 ...