全局变量
Mat matrix;
Point2f center;
Mat frame;
float scale = 0.25;
bool flag = false;
主函数
int main()
{
frame = imread("celiang.jpg");
resize(frame, frame, Size(0,0), scale, scale, INTER_LINEAR);
imshow("original", frame);
int thres[] = { 50,255 };
Mat out;
Point2f a4;
getcontours(frame, thres,out,a4);
double a4_width = 210;
double a4_height = 297;
int scale = 3;
Point2f mart;
int th[] = { 50,255 };
getcontours_2(out, th,mart);
if (flag)
{
int wid = round(mart.x / a4.x * a4_width);
int hei = round(mart.y / a4.y * a4_height);
string w = to_string(wid)+"cm";
putText(out, w, Point(center.x, center.y - 50), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(0, 0, 255), 1);
string h = to_string(hei)+"cm";
putText(out, h, Point(center.x - 70, center.y + hei ), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(0, 0, 255), 1);
}
imshow("test", out);
waitKey(0);
return 0;
}
参照物识别
void getcontours(Mat img, const int arr[],Mat& out,Point2f& ob,int minarea=1000*scale)
{
Mat imggray;
cvtColor(img,imggray, COLOR_BGR2GRAY);
Mat imggauss;
GaussianBlur(imggray,imggauss, Size(5, 5), 1);
Mat imgcanny;
Canny(imggauss, imgcanny, arr[0], arr[1]);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(imgcanny, imgcanny, MORPH_CLOSE, kernel);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imgcanny, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector<vector<Point>>finalcontours;
vector<vector<Point>>contours_poly(contours.size());
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > minarea)
{
finalcontours.push_back(contours[i]);
}
}
double len = INT_MIN;
int index;
for (int i = 0; i < finalcontours.size(); i++){
double l = arcLength(finalcontours[i], true);
if ( l> len){len = l;}
index = i;}
if (finalcontours.size() != 0)
{
vector<Point2f>maxcontours(finalcontours[index].size());
approxPolyDP(Mat(finalcontours[index]), maxcontours, 0.02*arcLength(finalcontours[index], true), true);
RotatedRect bbox = minAreaRect(maxcontours);
ob.x = bbox.size.width;
ob.y = bbox.size.height;
map<double, int> reorder;
reorder.insert(pair<double, int>(maxcontours[0].x + maxcontours[0].y, 0));
reorder.insert(pair<double, int>(maxcontours[1].x + maxcontours[1].y, 1));
reorder.insert(pair<double, int>(maxcontours[2].x + maxcontours[2].y, 2));
reorder.insert(pair<double, int>(maxcontours[3].x + maxcontours[3].y, 3));
int i = 0;
Point2f srcimg[4];
for (map<double, int>::iterator iter = reorder.begin(); iter != reorder.end(); iter++)
{
srcimg[i] = maxcontours[iter->second];
i++;
}
const Point2f dstimg[4] = { Point2f(0, 0),Point2f(bbox.size.width, 0) , Point2f(0, bbox.size.height),Point2f(bbox.size.width, bbox.size.height) };
matrix = getPerspectiveTransform(srcimg, dstimg);
inv_matrix = matrix.inv();
Mat imgwarp;
warpPerspective(img, imgwarp, matrix, Size(bbox.size.width, bbox.size.height));
out = imgwarp.clone();
}
}
目标识别
void getcontours_2(Mat& img, const int arr[], Point2f& mart,int minarea = 1000*scale)
{
flag = false;
Mat imggray;
cvtColor(img, imggray, COLOR_BGR2GRAY);
Mat imggauss;
GaussianBlur(imggray, imggauss, Size(5, 5), 1);
Mat imgcanny;
Canny(imggauss, imgcanny, arr[0], arr[1]);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
morphologyEx(imgcanny, imgcanny, MORPH_CLOSE, kernel);
vector<vector<Point>>contours;
vector<Vec4i>hierarchy;
findContours(imgcanny, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
vector<vector<Point>>finalcontours;
vector<vector<Point>>contours_poly(contours.size());
for (int i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > minarea&&contours[i].size()>500*scale)
{
finalcontours.push_back(contours[i]);
}
}
double len = INT_MIN;
int index;
for (int i = 0; i < finalcontours.size(); i++) {
double l = arcLength(finalcontours[i], true);
if (l> len) { len = l; }
index = i;
}
if (finalcontours.size() != 0)
{
flag = true;
vector<Point2f>maxcontours(finalcontours[index].size());
approxPolyDP(Mat(finalcontours[index]), maxcontours, 0.02*arcLength(finalcontours[index], true), true);
RotatedRect bbox = minAreaRect(maxcontours);
mart.x = bbox.size.width;
mart.y = bbox.size.height;
map<double, int> reorder;
reorder.insert(pair<double, int>(maxcontours[0].x + maxcontours[0].y, 0));
reorder.insert(pair<double, int>(maxcontours[1].x + maxcontours[1].y, 1));
reorder.insert(pair<double, int>(maxcontours[2].x + maxcontours[2].y, 2));
reorder.insert(pair<double, int>(maxcontours[3].x + maxcontours[3].y, 3));
Point2f srcimg[4];
int i = 0;
for (map<double, int>::iterator iter = reorder.begin(); iter != reorder.end(); iter++)
{
srcimg[i] = maxcontours[iter->second];
i++;
}
Mat dest = (Mat_<double>(3, 3) << srcimg[0].x, srcimg[0].y, 1,
srcimg[1].x, srcimg[1].y, 1,
srcimg[2].x, srcimg[2].y, 1);
Mat original = dest*inv_matrix;
center = srcimg[0];
arrowedLine(img, srcimg[0], srcimg[1], Scalar(0, 0, 255), 3, 8, 0);
arrowedLine(img, srcimg[0], srcimg[2], Scalar(0, 0, 255), 3, 8, 0);
}
}
效果图
|