我在做的是=>我正在捕捉图像,之后我正在计算捕获图像的倾斜角度,然后我根据计算的倾斜角度旋转该图像。一切都运行正常,但问题是我在我的代码中使用了 cv :: RotatedRect ,因此我生成的图像也包含该框,我想删除它从我的结果图像框,任何人有任何想法如何删除该框..?我在下面指定我的代码..请帮助..!谢谢..! [我无法上传我的结果图片,因为我是StackOverflow的新手,但我无权上传图片。]
// Function to Find Skew angle.
double compute_skew(const char* filename)
{
// Load in grayscale.
cv::Mat img = cv::imread(filename, 0);
// Binarize
cv::threshold(img, img, 225, 255, cv::THRESH_BINARY);
// Invert colors
cv::bitwise_not(img, img);
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 3));
cv::erode(img, img, element);
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.end<uchar>();
for (; it != end; ++it)
if (*it)
points.push_back(it.pos());
cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
double angle = box.angle;
if (angle < -45.)
angle += 90.;
cv::Point2f vertices[4];
box.points(vertices);
for(int i = 0; i < 4; ++i)
cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(255, 0, 0), 1, CV_AA);
std::cout << "File **************Angle***************** " << filename << ": " << angle << std::endl;
return angle;
}
// Function to rotate image image
void deskew(const char* filename, double angle)
{
//angle = 90;
cv::Mat img = cv::imread(filename, 0);
cv::bitwise_not(img, img);
std::vector<cv::Point> points;
cv::Mat_<uchar>::iterator it = img.begin<uchar>();
cv::Mat_<uchar>::iterator end = img.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);
cv::Mat rotated;
cv::warpAffine(img, rotated, rot_mat, img.size(), cv::INTER_CUBIC);
cv::bitwise_not(rotated, rotated);
imwrite(filename,rotated);
}