Hello, I write the code in detect license plate using openCV and C++. It's work.
Now I want to crop region in bounding rect. Please help me. Thanks.
Here is my code
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv\ml.h>
#include <opencv\cxcore.h>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <opencv2\imgcodecs.hpp>
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace cv;
using namespace std;
cv::Mat _img; cv::Mat _imgGray;
int main()
{
_img = cv::imread("bs9.jpg"); if (_img.empty()) { std::cout << "error: image not read from file\n\n";
return(0);
}
cv::Mat src;
medianBlur(_img, src, 9);
cv::cvtColor(src, _imgGray, CV_BGR2GRAY);
cv::Mat _imgGray2;
medianBlur(_imgGray, _imgGray, 7);
blur(_imgGray, _imgGray, Size(3, 3));
cv::Mat edges;
cv::Canny(_imgGray, edges, 100, 200, 3);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
int w_threshold = 190;
int h_threshold = 140;
vector<int> selected;
Mat drawing = Mat::zeros(edges.size(), CV_8UC3);
Rect R;
for (int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(0, 255, 0);
R = boundingRect(contours[i]);
if (R.width / (double)R.height>1.20 && R.width / (double)R.height < 1.48 && R.width > 200 && R.height > 100)
{
selected.push_back(i);
drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
}
}
cv::imshow("Goc", _img); for (size_t i = 0; i < selected.size(); i++)
{
rectangle(_img, boundingRect(contours[selected[i]]), Scalar(0, 0, 255), 5);
}
cv::imshow("Xam", _imgGray); cv::imshow("edges", edges); cv::imshow("contours", drawing);
cv::imshow("detect", _img); cv::waitKey(0);
return(0);
}