OPENCV实例:车牌检测

#include <opencv2/opencv.hpp>
#include<vector>
#include <iostream>
#include <math.h>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

Mat src_img,dst_img;
Mat ResizePhoto(Mat , int);
vector<vector<Point>> predict(Mat);
void GetCardPlace(Mat, vector<vector<Point>>);
void GetHSV(Mat);
int main()
{

	src_img = imread("he.png");
	Mat mysrc;
	if (src_img.empty())
	{
		printf("could not load the image...\n");
		return -1;
	}
	 predict(src_img);
	waitKey(0);


	return 0;
}
Mat ResizePhoto(Mat InputeImg,int Max=500)
{
	Mat img = InputeImg;
	int row = img.rows;//行数,宽
	int clos = img.cols;//列数,长
	//cout << "图片宽度" << row << endl;
	if (row > Max)
	{
		float change;
		change = (float)Max / row;
		resize(img, img, Size(), change, change);
	}
	imwrite("car2-c.jpg", img);
	return img;


}


vector<vector<Point>> predict(Mat InputeImg)
{
	Mat gray,open,img_thresh, img_edge, img_edge1, img_edge2 ,element,equal;
	Mat img_copy = InputeImg.clone();
	cvtColor(img_copy, gray, COLOR_BGR2GRAY);
	GaussianBlur(gray, gray, Size(5, 5),0,0);
	//imshow("GaussianBlur", gray);

	element = getStructuringElement( MORPH_RECT, Size(10,10) );
	morphologyEx(gray, open, MORPH_TOPHAT, element);
	threshold(open, img_thresh, 0, 255, THRESH_BINARY + THRESH_OTSU);//+ THRESH_OTSU
	 element = getStructuringElement(MORPH_RECT, Size(40,15));
	 morphologyEx(img_thresh, img_edge1, MORPH_CLOSE, element);
	 //imshow("先闭", img_edge1);
	 element = getStructuringElement(MORPH_RECT, Size(15, 15));
	 morphologyEx(img_edge1, img_edge2, MORPH_OPEN, element);
	 imshow("后开", img_edge2);
	vector<vector<Point>> contours;
	vector<Vec4i> hireachy;
	findContours(img_edge2, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
	for (int i = 0; i < contours.size(); i++)
	{
		Scalar color(rand() & 255, rand() & 255, rand() & 255);
		vector<Rect> boundRect(contours.size());
		boundRect[i] = boundingRect(Mat(contours[i]));
		
	    float m = float(boundRect[i].width) / float( boundRect[i].height);
		if(m>3&&m<5)
		rectangle(img_copy, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
	}
	imshow("  ", img_copy);
	//cout << "contoursNum=" << contours.size()<<endl;
	return contours;

}

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值