#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <ctype.h>
using namespace cv;
using namespace std;
int main()
{
int largest_area = 0;
int largest_contour_index = 0;
int a = 0;
cout << "1 ideal\n" << "2 blurred\n" << "3 gradient\n";
cin >> a;
Mat IdealImage;
switch (a)
{
case 1:
{
IdealImage = imread("ideal.png");
break;
}
case 2:
{
IdealImage = imread("blurred.png");
break;
}
case 3:
{
IdealImage = imread("gradient.png");
break;
}
}
int rows = IdealImage.rows;
int cols = IdealImage.cols;
Mat EmptyBoard = imread("ref.png");
Mat GrayImage, ThresholdedImage, ReferenceImage, ContouredImage, IdealImageTransformed, GrayImage1; //Mat dst(IdealImage.row, IdealImage.cols, CV_8UC1, Scalar::all(0));
cvtColor(EmptyBoard, GrayImage, COLOR_BGR2GRAY);
adaptiveThreshold(GrayImage, ThresholdedImage, 100, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, 0);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
vector<vector<Point>>approximatedContour;
findContours(ThresholdedImage, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
double a = contourArea(contours[i], false);
if (a > largest_area)
{
largest_area = a;
largest_contour_index = i;
}
}
Scalar color(255, 255, 255); //drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy, 0);
vector<vector<Point>>largest_polynomial(1);
approxPolyDP(Mat(contours[largest_contour_index]), largest_polynomial[0], 10, true);
Rect boundRect = boundingRect(contours[largest_contour_index]);
Point ver1 = largest_polynomial[0][0];
Point ver2 = largest_polynomial[0][1];
Point ver3 = largest_polynomial[0][2];
Point ver4 = largest_polynomial[0][3];
polylines(IdealImage, largest_polynomial[0], true, color, 1, 8, 0);
vector<Point2f>Quad;
vector<Point2f>Square;
Quad.push_back(Point2f(largest_polynomial[0][0].x, largest_polynomial[0][0].y));
Quad.push_back(Point2f(largest_polynomial[0][1].x, largest_polynomial[0][1].y));
Quad.push_back(Point2f(largest_polynomial[0][3].x, largest_polynomial[0][3].y));
Quad.push_back(Point2f(largest_polynomial[0][2].x, largest_polynomial[0][2].y));
Square.push_back(Point2f(boundRect.x, boundRect.y));
Square.push_back(Point2f(boundRect.x, boundRect.y + boundRect.height));
Square.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y));
Square.push_back(Point2f(boundRect.x + boundRect.width, boundRect.y + boundRect.height));
Mat TransformedMatrix = getPerspectiveTransform(Quad, Square);
warpPerspective(IdealImage, IdealImageTransformed, TransformedMatrix, IdealImage.size());
Mat WarpedPerspective = IdealImageTransformed(boundRect);
Mat grayscaled, binarized;
cvtColor(WarpedPerspective, grayscaled, CV_BGR2GRAY);
threshold(grayscaled, binarized, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
Mat tak;
if (a == 1) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 31, 28); }
if (a == 2) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 27, 10); }
if (a == 3) { adaptiveThreshold(grayscaled, tak, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 41, 24); }
//imshow("tak", tak);
vector<vector<Point>>lettercontours;
vector<Vec4i> hierarchyoflet;
findContours(tak, lettercontours, hierarchyoflet, CV_RETR_LIST, CV_CHAIN_APPROX_TC89_L1);
int idx = 0;
Scalar color2(255, 0, 0);
//vector<Point>contour;
double tablica[100][7];
int b = 0;
//fstream humom("D", ios::out);
int c = 0;
double hu[7];
fstream humom("humombase.txt", ios::out);
fstream dupa("a.txt", ios::out);
// fstream chuj("a.txt", ios::out);
vector<Point> koordynat;
vector<Point> kontur = lettercontours.at(4);
for (int i = 0; idx >= 0; idx = hierarchyoflet[idx][0], i++)
{
//int xparam = lettercontours[i][2].x;
Point koordynat = kontur.at(4);
int b = koordynat.x;
Moments lettermoments;
double area = contourArea(lettercontours[i]);
if (area >= 162 && area <= 500)
{
drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3);
lettermoments = moments(lettercontours[i]);
HuMoments(lettermoments, hu);
humom << hu[0] << " " << hu[1] << " " << hu[2] << " " << hu[3] << " " << hu[4] << " " << hu[5] << " " << hu[6] << "\n";
if (hu[0] > 0.35 && hu[0] < 0.36 && hu[1]> 0.047 && hu[1] < 0.054 && hu[2]> 3.1336e-05 && hu[2] < 0.00033) { dupa << "M"; }
//0.357834 0.0528427 0.000212741 1.52876e-05 - 2.2733e-10 - 5.24759e-07 - 8.41673e-10
//0.351721 0.0471768 0.000123528 2.02553e-05 8.49384e-10 3.05756e-06 - 5.52353e-10
//0.353218 0.0524782 6.62608e-05 1.78519e-05 6.11112e-10 2.93039e-06 5.92793e-11
if (hu[0] > 0.25 && hu[0] < 0.254 && hu[1]> 0.006207 && hu[1] < 0.006208) { dupa << "R"; }
//0.253799 0.00620756 8.20748e-05 0.000118764 - 4.62818e-09 7.75944e-06 - 1.07735e-08
if (hu[0] > 0.209 && hu[0] < 0.22 && hu[1]> 0.0026 && hu[1] < 0.0028 && hu[2]> 0.0055 && hu[2] < 0.0061) { dupa << "A"; }
//0.211435 0.00277891 0.00559733 0.000347034 - 4.21318e-07 - 1.80716e-05 2.37545e-07
//0.209258 0.00267701 0.00556412 0.000330352 -3.46123e-07 -1.49303e-05 2.84248e-07
//0.213268 0.00270815 0.00609144 0.000331404 -4.5811e-07 -1.71538e-05 1.08849e-07
if (hu[0] > 0.40 && hu[0] < 0.44 && hu[1]> 0.004 && hu[1] < 0.064 && hu[2]> 0.0004 && hu[2] < 0.0035) { dupa << "O"; }
//0.401631 0.0431767 0.000441394 9.52397e-05 -1.7502e-08 -1.8505e-05 8.65982e-09 340 380
//0.439863 0.0633895 0.00345674 0.000441106 - 4.53871e-07 - 0.000101384 - 3.0114e-07
if (hu[0] > 0.265 && hu[0] < 0.281 && hu[1]> 0.0046 && hu[1] < 0.0103 && hu[2]> 0.00027 && hu[2]< 0.0041) { dupa << "S"; }
//0.26567 0.00460785 0.00027196 6.60365e-06 2.00793e-10 4.03965e-07 1.94934e-10 280 291
//0.275741 0.00979134 0.00274575 4.91276e-05 -8.69577e-09 -8.15549e-07 -1.58098e-08
//0.280532 0.0102846 0.00407578 0.000111535 -3.8332e-08 1.06307e-05 6.46982e-08
if (hu[0] > 0.24255 && hu[0] < 0.24256 && hu[1]> 0.0176 && hu[1] < 0.0177 && hu[2] > 0.0034656 && hu[2] < 0.00346565) { dupa << "L"; }
// 0.242559 0.017601 0.00346563 0.000493662 2.61852e-07 - 3.77269e-05 5.90231e-07
if (hu[0] > 0.316 && hu[0] < 0.317 && hu[1]> 0.027 && hu[1] < 0.0276 && hu[2] > 0.01 && hu[2] < 0.011) { dupa << "P"; }
//0.316841 0.0275557 0.0107918 0.00148656 5.94034e-06 0.000232005 -4.05691e-07
if (hu[0] > 0.22 && hu[0] < 0.239 && hu[1]> 0.014 && hu[1] < 0.01461 && hu[2] > 0.0019 && hu[2] < 0.0042) { dupa << "T"; }
//0.238834 0.0143339 0.00419507 6.10304e-05 -3.08808e-08 -4.80126e-06 2.99048e-11 200 210
//0.227097 0.0146042 0.00194496 9.9943e-05 3.05285e-08 -5.88191e-06 3.17749e-08 180 200
if (hu[0]> 0.24 && hu[0] < 0.261 && hu[1]> 0.0035 && hu[1] < 0.008 && hu[2] > 0.0003 && hu[2] < 0.0015) { dupa << "E"; }
//0.251685 0.00774301 0.000544931 0.000897187 - 5.07325e-07 - 7.49691e-05 - 3.69002e-07 215 225
//0.243329 0.00388335 0.000332522 0.000625236 2.30959e-07 -3.14723e-05 1.67128e-07 225 245
//0.260123 0.00573609 0.0013481 0.00109925 -1.21903e-06 -8.05672e-05 5.5191e-07 275 280
if (hu[0] > 0.228304 && hu[0] < 0.228306 && hu[1]> 0.0257696 && hu[1] < 0.0257698 && hu[2] > 4.586e-05 && hu[2] < 4.5869e-05) { dupa << "I"; }
//0.228305 0.0257697 4.58629e-05 4.27387e-06 - 4.46381e-11 - 5.51293e-07 3.98471e-11
}
//waitkey(0);
//lettercom
//tak = target.clne;
//{ //mu[i] = moments(hierarchyoflet[i], true);
// void HuMoments(const Moments& moments, double hu[7]);
//
//}
//Moments mom = moments(contourArea(lettercontours[i]));
//vector<Moments> mom(hierarchyoflet.size());
// double area = contourArea(lettercontours[i]);
//if (a == 1 && area > 100 && area < 500) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
//if (a == 2 && area > 120 && area < 500) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
//if (a == 3 && area > 100 && area < 490) { drawContours(tak, lettercontours, idx, color2, CV_FILLED, 8, hierarchyoflet, 3); }
//imshow("tak", tak);
//waitKey(1);
/*b = b + 1;
if (b > 200)
{
imshow("IdealImage", IdealImage);
imshow("done", binarized);
imshow("tak", tak);
waitKey(1);
}*/
}
imshow("tak", tak);
//imshow("IdealImage", IdealImage);
//imshow("done", binarized);
//imshow("tak", tak);
waitKey();
return 0;
}