问题描述
  真实案例,扫描仪扫描到的法律文件,需要切边,去掉边 缘空白,这样看上去才真实。

opencv实践::切边-LMLPHP

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h> using namespace cv;
using namespace std; #define IMAGE_PATH "D:/case2.png" Mat src, gray_src, tmp_src, dst;
int threshold_value = ;
int max_level = ;
const char* roi_win = "Final Result"; void FindROI(int, void*); void Check_Skew(int, void*); int main(int argc, char** argv) {
src = imread(IMAGE_PATH);
if (src.empty()) {
printf("could not load image...\n");
return -;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src); //纠正图像角度
Check_Skew(, ); //图像切边
FindROI(, ); waitKey();
return ;
} void Check_Skew(int, void*) {
//寻找最大轮廓
Mat canny_output;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
//Canny 算法做边缘检测
Canny(gray_src, canny_output, threshold_value, threshold_value * , , false); //在二值图像中寻找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(, )); //创建一张黑色的图,每个像素的每个通道都为0,Scalar(0,0,0)
//Mat drawImg = Mat::zeros(src.size(), CV_8UC3); float maxw = ;//矩形宽
float maxh = ;//矩形高
double degree = ;
for (size_t t = ; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
//矩形角度绝对值
degree = abs(minRect.angle);
if (degree > ) {
maxw = max(maxw, minRect.size.width);
maxh = max(maxh, minRect.size.height);
}
if (degree > ) {
if (maxw == minRect.size.width && maxh == minRect.size.height) {
degree = minRect.angle;
}
}
}
printf("max contours width : %f\n", maxw);
printf("max contours height : %f\n", maxh);
printf("max contours angle : %f\n\n\n", degree); //寻找几何中心
Point2f center(src.cols / , src.rows / );
//旋转degree角度
Mat rotm = getRotationMatrix2D(center, degree, 1.0);
//对图像做仿射变换
warpAffine(src, tmp_src, rotm, src.size(), INTER_LINEAR, , Scalar(, , ));
imshow("Correct Image", tmp_src);
} void FindROI(int, void*) {
//灰度图
cvtColor(tmp_src, gray_src, COLOR_BGR2GRAY);
Mat canny_output;
//Canny 算法做边缘检测
Canny(gray_src, canny_output, threshold_value, threshold_value * , , false); //在二值图像中寻找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(, )); //定义最小轮廓
float minw = tmp_src.cols*0.5;
float minh = tmp_src.rows*0.5; float minstW = 0.0;
bool bfirst = true;
RotatedRect minstRect;
Rect bbox;
for (size_t t = ; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
if (minRect.size.width > minw && minRect.size.height > minh && minRect.size.width < (src.cols - )) {
{
//找宽度最小的矩形,既是要找的图像。
printf("t = %d, w = %f , h = %f \n",t, minRect.size.width, minRect.size.height);
if (bfirst)
{
minstW = minRect.size.width;
minstRect = minRect;
bfirst = false;
}
else
{
float tmp = min(minstW, minRect.size.width);
if (tmp < minstW)
{
minstW = tmp;
minstRect = minRect;
}
}
}
}
}
bbox = minstRect.boundingRect();
if (bbox.width > && bbox.height > ) {
Mat roiImg = tmp_src(bbox);
imshow(roi_win, roiImg);
}
return;
}
05-22 08:09