//
// PlateLocation.cpp
// MyGraduationDesgin
//
// Created by xieshiyong on 2018/4/22.
// Copyright © 2018年 xieshiyong. All rights reserved.
#include "PlateLocation.hpp"
///////////////////////////////////////////////////////////////////
//非核心函数
///////////////////////////////////////////////////////////////////
PlateLocation* PlateLocation::location = NULL;
PlateLocation* PlateLocation::instance()
{
if (location == NULL)
{
location = new PlateLocation();
}
return location;
}
PlateLocation::~PlateLocation()
{
delete location;
}
void PlateLocation::debug(bool flag)
{
_debug_ = flag;
}
bool PlateLocation::debug()
{
return _debug_;
}
void PlateLocation::maxAngle(double angle)
{
max_angle = angle;
}
double PlateLocation::maxAngle()
{
return max_angle;
}
PlateLocation::PlateLocation()
{
this->gaussian_size = Size(3, 3);
this->morph_colsed_size = Size(17, 3);
this->sobel_x_scale = 1.0;
this->sobel_y_scale = 0.0;
min_area = 500;
max_area = 35000;
min_length_scale = 1.8;
max_length_scale = 5.3;
this->max_angle = 45;
this->_debug_ = true;
addBackgroundColor(min_blue, max_blue);
addBackgroundColor(min_yellow, max_yellow);
min_plate_num = 1;
}
int PlateLocation::minPlateNum()
{
return min_plate_num;
}
void PlateLocation::minPlateNum(int cnt)
{
min_plate_num = cnt;
}
int PlateLocation::addBackgroundColor(Vec3b minHsv, Vec3b maxHsv)
{
background_color.push_back(minHsv);
background_color.push_back(maxHsv);
return (int)background_color.size() / 2 + 1;
}
int PlateLocation::clearColorInfo()
{
background_color.clear();
font_color.clear();
return 0;
}
int PlateLocation::addFontColor(Vec3b minHsv, Vec3b maxHsv)
{
font_color.push_back(minHsv);
font_color.push_back(maxHsv);
return (int)font_color.size() / 2 + 1;
}
int PlateLocation::addColorInfo(pair<Vec3b, Vec3b> background, pair<Vec3b, Vec3b> font)
{
int cnt = addBackgroundColor(background.first, background.second);
addFontColor(font.first, font.second);
return cnt;
}
//HSV是否合法
bool PlateLocation::isValidColor(uchar h, uchar s, uchar v)
{
for (int i = 0; i < background_color.size(); i += 2)
{
Vec3b min_hsv = background_color[i];
Vec3b max_hsv = background_color[i + 2];
if (h < min_hsv[0] || s < min_hsv[1] || v < min_hsv[2] || h > max_hsv[0] || s > max_hsv[1] || v > max_hsv[2])
{
return false;
}
}
return true;
}
///////////////////////////////////////////////////////////////////
//核心函数
///////////////////////////////////////////////////////////////////
//通过颜色预处理
bool PlateLocation::colorPretreatment(Mat& src, Mat& dst, int color_index)
{
if (background_color.size() == 0)
{
return false;
}
vector<Mat> vecMat;
maxAngle(60);
max_area = 90000;
Mat src_hsv, temp;
cvtColor(src, src_hsv, CV_BGR2HSV);
myshow("temp", src_hsv);
Vec3b min_hsv = background_color[2 * color_index + 0], max_hsv = background_color[2 * color_index + 1];
//cout << "mis_hsv" << min_hsv << endl;
//cout << "max_hsv" << max_hsv << endl;
inRange(src_hsv, Scalar(min_hsv[0], min_hsv[1], min_hsv[2]), Scalar(max_hsv[0], max_hsv[1], max_hsv[2]), temp);
Mat ele = getStructuringElement(MORPH_RECT, Size(10, 3));
morphologyEx(temp, temp, MORPH_CLOSE, ele);
myshow("temp", temp);
BFSFilter(temp);
temp.copyTo(dst);
return true;
}
//大致过滤掉一些噪声
void PlateLocation::BFSFilter(Mat& img)
{
int dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
Mat tag = Mat::zeros(img.rows, img.cols, img.type());
Mat temp = Mat(img.size(), img.type(), Scalar(0));
myshow("BFSFIlter img", img);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
queue<Point> q;
vector<Point> v;
Point temp;
int maxr = -1, maxb = -1, minu = img.rows + 1, minl = img.cols + 1;
if (img.at<uchar>(i, j) == 255 && tag.at<uchar>(i, j) == 0)
{
temp.x = i;
temp.y = j;
v.push_back(temp);
q.push(temp);
tag.at<uchar>(i, j) = 1;
//temp.x is rows
//temp.y is cols
maxr = max(maxr, temp.y);
minl = min(minl, temp.y);
maxb = max(maxb, temp.x);
minu = min(minu, temp.x);
while (!q.empty())
{
Point top = q.front();
q.pop();
for (int k = 0; k < 4; k++)
{
int tempi = top.x + dir[k][0];
int tempj = top.y + dir[k][1];
if (tempi >= 0 && tempi < img.rows && tempj >= 0 && tempj < img.cols && img.at<uchar>(tempi, tempj) == 255 && tag.at<uchar>(tempi, tempj) == 0)
{
temp.x = tempi;
temp.y = tempj;
maxr = max(maxr, temp.y);
minl = min(minl, temp.y);
maxb = max(maxb, temp.x);
minu = min(minu, temp.x);
tag.at<uchar>(tempi, tempj) = 1;
v.push_back(temp);
q.push(temp);
}
}
}
int width = maxr - minl + 1;
int height = maxb - minu + 1;
if (v.size() < min_area || v.size() > max_area || width < 50 || height < 10)
{
if (_debug_)
{
//printf("size = %d\n", (int)v.size());
//printf("width = %d\n", width);
//printf("height = %d\n", height);
}
for (int k = 0; k < v.size(); k++)
{
img.at<uchar>(v[k].x, v[k].y) = 0;
}
if (_debug_)
{
//myshow("BFSFilter debug", img);
}
}
}
}
}
}
bool PlateLocation::rectPretreatment(Mat&src, Mat& dst)
{
Mat temp;
Mat sobel_x, sobel_y;
// if (src.total() >= 1000000)
// {
// resize(src, src, Size(src.cols / 2, src.rows / 2));
// }
maxAngle(30);
max_area = 35000;
GaussianBlur(src, temp, Size(5, 5), 0.0, 0.0, BORDER_DEFAULT);
myshow("temp", temp);
cvtColor(temp, temp, CV_RGB2GRAY);
myshow("temp", temp);
Sobel(temp, sobel_x, CV_16S, 1, 0, 3, 1, 0);
convertScaleAbs(sobel_x, sobel_x);
Sobel(temp, sobel_y, CV_16S, 0, 1, 3, 1, 0);
convertScaleAbs(sobel_y, sobel_y);
addWeighted(sobel_x, 1, sobel_y, 0, 0, temp);
myshow("temp", temp);
threshold(temp, temp, 0, 255, THRESH_OTSU | THRESH_BINARY);
myshow("temp", temp);
Mat ele = getStructuringElement(MORPH_RECT, Size(17, 3));
morphologyEx(temp, temp, MORPH_CLOSE, ele);
myshow("temp", temp);
BFSFilter(temp);
temp.copyTo(dst);
return true;
}
void PlateLocation::myshow(string winname, Mat& img)
{
if (_debug_)
{
namedWindow(winname, WINDOW_AUTOSIZE);
imshow(winname, img);
waitKey();
destroyWindow(winname);
}
}
vector<vector<Point> > PlateLocation::findROI(Mat& img)
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(img, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_NONE);
if (_debug_ & 0)
{
MyGraduationDesgin.zip_opencv 车牌识别_opencv车牌识别_定位_神经网络_车牌
版权申诉
56 浏览量
2022-07-15
08:00:40
上传
评论
收藏 23KB ZIP 举报
APei
- 粉丝: 63
- 资源: 1万+
最新资源
- 卡方数据.sav
- Python实现基于Django框架的二手物品购物网站设计源码+数据库.zip
- 基于Python和Django的二手物品购物网站设计源码+数据库脚本.zip
- 【计算机毕业设计】基于h5的移动网赚项目小程序【源码+lw+部署文档+讲解】
- 【计算机毕业设计】基于ssm-vue的oa系统设计与实现【源码+lw+部署文档+讲解】
- 基于pytorch实现的YOLOV5+SORT的车辆行人目标识别及追踪系统源码.zip
- 【计算机毕业设计】基于servlet+jdbc的在线选房系统设计与实现【源码+lw+部署文档+讲解】
- 投身科技创新,勇担时代先锋.pptx
- 【计算机毕业设计】基于SSM的仿微博系统的设计与实现【源码+lw+部署文档+讲解】
- 使用 Web Components 实现,遵循 Material You 设计规范的 Web 前端组件库
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