#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <Windows.h>
using namespace std;
using namespace cv;
Point2f centerpoint(0, 0);
int j = 0;
Point2f computeIntersect(Vec4i a, Vec4i b) {
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1],
x4 = b[2], y4 = b[3];
if (float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)))
{
Point2f pnt;
pnt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 -
y3 * x4)) / d;
pnt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 -
y3 * x4)) / d;
return pnt;
}
else
return Point2f(-1, -1);
}
void sortCorners(vector<Point2f>& corner_points, Point2f centerpoint)
{
vector<Point2f> top, bot;
for (int i = 0; i < corner_points.size(); i++)
{
if (corner_points[i].y < centerpoint.y)
top.push_back(corner_points[i]);
else
bot.push_back(corner_points[i]);
}
Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
corner_points.clear();
corner_points.push_back(tl);
corner_points.push_back(tr);
corner_points.push_back(br);
corner_points.push_back(bl);
}
int main()
{
/*
int dist = 1;
int theta = 180;
int threshold = 92;
int minLine = 30;
int maxLine = 20;
namedWindow("Perspectives", CV_WINDOW_AUTOSIZE);
createTrackbar("Distance Resolution","Perspectives", &dist, 5);
createTrackbar("Theta", "Perspectives", &theta, 180);
createTrackbar("threshold", "Perspectives", &threshold, 100);
createTrackbar("Min Line", "Perspectives", &minLine, 100);
createTrackbar("Max Line", "Perspectives", &maxLine, 100);
*/
vector<String> filenames;
String folder = "D:\\datas";
glob(folder, filenames);
while (j < filenames.size())
{
for (size_t i = 0; i < filenames.size(); i++)
{
Mat src = imread(filenames[i]);
if (!src.data)
return -1;
Mat dst = src.clone();
Mat bw;
cvtColor(src, bw, CV_BGR2GRAY);
Canny(bw, bw, 100, 100, 3);
vector<Vec4i> lines;
HoughLinesP(bw, lines, 1, CV_PI / 180, 92, 30, 20);
vector<Point2f> corner_points;
for (int i = 0; i < lines.size(); i++)
{
for (int j = i + 1; j < lines.size(); j++)
{
Point2f pnt = computeIntersect(lines[i], lines[j]);
if (pnt.x >= 0 && pnt.y >= 0)
corner_points.push_back(pnt);
}
}
vector<Point2f> approx;
approxPolyDP(Mat(corner_points), approx, arcLength(Mat(corner_points),
true) * 0.02, true);
//if (approx.size() != 4)
//{
// cout << "The object is not quadrilateral!" << endl;
//return -1;
//}
//Get center point
for (int i = 0; i < corner_points.size(); i++)
centerpoint += corner_points[i];
centerpoint *= (1. / corner_points.size());
sortCorners(corner_points, centerpoint);
//Draw lines
for (int i = 0; i < lines.size(); i++)
{
Vec4i v = lines[i];
line(dst, Point(v[0], v[1]), Point(v[2], v[3]), CV_RGB(0, 255, 0));
}
//Draw corner points
circle(dst, corner_points[0], 3, CV_RGB(255, 0, 0), 2);
circle(dst, corner_points[1], 3, CV_RGB(0, 255, 0), 2);
circle(dst, corner_points[2], 3, CV_RGB(0, 0, 255), 2);
circle(dst, corner_points[3], 3, CV_RGB(255, 255, 255), 2);
//Draw mass center points
circle(dst, centerpoint, 3, CV_RGB(255, 255, 0), 2);
//Calculate corresponding points for corner points
Mat quad = Mat::zeros(src.rows, src.cols, CV_8UC3);
vector<Point2f> quad_pnts;
quad_pnts.push_back(Point2f(0, 0));
quad_pnts.push_back(Point2f(quad.cols, 0));
quad_pnts.push_back(Point2f(quad.cols, quad.rows));
quad_pnts.push_back(Point2f(0, quad.rows));
// Draw corresponding points
circle(dst, quad_pnts[0], 3, CV_RGB(255, 0, 0), 2);
circle(dst, quad_pnts[1], 3, CV_RGB(0, 255, 0), 2);
circle(dst, quad_pnts[2], 3, CV_RGB(0, 0, 255), 2);
circle(dst, quad_pnts[3], 3, CV_RGB(255, 255, 255), 2);
Mat transmtx = getPerspectiveTransform(corner_points, quad_pnts);
warpPerspective(src, quad, transmtx, quad.size());
//Create windows and display results
//namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
namedWindow("Selected Points", CV_WINDOW_AUTOSIZE);
namedWindow("Corrected Perspertive", CV_WINDOW_NORMAL);
//imshow("Original Image", src);
imshow("Selected Points", dst);
imshow("Corrected Perspertive", quad);
cout << filenames[i] << endl;
if (waitKey(30) == 27)
break;
Sleep(1000);
j++;
}
}
destroyAllWindows();
return 0; //End
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น