1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 【学习OpenCV】给轮廓画出矩形和圆形边界

【学习OpenCV】给轮廓画出矩形和圆形边界

时间:2019-01-07 04:15:25

相关推荐

【学习OpenCV】给轮廓画出矩形和圆形边界

OpenCV支持大量的轮廓、边缘、边界的相关函数,相应的函数有moments、HuMoments、findContours、drawContours、approxPolyDP、arcLength、boundingRect、contourArea、convexHull、fitEllipse、fitLine、isContourConvex、minAreaRect、minEnclosingCircle、mathcShapes、pointPolygonTest。

目标

在本教程中,您将学习如何:

使用OpenCV函数cv :: boundingRect使用OpenCV函数cv :: minEnclosingCircle

Code

本教程代码如下所示。您也可以从这里下载

下面这个程序用到的函数有,简单介绍其功能如下:

findContours:找到图像中轮廓

approxPolyDP:对多边形曲线做近似

boundingRect:计算并返回包围轮廓点集的最小矩形

minEnclosingCircle:计算并返回包围轮廓点集的最小圆形及其半径

drawContours:根据轮廓点集和轮廓结构画出轮廓

程序代码及详细注释:

#include "stdafx.h"#include "opencv2/highgui/highgui.hpp"#include "opencv2/imgproc/imgproc.hpp"#include <iostream>#include <stdio.h>#include <stdlib.h>using namespace cv;using namespace std;Mat src; Mat src_gray;int thresh = 100;int max_thresh = 255;RNG rng(12345);/// 函数声明void thresh_callback(int, void* );int main( int argc, char** argv ){/// 载入原图像, 返回3通道图像src = imread( "boundrect.jpg" );/// 转化成灰度图像并进行平滑cvtColor( src, src_gray, CV_BGR2GRAY );blur( src_gray, src_gray, Size(3,3) );/// 创建窗口char* source_window = "Source";namedWindow( source_window, CV_WINDOW_AUTOSIZE );imshow( source_window, src );createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );thresh_callback( 0, 0 );waitKey(0);return(0);}void thresh_callback(int, void* ){Mat threshold_output;vector<vector<Point> > contours; //轮廓数组(非矩形数组),每个轮廓是一个Point型的vectorvector<Vec4i> hierarchy; //见下面findContours的解释/// 使用Threshold二值threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );/// 找到轮廓//contours参数为检测的轮廓数组,每一个轮廓用一个point类型的vector表示//hiararchy参数和轮廓个数相同,每个轮廓contours[ i ]对应4个hierarchy元素hierarchy[ i ][ 0 ] ~hierarchy[ i ][ 3 ],//分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,该值设置为负数。//CV_RETR_TREE:建立一个等级树结构的轮廓//findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );/// 多边形逼近轮廓 + 获取矩形和圆形边界框vector<vector<Point> > contours_poly( contours.size() );//近似后的轮廓点集vector<Rect> boundRect( contours.size() ); //包围点集的最小矩形vectorvector<Point2f>center( contours.size() ); //包围点集的最小圆形vectorvector<float>radius( contours.size() );//包围点集的最小圆形半径vectorfor( int i = 0; i < contours.size(); i++ ){approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );//对多边形曲线做适当近似,contours_poly[i]是输出的近似点集boundRect[i] = boundingRect( Mat(contours_poly[i]) ); //计算并返回包围轮廓点集的最小矩形minEnclosingCircle( contours_poly[i], center[i], radius[i] );//计算并返回包围轮廓点集的最小圆形及其半径}/// 画多边形轮廓 + 包围的矩形框 + 圆形框Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );for( int i = 0; i< contours.size(); i++ ){Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); //随机颜色// drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );drawContours( drawing, contours_poly, i, color, 1, 8, hierarchy, 0, Point() ); //根据轮廓点集contours_poly和轮廓结构hierarchy画出轮廓rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 ); //画矩形,tl矩形左上角,br右上角circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );//画圆形}/// 显示在一个窗口namedWindow( "Contours", CV_WINDOW_AUTOSIZE );imshow( "Contours", drawing );}

说明

主要功能相当简单,如下所述:

打开图像,将其转换为灰度,并将其模糊以摆脱噪点。

src = imread( argv[1], IMREAD_COLOR );cvtColor( src, src_gray, COLOR_BGR2GRAY );blur( src_gray, src_gray, Size(3,3) );

创建一个标题为“Source”的窗口,并在其中显示源文件。

const char* source_window = "Source";namedWindow( source_window, WINDOW_AUTOSIZE );imshow( source_window, src );

在source_window上创建一个跟踪栏,并为其分配一个回调函数。一般来说,回调函数用于对某种信号做出反应,在我们的例子中它是跟踪栏的状态变化。

createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );

显式的一次性电话thresh_callback是必要的同时显示“轮廓”窗口与“源”窗口。

thresh_callback( 0, 0 );

等待用户关闭窗口。

waitKey(0);

回调函数thresh_callback执行所有有趣的工作。

写入threshold_output灰度图片的阈值(您可以在这里查看阈值)。

threshold(src_gray,threshold_output,thresh,255,THRESH_BINARY);

找到轮廓并将其保存到向量contourhierarchy

findContours(threshold_output,contour,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point(0,0));

对于每个找到的轮廓,我们现在将逼近逼近具有精度±3的多边形,并表示曲线必须关闭。

之后,我们为每个多边形找到一个边界,并将其保存boundRect。

最后,我们发现每一个多边形的最小封闭圈,并保存到center和radius载体

for( size_t i = 0; i < contours.size(); i++ ){approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );boundRect[i] = boundingRect( Mat(contours_poly[i]) );minEnclosingCircle( contours_poly[i], center[i], radius[i] );}

