// asw.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <opencv2/opencv.hpp> using namespace cv; using namespace std; int main() { Mat srcImage = imread("C:/Users/Administrator/Desktop/车牌识别/车牌图像库/1.jpg"); //DrawEllipse(img,(10,20),(30,20),0,360,2,8); Mat dstImage; imshow("原图",srcImage); int rowNumber = srcImage.rows; int colNumber = srcImage.cols; Mat imageRIO = srcImage(Rect(50,50,100,000)); //imshow("3",imageRIO); imwrite("图3.jpg",imageRIO); Mat imgGray; cvtColor(srcImage,dstImage,CV_BGR2GRAY); imshow("灰度图.jpg",dstImage); imgGray = dstImage; //边缘检测 Sobel Laplacian Canny 其中Canny算子只能处理(8位)灰度图,其余两种8位32位都可以 Mat grad_x,grad_y; Sobel(imgGray,grad_x,CV_8U,1,0,3,1,1);//X方向上的Sobel算子检测,其中3,1,0都是默认值 imshow("Sobel算子X方向检测图",grad_x); Sobel(imgGray,grad_y,CV_8U,0,1,3,1,0);//Y方向上的Sobel算子检测,其中3,1,0都是默认值 imshow("Sobel算子Y方向检测图",grad_y); addWeighted(grad_x,0.5,grad_y,0.5,0,dstImage);//合并梯度 imshow("整体方向Sobel图",dstImage); Laplacian(imgGray,dstImage,CV_8U); imshow("laplacian算子检测图",dstImage); Canny(imgGray,dstImage,50,200,3);//50和200表示第一个滞后性阈值和第二个滞后性阈值,较小者用于边缘连接,较大者控制强边缘的初始段,达阈值opnecv推荐为小阈值的3倍; //3表示应用的Sobel算子的孔径大小 有默认值为3; imshow("Caany算子检测图",dstImage); //waitKey(0); // 霍夫变换 hough vector<Vec2f> lines;//定义一个矢量结构lines用于存放得到的线段矢量集合 HoughLines(dstImage,lines,1,CV_PI/180,150); //依次在图中绘制出每条线段 for (size_t i = 0;i < lines.size();i++) { float rho = lines[i][0],theta = lines[i][1]; Point pt1,pt2; double a = cos(theta),b = sin(theta); double x0 = rho*a,y0 = rho*b;//A是与直线垂直的线交点 坐标为(x0,y0)=(rho*cos(theta),rho*sin(theta)); //向上取整函数cvCeil、向下取整函数cvFloor、四舍五入函数cvRound; pt1.x = cvRound(x0+1000*(-b));//1000是取两点之间的距离,可操控量; pt1.y = cvRound(y0+1000*(a));//pt1是位于A较上的一个点; pt2.x = cvRound(x0-1000*(-b));//pt2是位于A较下的一个点; pt2.y = cvRound(y0-1000*(a)); line(dstImage,pt1,pt2,Scalar(55,100,195),1,CV_AA); } imshow("hough检测直线图",dstImage); //waitKey(0); // 寻找轮廓 只处理8位 即灰度图像 vector<vector<Point>> contours; findContours(imgGray,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE); drawContours(dstImage,contours,-1,Scalar(0),3); imshow("轮廓图",dstImage); waitKey(0); //阈值化操作 threshold(srcImage,dstImage,100,255,3); imshow("固定阈值化图像",dstImage); adaptiveThreshold(imgGray,dstImage,255,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,3,1); imshow("自适应阈值化图像",dstImage); waitKey(0); // resize函数实现 resize(srcImage,dstImage,Size(),0.5,0.5);//缩小为一半 imshow("缩放1/2图",dstImage); resize(srcImage,dstImage,Size(),2,2);//放大2倍 imshow("放大2倍图",dstImage); resize(srcImage,dstImage,Size(srcImage.cols*3,srcImage.rows*3));//放大3倍 imshow("放大3倍图",dstImage); //waitKey(0); // 金字塔函数实现 pyrUp(srcImage,dstImage,Size(srcImage.cols*2,srcImage.rows*2));// 放大2倍 imshow("金字塔放大2倍图",dstImage); pyrDown(srcImage,dstImage,Size(srcImage.cols/2,srcImage.rows/2));// 缩小2倍 imshow("金字塔缩小2倍图",dstImage); waitKey(0); //漫水填充算法 Rect ccomp; floodFill(srcImage,Point(50,300),Scalar(155,255,55),&ccomp,Scalar(20,20,20),Scalar(20,20,20)); imshow("漫水填充图",srcImage); //膨胀腐蚀 Mat element = getStructuringElement(MORPH_RECT,Size(15,15)); erode(srcImage,dstImage,element); imshow("腐蚀图",dstImage); dilate(srcImage,dstImage,element); imshow("膨胀图",dstImage); waitKey(0); //滤波 boxFilter(srcImage,dstImage,-1,Size(3,3)); imshow("方框滤波图",dstImage); blur(srcImage,dstImage,Size(3,3)); imshow("均值滤波图",dstImage); GaussianBlur(srcImage,dstImage,Size(5,7),1,1); imshow("高斯滤波图",dstImage); waitKey(0); cvtColor(srcImage,dstImage,CV_BGR2GRAY); imwrite("图1.jpg",srcImage); Mat logoImage = imread("图3.jpg"); if (!logoImage.data) { printf("读取图片失败 \n"); return false; } //定义一个Mat类型并给其设定RIO区域 Mat imageRIO1 = srcImage(Rect(100,200,imageRIO.rows,imageRIO.cols)); Mat mask = imread("原图.jpg"); logoImage.copyTo(imageRIO1,mask); imshow("1",srcImage); imshow("2",dstImage); waitKey(0); return 0; }
时间: 2024-10-11 17:08:18