void rotate(cv::Mat& src, double angle, cv::Mat& dst){ //cout << RANDCOL << "R O T A T I N G" << endlr; //int len = std::max(src.cols, src.rows); cv::Point2f ptCp(src.cols*0.5, src.rows*0.5); //cv::Point2f pt(len/2., len/2.); cv::Mat M = cv::getRotationMatrix2D(ptCp, angle, 1.0); cv::warpAffine(src, dst, M, src.size(), cv::INTER_CUBIC); //Nearest is too rough, }
Mat Normalized::SkewDetectionUsesCentreOfMass(int upper, int lower) { cout << "upper = " << upper << endl; cout << "lower = " << lower << endl; cv::Rect crop(0, upper, this->word.cols, lower - upper); Mat word_removed_baseline = this->word(crop).clone(); //Mat word_removed_baseline = this->word.clone(); //imwrite("D:/word_removed_baseline.jpg", word_removed_baseline); int x_CoM_left, x_CoM_right; int y_CoM_left, y_CoM_right; cv::line(word_removed_baseline, cv::Point(word_removed_baseline.cols / 2, 0), cv::Point(word_removed_baseline.cols / 2, word_removed_baseline.rows), Scalar(128, 128, 128)); x_CoM_left = this->xCentroid(word_removed_baseline, 0, 0, word_removed_baseline.cols / 2, word_removed_baseline.rows); x_CoM_right = this->xCentroid(word_removed_baseline, word_removed_baseline.cols / 2, 0, word_removed_baseline.cols, word_removed_baseline.rows) + word_removed_baseline.cols / 2; y_CoM_left = this->yCentroid(word_removed_baseline, 0, word_removed_baseline.cols / 2, word_removed_baseline.rows); y_CoM_right = this->yCentroid(word_removed_baseline, word_removed_baseline.cols / 2, word_removed_baseline.cols, word_removed_baseline.rows); float slope = float(y_CoM_right - y_CoM_left) / (x_CoM_right - x_CoM_left); cv::line(word_removed_baseline, cv::Point(x_CoM_left, y_CoM_left), cv::Point(x_CoM_right, y_CoM_right), Scalar(128, 128, 128)); //float slope = -10; float theta = atan(slope) * 180 / M_PI; //cout << "left (x, y) = (" << x_CoM_left << ", "<< y_CoM_left << ")"<< endl; //cout << "right (x, y) = (" << x_CoM_right << ", " << y_CoM_right << ")" << endl; //cout << "slope = " << slope << endl; cout << "theta = " << theta << endl; if (theta <= 15) { cv::Point2f ptCp(0, 0); cv::Mat M = cv::getRotationMatrix2D(ptCp, theta, 1); cv::warpAffine(word, word, M, word.size(), INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar(255, 255, 255)); //Nearest is too rough, } else { cout << "Nghieng qua khong lam gi ca" << endl; } //imshow("Rotate", word); //imwrite("D:/Rotate.jpg", word); return word_removed_baseline; }