#include "recognisebusiness.h"
#include "opencv2/ml.hpp"
using namespace ml;
RecogniseBusiness::RecogniseBusiness()
{
}
RecogniseBusiness::~RecogniseBusiness()
{
}
QImage RecogniseBusiness::disnoising(QImage& image)
{
Mat srcMat=QImage2cvMat(image);
cv::GaussianBlur(srcMat, srcMat, Size(5, 5), 0, 0, BORDER_DEFAULT);
return cvMat2QImage(srcMat);
}
QImage RecogniseBusiness::grey(QImage& image)
{
Mat srcMat=QImage2cvMat(image);
cv::cvtColor(srcMat, srcMat, CV_BGR2GRAY);
return cvMat2QImage(srcMat);
}
QImage RecogniseBusiness::sobel(QImage& image)
{
Mat srcMat=QImage2cvMat(image);
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
//Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
//Gradient X
Sobel( srcMat, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
//Gradient Y
Sobel( srcMat, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
addWeighted( abs_grad_x, 1, abs_grad_y, 0, 0, srcMat );
return cvMat2QImage(srcMat);
}
QImage RecogniseBusiness::binary(QImage& image)
{
Mat srcMat=QImage2cvMat(image);
threshold(srcMat, srcMat, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
return cvMat2QImage(srcMat);
}
QImage RecogniseBusiness::dilateCondition(QImage& image,QImage con)
{
Mat srcMat=QImage2cvMat(image);
Mat conMat=QImage2cvMat(con);
//颜色信息膨胀
Mat element = getStructuringElement(MORPH_RECT, Size(21, 3) );
srcMat=dilateCondition(srcMat,srcMat,conMat,element);
//小范围膨胀
Mat element1 = getStructuringElement(MORPH_RECT, Size(3, 3) );
dilate(srcMat,srcMat,element1);
return cvMat2QImage(srcMat);
}
QImage RecogniseBusiness::erode(QImage& image)
{
Mat srcMat=QImage2cvMat(image);
Mat element2 = getStructuringElement(MORPH_RECT, Size(7, 3) );
cv::erode(srcMat, srcMat, element2);
return cvMat2QImage(srcMat);
}
int RecogniseBusiness::possible(QImage& image,QImage con,vector<QImage>& rv)
{
Mat srcMat=QImage2cvMat(image);
Mat conMat=QImage2cvMat(con);
//寻找可能的车牌轮廓
vector< vector< Point> > contours;
findContours(srcMat,
contours, // a vector of contours
CV_RETR_EXTERNAL, // 提取外部轮廓
CV_CHAIN_APPROX_NONE); // all pixels of each contours
//Start to iterate to each contour founded
vector<vector<Point> >::iterator itc = contours.begin();
vector<RotatedRect> rects;
//Remove patch that are no inside limits of aspect ratio and area.
//筛出倾斜角过大的矩形
float minH=conMat.rows/100;
float minW=conMat.cols/15;
while (itc != contours.end())
{
//Create bounding rect of object
RotatedRect minRect = minAreaRect(Mat(*itc));
//筛选
float r = (float)minRect.size.width / (float)minRect.size.height;
float angle = minRect.angle;
Size rect_size = minRect.size;
if (r < 1)
{
angle = 90 + angle;
swap(rect_size.width, rect_size.height);
}
//如果抓取的方块旋转超过m_angle角度,则不是车牌,放弃处理
if (angle - 30 < 0 && angle + 30 > 0&&rect_size.height>minH&&rect_size.width>minW)
{
//Create and rotate image
Mat rotmat = getRotationMatrix2D(minRect.center, angle, 1);
Mat img_rotated;
warpAffine(conMat, img_rotated, rotmat, conMat.size(), CV_INTER_CUBIC);
Mat resultMat;
resultMat = showResultMat(img_rotated, rect_size, minRect.center);
rects.push_back(minRect);
rv.push_back(this->cvMat2QImage(resultMat));
++itc;
}
else
{
itc = contours.erase(itc);
}
}//end of while
return rv.size();
}
int RecogniseBusiness::plateLocate(QImage& s, vector<QImage>& rv)
{
//QImage转cvMat
Mat srcImage=QImage2cvMat(s);
//高斯模糊
cv::Mat dstImage(srcImage.clone());
cv::GaussianBlur(srcImage, dstImage, Size(5, 5), 0, 0, BORDER_DEFAULT);
// cv::imshow("beforegaus",srcImage);
// cv::imshow("aftergaus",dstImage);
//灰度化
cv::cvtColor(dstImage, dstImage, CV_BGR2GRAY);
// cv::imshow("aftercvtColor",dstImage);
//sobel算子
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
//Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
//Gradient X
//Scharr( dstImage, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( dstImage, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
//Gradient Y
//Scharr( dstImage, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( dstImage, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
//Total Gradient (approximate)
// addWeighted( abs_grad_x, SOBEL_X_WEIGHT, abs_grad_y, SOBEL_Y_WEIGHT, 0, grad );
// 在调用参数中有两个常量SOBEL_X_WEIGHT与SOBEL_Y_WEIGHT代表水平方向和垂直方向的权值,默认前者是1,后者是0,
// 代表仅仅做水平方向求导,而不做垂直方向求导。这样做的意义是,如果我们做了垂直方向求导,会检测出很多水平边缘。
addWeighted( abs_grad_x, 1, abs_grad_y, 0, 0, dstImage );
// cv::imshow("afterSobel",dstImage);
//二值化
threshold(dstImage, dstImage, 0, 255, CV_THRESH_OTSU+CV_THRESH_BINARY);
//threshold(dstImage, dstImage, 75, 255, CV_THRESH_BINARY);
// cv::imshow("afterthreshold",dstImage);
//颜色信息膨胀
Mat element = getStructuringElement(MORPH_RECT, Size(21, 3) );
dstImage=dilateCondition(dstImage,dstImage,srcImage,element);
// dilate(dstImage,dstImage,element);
//小范围膨胀
Mat element1 = getStructuringElement(MORPH_RECT, Size(3, 3) );
dilate(dstImage,dstImage,element1);
// cv::imshow("small",dstImage);
//腐蚀
Mat element2 = getStructuringElement(MORPH_RECT, Size(7, 3) );
cv::erode(dstImage, dstImage, element2);
// cv::imshow("erode",dstImage);
//寻找可能的车牌轮廓
vector< vector< Point> > contours;
findContours(dstImage,
contours, // a vector of contours
CV_RETR_EXTERNAL, // 提取外部轮廓
CV_CHAIN_APPROX_NONE); // all pixels of each contours
//Start to iterate to each contour founded
vector<vector<Point> >::iterator itc = contours.begin();
vector<RotatedRect> rects;
//Remove patch that are no inside limits of aspect ratio and area.
//筛出倾斜角过大的矩形
float minH=srcImage.rows/100;
float minW=srcImage.cols/15;
while (itc != contours.end())
{
//Create bounding rect of object
RotatedRect minRect = minAreaRect(Mat(*itc));
//筛选
float r = (float)minRect.size.width / (float)minRect.size.height;
float angle = minRect.angle;
Size rect_size = minRect.size;
if (r < 1)
{
angle = 90 + angle;
swap(rect_size.width, rect_size.height);
}
//如果抓取的方块旋转超过m_angle角度,则不是车牌,放弃处理
if (angle - 30 < 0 && angle + 30 > 0&&rect_size.height>minH&&rect_size.width>minW)
{
//Create and rotate image
Mat rotmat = getRotationMatrix2D(minRect.center, angle, 1);
Mat img_rotated;
warpAffine(srcImage, img_rotated, rotmat, srcImage.size(), CV_INTER_CUBIC);
Mat resultMat;
resultMat