OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形

 

【OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形

标签: OpenCV图像处理
2017-04-01 14:35 945人阅读 评论(2) 收藏 举报
OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形 分类:

轮廓特征属性及应用(四)——正外接矩形

1.轮廓正外接矩形——boundingRect()

2.完成了三个小应用:正外接矩形的查找绘制、分割硬币轮廓、简单车牌字符分割

先上ppt:

OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形


OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形


OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形


OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形

代码:

正外接矩形的查找绘制:

[cpp] view plain copy
  1. ///正外接矩形的查找绘制  
  2. #include "opencv2/opencv.hpp"  
  3. using namespace cv;  
  4. #include <iostream>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     //1.查找轮廓  
  9.     //1.1查找轮廓前的预处理(灰度图,阈值化)  
  10.     Mat srcImg = imread("12.jpg",CV_LOAD_IMAGE_COLOR);  
  11.     imshow("srcImg", srcImg);  
  12.     Mat copyImg = srcImg.clone();  
  13.     cvtColor(srcImg,srcImg,CV_BGR2GRAY);  
  14.     threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);  
  15.     imshow("threshold",srcImg);  
  16.     vector <vector<Point>> contours;  
  17.     vector<Vec4i> hierarcy;//没用到  
  18.     //1.2查找轮廓  
  19.     findContours(srcImg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//最外层轮廓  
  20.     //1.3绘制所有轮廓  
  21.     drawContours(copyImg,contours,-1,Scalar(0,255,0),1,8);  
  22.     //2.由轮廓确定正外接矩形  
  23.     int width = 0;  
  24.     int height = 0;  
  25.     int x = 0;  
  26.     int y = 0;  
  27.     //2.1 定义Rect类型的vector容器boundRect存放正外接矩形,初始化大小为contours.size()即轮廓个数  
  28.     vector<Rect> boundRect(contours.size());  
  29.     //2.2 遍历每个轮廓  
  30.     for (int i = 0; i < contours.size(); i++)  
  31.     {  
  32.         //2.3由轮廓(点集)确定出正外接矩形  
  33.         boundRect[i] = boundingRect(Mat(contours[i]));  
  34.         //2.4获得正外接矩形的左上角坐标及宽高  
  35.         width = boundRect[i].width;  
  36.         height = boundRect[i].height;  
  37.         x = boundRect[i].x;  
  38.         y = boundRect[i].y;  
  39.         //2.5用画矩形方法绘制正外接矩形  
  40.         rectangle(copyImg,Rect(x,y,width,height),Scalar(0,0,255),1,8);  
  41.     }  
  42.     imshow("轮廓和正外接矩形", copyImg);  
  43.     waitKey(0);  
  44.     return 0;  
  45. }  

运行结果:

OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形


分割硬币轮廓:

[cpp] view plain copy
  1. ///分割硬币轮廓  
  2. #include "opencv2/opencv.hpp"  
  3. using namespace cv;  
  4. #include <iostream>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     //1.查找轮廓  
  9.     //1.1查找轮廓前的预处理(灰度图,阈值化)  
  10.     Mat srcImg = imread("33.png",CV_LOAD_IMAGE_COLOR);  
  11.     imshow("srcImg", srcImg);  
  12.     Mat copyImg = srcImg.clone();  
  13.     cvtColor(srcImg,srcImg,CV_BGR2GRAY);  
  14.     threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);  
  15.     imshow("threshold",srcImg);  
  16.     //*1.2增加了膨胀和腐蚀  
  17.     //因为有一个轮廓有断点,导致外接矩形是两个小的而不是一个整的,故要膨胀,将断点连起来  
  18.     //1.2.1定义kernel  
  19.     Mat kernel = getStructuringElement(MORPH_RECT,Size(5,5),Point(-1,-1));  
  20.     //1.2.2膨胀  
  21.     dilate(srcImg,srcImg,kernel,Point(-1,-1));  
  22.     //1.2.3腐蚀  
  23.     erode(srcImg,srcImg,kernel,Point(-1,-1));  
  24.     imshow("膨胀和腐蚀", srcImg);  
  25.     //1.3查找轮廓  
  26.     vector <vector<Point>> contours;  
  27.     vector<Vec4i> hierarcy;//没用到  
  28.     findContours(srcImg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//最外层轮廓  
  29.     //1.4绘制所有轮廓  
  30.     drawContours(copyImg,contours,-1,Scalar(0,255,0),1,8);  
  31.     //2.由轮廓确定正外接矩形  
  32.     int width = 0;  
  33.     int height = 0;  
  34.     int x = 0;  
  35.     int y = 0;  
  36.     //2.1 定义Rect类型的vector容器boundRect存放正外接矩形,初始化大小为contours.size()即轮廓个数  
  37.     vector<Rect> boundRect(contours.size());  
  38.     //2.2 遍历每个轮廓  
  39.     for (int i = 0; i < contours.size(); i++)  
  40.     {  
  41.         //2.3由轮廓(点集)确定出正外接矩形  
  42.         boundRect[i] = boundingRect(Mat(contours[i]));  
  43.         //2.4获得正外接矩形的左上角坐标及宽高  
  44.         width = boundRect[i].width;  
  45.         height = boundRect[i].height;  
  46.         x = boundRect[i].x;  
  47.         y = boundRect[i].y;  
  48.         //*2.5 对正外接矩形进行筛选(过滤掉小的)  
  49.         if (width>40 && height > 40)  
  50.         {  
  51.             //2.6用画矩形方法绘制正外接矩形  
  52.             rectangle(copyImg, Rect(x, y, width, height), Scalar(0, 0, 255), 1, 8);  
  53.         }  
  54.     }  
  55.     imshow("轮廓和正外接矩形", copyImg);  
  56.     waitKey(0);  
  57.     return 0;  
  58. }  

