在iOS中使用openCV计算倾斜角度

时间:2018-02-14 17:14:38

标签: c++ ios opencv

我正在尝试为名片执行OCR,我正在尝试预处理图像,而图像旋转/纠偏是其中一步。

我正在使用openCV来做到这一点。

这是带有一些倾斜文字的图像:

enter image description here

输出:

enter image description here

我做了一些研究,发现通过计算轮廓和创建充满洪水的图像可以改善计算倾斜角度。

我也尝试了那个,这里是洪水填充产生的图像:

enter image description here

floodfill_threshold:

enter image description here

flood fill_inverse:

enter image description here

floodfill_threshold的灰度图片:

enter image description here

计算倾斜角度的代码:

cv::Mat tempMat2 = im_floodfill.clone();//Tried with all three image results
cv::cvtColor(cvImage2, tempMat2, cv::COLOR_BGR2GRAY);
cv::bitwise_not(tempMat2, tempMat2);

UIImage *invertImg5 = [self UIImageFromCVMat:tempMat2];//just to see the image

//Calculating Skew angle
std::vector<cv::Vec4i> lines;
cv::HoughLinesP(tempMat2, lines, 1, CV_PI/180, 100, size.width / 2.f, 30);
cv::Mat disp_lines(size, CV_8UC1, cv::Scalar(0, 0, 0));
double angle = 0.;
unsigned nb_lines = lines.size();
for (unsigned i = 0; i < nb_lines; ++i)
{
    cv::line(disp_lines, cv::Point(lines[i][0], lines[i][1]),
             cv::Point(lines[i][2], lines[i][3]), cv::Scalar(255, 0 ,0));
    angle += atan2((double)lines[i][3] - lines[i][1],
                   (double)lines[i][2] - lines[i][0]);
}
angle /= nb_lines; // mean angle, in radians.

UIImage *newImg = [self UIImageFromCVMat:disp_lines];

return angle;

要轮换的代码:

//Got the angle and now rotate the image as per the angle
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = cvImage.begin<uchar>();
cv::Mat_<uchar>::iterator end = cvImage.end<uchar>();
for (; it != end; ++it)
    if (*it)
        points.push_back(it.pos());

cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));

cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, angle, 1); //angle

cv::Mat rotated;
cv::warpAffine(cvImage, rotated, rot_mat, cvImage.size(), cv::INTER_CUBIC);

行数为575,输出图像仍然相同:

enter image description here

有一会儿我忽略了这个输出图像,并且关心了21度的倾斜角度,

生成的图像是:

enter image description here

当我尝试旋转负数21时,结果为:

enter image description here

这不是我所希望的。我不确定为什么计算的倾斜角度是错误的。

我不知道的一件事是

中的行高参数
cv::HoughLinesP(tempMat2, lines, 1, CV_PI/180, 100, size.width / 2.f, 30);

我在这里使用了30,但我不知道如何根据图像(名片图像)计算出这个数字。

我在这里缺少什么?

0 个答案:

没有答案
相关问题