opencv实践::切边
生活随笔
收集整理的這篇文章主要介紹了
opencv实践::切边
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述 真實案例,掃描儀掃描到的法律文件,需要切邊,去掉邊 緣空白,這樣看上去才真實。
#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 = 100;
int max_level = 255;
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...
");
return -1;
}
namedWindow("input image", CV_WINDOW_AUTOSIZE);
imshow("input image", src);
//糾正圖像角度
Check_Skew(0, 0);
//圖像切邊
FindROI(0, 0);
waitKey(0);
return 0;
}
void Check_Skew(int, void*) {
//尋找最大輪廓
Mat canny_output;
cvtColor(src, gray_src, COLOR_BGR2GRAY);
//Canny 算法做邊緣檢測
Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);
//在二值圖像中尋找輪廓
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
//創建一張黑色的圖,每個像素的每個通道都為0,Scalar(0,0,0)
//Mat drawImg = Mat::zeros(src.size(), CV_8UC3);
float maxw = 0;//矩形寬
float maxh = 0;//矩形高
double degree = 0;
for (size_t t = 0; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
//矩形角度絕對值
degree = abs(minRect.angle);
if (degree > 0) {
maxw = max(maxw, minRect.size.width);
maxh = max(maxh, minRect.size.height);
}
if (degree > 0) {
if (maxw == minRect.size.width && maxh == minRect.size.height) {
degree = minRect.angle;
}
}
}
printf("max contours width : %f
", maxw);
printf("max contours height : %f
", maxh);
printf("max contours angle : %f
", degree);
//尋找幾何中心
Point2f center(src.cols / 2, src.rows / 2);
//旋轉degree角度
Mat rotm = getRotationMatrix2D(center, degree, 1.0);
//對圖像做仿射變換
warpAffine(src, tmp_src, rotm, src.size(), INTER_LINEAR, 0, Scalar(255, 255, 255));
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 * 2, 3, false);
//在二值圖像中尋找輪廓
vector<vector<Point>> contours;
vector<Vec4i> hireachy;
findContours(canny_output, contours, hireachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
//定義最小輪廓
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 = 0; t < contours.size(); t++) {
RotatedRect minRect = minAreaRect(contours[t]);
if (minRect.size.width > minw && minRect.size.height > minh && minRect.size.width < (src.cols - 20)) {
{
//找寬度最小的矩形,既是要找的圖像。
printf("t = %d, w = %f , h = %f
",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 > 0 && bbox.height > 0) {
Mat roiImg = tmp_src(bbox);
imshow(roi_win, roiImg);
}
return;
}
總結
以上是生活随笔為你收集整理的opencv实践::切边的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 表现期和观察期
- 下一篇: 如何批量将dwg文件转换为pdf文件