运行结果:

OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形


简单车牌字符分割:

[cpp] view plain copy
  1. ///简单车牌字符分割  
  2. #include "opencv2/opencv.hpp"  
  3. using namespace cv;  
  4. #include <iostream>  
  5. using namespace std;  
  6. int main()  
  7. {  
  8.     //1.查找轮廓  
  9.     //1.1查找轮廓前的预处理(灰度图,阈值化)  
  10.     Mat srcImg = imread("Car.jpg", CV_LOAD_IMAGE_COLOR);  
  11.     imshow("srcImg", srcImg);  
  12.     Mat copyImg = srcImg.clone();  
  13.     cvtColor(srcImg, srcImg, CV_BGR2GRAY);  
  14.     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);  
  15.     imshow("threshold", srcImg);  
  16.     //1.2查找轮廓  
  17.     vector <vector<Point>> contours;  
  18.     vector<Vec4i> hierarcy;//没用到  
  19.     findContours(srcImg, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);//所有轮廓  
  20.     //1.3绘制所有轮廓  
  21.     drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 1, 8);  
  22.     //2.由轮廓确定正外接矩形  
  23.     int width = 0;  
  24.     int height = 0;  
  25.     int x = 0;  
  26.     int y = 0;  
  27.     //2.1 定义Rect类型的vector容器boundRect存放正外接矩形,初始化大小为contours.size()即轮廓个数  
  28.     vector<Rect> boundRect(contours.size());  
  29.     //2.2 遍历每个轮廓  
  30.     for (int i = 0; i < contours.size(); i++)  
  31.     {  
  32.         //2.3由轮廓(点集)确定出正外接矩形  
  33.         boundRect[i] = boundingRect(Mat(contours[i]));  
  34.         //2.4获得正外接矩形的左上角坐标及宽高  
  35.         width = boundRect[i].width;  
  36.         height = boundRect[i].height;  
  37.         x = boundRect[i].x;  
  38.         y = boundRect[i].y;  
  39.         //*2.5 对正外接矩形进行筛选(过滤掉过小及过大的)  
  40.         if (width>(1.0 / 12)*srcImg.cols && width<(1.0 / 7)*srcImg.cols   
  41.              &&height >(1.0 / 6)*srcImg.rows&& height < (5.0 / 6)*srcImg.rows)  
  42.         {  
  43.             //2.6用画矩形方法绘制正外接矩形  
  44.             rectangle(copyImg, Rect(x, y, width, height), Scalar(0, 0, 255), 1, 8);  
  45.             //*2.7 通过ROI保存分割出的车牌字符  
  46.             //2.7.1 定义ROI  
  47.             Mat ROI = copyImg(Rect(x,y,width,height));  
  48.             //2.7.2 通过sprintf格式化文件存储名name  
  49.             char name[20] = {0};  
  50.             sprintf(name,"E:\\temp\\%d.jpg",i);  
  51.             //2.7.3 写ROI到本地  
  52.             imwrite(name,ROI);  
  53.         }  
  54.     }  
  55.     imshow("轮廓和正外接矩形", copyImg);  
  56.     waitKey(0);  
  57.     return 0;  
  58. }  

运行结果:

OpenCV学习笔记】二十七、轮廓特征属性及应用(四)——正外接矩形