#include "cv.h"
#include "highgui.h"
#include "iostream"
#include "string"
#include "cmath"
#include <sstream>
#define PI 3.1415926
using namespace std;
using namespace cv;
FILE* fp;
struct Lines{
double k;
double b;
bool per;
};
double rotationAngle = 0.0;
bool LineCmp(const Vec2f& a, const Vec2f& b) {
double threshold = 0.1;
if (abs(a[1] - b[1]) < threshold) {
return a[0] < b[0];
}
return a[1] < b[1];
}
bool PointCmp(const Point2f& a, const Point2f& b) {
return a.x + a.y < b.x + b.y;
}
vector<Lines> Findlines(Mat img) {
vector<Vec2f> lines;
vector<Lines> mls;
//rotationAngle = 0.0;
HoughLines(img, lines, 1, CV_PI / 100, 80, 0, 0);
sort(lines.begin(), lines.end(), LineCmp);
int last = lines.size() - 1;
const double minTheta = 0.1;
const double maxTheta = 3.1;
const double rhoThreshold = 50;<a target=_blank href="https://github.com/yinmingwang/identifyhandwriting">github链接</a>
while (lines[0][1] <= minTheta && lines[last][1] >= maxTheta) {
vector<Vec2f>::iterator it = lines.begin() + 1;
for (size_t i = 1; i < last; ++i) {
if (lines[i][1] <= minTheta && abs(lines[0][0] - lines[i][0]) < rhoThreshold) {
++it;
}
}
lines.insert(it, lines[last]);
it = lines.end() - 1;
lines.erase(it);
}
rotationAngle = lines[0][1] * 180 / CV_PI;
const double rotationThreshold = 0.1;
if (lines[0][1] > rotationThreshold) {
rotationAngle -= 90;
}
for (size_t i = 0; i < lines.size(); i++) {
float rho = lines[i][0], theta = lines[i][1];
const float deltaRho = 200;
const float deltaTheta = 0.25;
if (i > 0 && abs(abs(rho) - abs(lines[i - 1][0])) < deltaRho &&
(abs(CV_PI - (theta + lines[i - 1][1])) < deltaTheta || abs(theta - lines[i - 1][1]) < deltaTheta)) {
手写体识别(基于Opencv)
最新推荐文章于 2025-03-20 07:36:55 发布