#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;
}