日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

opencv实践::切边

發布時間:2023/12/24 综合教程 37 生活家
生活随笔 收集整理的這篇文章主要介紹了 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实践::切边的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。