我们发现了我们需要的一切,我们所要做的就是绘制。

创建新的无符号8位字符的Mat,填充零。它将包含我们要制作的所有图纸(直角和圆圈)。

Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );

对于每个轮廓:选择随机颜色,绘制轮廓,边界矩形和最小包围圆,

for( size_t i = 0; i< contours.size(); i++ ){Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );}

显示结果:创建一个新窗口“轮廓”,并显示我们添加到图纸上的所有内容。

namedWindow( "Contours", WINDOW_AUTOSIZE );imshow( "Contours", drawing );

结果 运行结果:

轮廓查找与绘制——正外接矩形

转载/little-monkey/p/7429381.html

一、简介

二、外接矩形的查找绘制

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

三、分割硬币轮廓并计数

1 #include "opencv2/opencv.hpp" 2 #include<iostream> 3 using namespace cv; 4 using namespace std; 5 void main() 6 { 7//分割硬币轮廓 8Mat srcImg =imread("E://33.png"); 9imshow("src", srcImg);10Mat dstImg = srcImg.clone(); //原图备份11cvtColor(srcImg, srcImg, CV_BGR2GRAY); //转为灰度图12threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY); //二值化13Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1)); //获得结构元素14dilate(srcImg, srcImg, element); //膨胀操作15imshow("dilate",srcImg);16 17vector<vector<Point>> contours; 18vector<Vec4i> hierarcy;19findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓20vector<Rect> boundRect(contours.size()); //定义外接矩形集合21int x0=0, y0=0, w0=0, h0=0,num=0;22for(int i=0; i<contours.size(); i++)23{24 boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形25 drawContours(dstImg, contours, i, Scalar(0, 0, 255), 2, 8); //绘制轮廓26 x0 = boundRect[i].x; 27 y0 = boundRect[i].y; 28 w0 = boundRect[i].width; 29 h0 = boundRect[i].height; 30 if(w0>30 && h0>30)//筛选31 {32 rectangle(dstImg, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形33 num++;34 }35}36cout<<"硬币数量:"<<num;37imshow("boundRect", dstImg);38waitKey(0); 39 }

四、简单车牌字符分隔

1 #include "opencv2/opencv.hpp" 2 using namespace cv; 3 void main() 4 { 5//---简单车牌字符分隔 6Mat srcImg =imread("E://Car2.jpg"); 7Mat dstImg = srcImg.clone(); //原图备份 8medianBlur(srcImg, srcImg, 5); //中值滤波 9cvtColor(srcImg, srcImg, CV_BGR2GRAY); //转为灰度图10threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY); //二值化11imshow("threshold", srcImg);12imwrite("F://car0.jpg", srcImg);13 14vector<vector<Point>> contours; 15vector<Vec4i> hierarcy;16findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CHAIN_APPROX_NONE); //查找所有轮廓17vector<Rect> boundRect(contours.size()); //定义外接矩形集合18int x0=0, y0=0, w0=0, h0=0;19for(int i=0; i<contours.size(); i++)20{21 boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形22 x0 = boundRect[i].x; 23 y0 = boundRect[i].y; 24 w0 = boundRect[i].width; 25 h0 = boundRect[i].height; 26 if(w0>srcImg.cols/12 && w0<srcImg.cols/5 && h0>srcImg.rows/6 && h0<srcImg.rows*5/6)27 {28 char pic_name[10];29 sprintf(pic_name, "F:\\%d.bmp", i);30 Mat ROI = dstImg(Rect(x0, y0, w0, h0));31 imwrite(pic_name, ROI);32 rectangle(dstImg, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形33 }34}35imshow("boundRect", dstImg);36waitKey(0);37 }

灰度图上进行透明彩色绘制

为了提取有效的信息并且显示,需要将有效区域用其他颜色画出来

使用opencv circle函数

首先将单通道灰度图转化成3通道的彩色图像

实例如下:

// read imagecv::Mat img_gray = imread(path,0);// create 8bit color image. IMPORTANT: initialize image otherwise it will result in 32Fcv::Mat img_rgb(img_gray.size(), CV_8UC3);// convert grayscale to color imagecv::cvtColor(img_gray, img_rgb, CV_GRAY2RGB);circle(img_rgb, Point(100,100), 30, Scalar(100, 250, 100), -1);//Point圆心中心点,30,半斤,Scalar画笔的参数,-1表示画成实心状态imshow("test", img_rgb);

接下来我们如何能让画的圈为透明的,不遮挡重点显示的区域呢

接下来我们要使用addWeighted函数来实现这个功能,通过实现两张图片进行叠加

Mat overlay;rgb_img.copyTo(overlay);circle(overlay, p, 50, Scalar(100, 250, 100), -1);addWeighted(overlay, 0.3, img_rgb, 0.7, 0, img_rgb);imshow("test", img_rgb);waitKey();

最后实现的效果如下:

16位图片转化成为8位图形---convertScaleAbs函数

在将RealSense提取的深度图片进行显示时,由于是16位图片,想将图片转化成为8位图形进行显示

Opencv中有一个函数convertScaleAbs可以实现这种功能

C++: void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)

Parameters:

src: input array

dst: output array

alpha: optional scale factor

beta: optional delta added to the scaled values

the governmental definition for the function is :

On each element of the input array, the function covertScaleAbs performs three operations sequentially: scaling, taking an absolute value, conversion to an unsigned 8-bit type:

接下来示例代码:

cv::Mat map = cv::imread("C:/Users/Lee_gc/Desktop/try.png", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);//read a 16bits depth picturesdouble min;double max;cv::minMaxIdx(map, &min, &max);cv::Mat adjMap();cv::convertScaleAbs(map, adjMap, 255 / max);imshow("8bitPic", adjMap);//the picture can be showed and converted to 8bits picwaitKey();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。