using namespace std;
using namespace cv;
int main()
{
vector<String> src_paths;
glob(src_path, src_paths);
for (size_t m = 0; m < src_paths.size(); m++)
{
Mat src = imread(src_paths[m], 1);
Mat dst;
if (src.type() == CV_8UC1)
cvtColor(src, dst, COLOR_GRAY2BGR);
else
{
dst = src.clone();
cvtColor(src, src, COLOR_BGR2GRAY);
}
threshold(src, src, 90, 255,THRESH_BINARY);
vector<vector<Point>> contours;
findContours(src, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
cout << contours.size() << endl;
//
double maxValue = 0.0;
int maxIndex = -1;
for (size_t i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i]) > maxValue)
{
maxValue = contourArea(contours[i]);
maxIndex = i;
}
}
if (maxIndex == -1)
{
return -1;
}
RotatedRect r1 = minAreaRect(contours[maxIndex]);
//drawContours(dst, contours, maxIndex, Scalar(255, 0, 0),3);
//角度判断
double waffeAngle;
if (r1.size.width > r1.size.height)
waffeAngle = r1.angle ;
else
waffeAngle = r1.angle + 90;
Mat M = getRotationMatrix2D(r1.center, waffeAngle, 1.0);
Mat resultImg;
warpAffine(dst, resultImg, M, src.size(),1, BORDER_REPLICATE);
}
return 0;
}
校正后width在宽边