C++ opencv-3.4.1 復雜的圖像矯正方法

參考賈志剛的opencv圖像處理方法春叫,對一張傾斜圖片進行矯正肩钠。條條大路通羅馬,對一張傾斜圖片進暂殖,有很多方法价匠,這是最復雜的一種,通過求四個傾斜角點的位置呛每,再設置目標點的位置踩窖,通過仿射變換進行圖片矯正。

與PS方法的優(yōu)勢就是可以通過代碼批量處理圖片晨横。

圖片:


只能說這種方法確實比較好洋腮,特別是在進行圖片批處理的時候,雖然復雜了點手形,但是能保證對大部分圖片效果較好啥供。
主要流程:
1 . 二值化

  1. 形態(tài)學操作,去噪點
  2. 進行輪廓查找叁幢, 通過 矩形的長款過濾較小和圖片的大邊框
  3. 霍夫直線變換滤灯,查找直線
  4. 過濾直線,通過直線位置和長度確定上下左右四條直線
  5. 求出四條直線
  6. 得到四條直線的交點,這就是物體原始四個角點
  7. 把原始的四個角點鳞骤,變換到圖片的四個角落窒百,透視變換會把相對位置的像素通過線性插值填充
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;
Mat src, dst, gray_src;
char input_image[] = "input image";
char output_image[] = "output image";

int main(int argc, char ** argv){

    src = imread("case5.jpg");
    if (src.empty()){
        printf("colud not load image ..\n");
        return -1;
    }

    namedWindow(input_image, CV_WINDOW_AUTOSIZE);
    namedWindow(output_image, CV_WINDOW_AUTOSIZE);
    imshow(input_image, src);

    // 二值化處理
    Mat binary;
    cvtColor(src, gray_src, COLOR_BGR2GRAY);
    threshold(gray_src, binary, 0, 255, CV_THRESH_BINARY_INV | THRESH_OTSU);
    imshow("binary image", binary);

    // 腐蝕操作
    Mat structureElement = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
    dilate(binary, binary, structureElement); //腐蝕
    imshow("erode", binary);

    // 形態(tài)學操作
    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    morphologyEx(binary, dst, MORPH_OPEN, kernel, Point(-1, -1), 3);
    imshow("morphology", dst);
    
    



    // 輪廓發(fā)現(xiàn)
    bitwise_not(dst, dst, Mat());
    vector<vector<Point>> contours;
    vector<Vec4i> hireachy;
    int width = src.cols;
    int height = src.rows;
    Mat drawImage = Mat::zeros(src.size(), CV_8UC3);
    findContours(dst, contours, hireachy, CV_RETR_TREE, CHAIN_APPROX_SIMPLE, Point());
    for (size_t t = 0; t < contours.size(); t++){
        Rect rect = boundingRect(contours[t]);
        printf("rect.width : %d, src.cols %d \n ", rect.width, src.cols);
        if (rect.width > (src.cols / 2) && rect.width < (src.cols - 5))
        {
            drawContours(drawImage, contours, static_cast<int>(t), Scalar(0, 0, 255), 2, 8, hireachy, 0, Point());
        }
    }
    imshow("contours", drawImage);


    // 繪制直線
    vector<Vec4i> lines;
    Mat contoursImg;
    int accu = min(width * 0.5, height *0.5); 
    cvtColor(drawImage, contoursImg, COLOR_BGR2GRAY);
    Mat linesImage = Mat::zeros(src.size(), CV_8UC3);
    HoughLinesP(contoursImg, lines, 1, CV_PI / 180.0, accu, accu, 0);
    for (size_t t = 0; t < lines.size(); t++){
        Vec4i ln = lines[t];
        line(linesImage, Point(ln[0], ln[1]), Point(ln[2], ln[3]), Scalar(0, 0, 255), 2, 8, 0);

    }
    printf("number of lines : %d", lines.size());
    imshow("lines image :", linesImage);

    // 定位直線
    int deltah = 0;
    Vec4i topLine, bottomLine, leftLine, rightLine;
    for (int i = 0; i < lines.size(); i++){
        Vec4i ln = lines[i];
        deltah = abs(ln[3] - ln[1]);
        if (ln[3] < height / 2.0 && ln[1] < height / 2.0 && deltah < accu - 1){
            topLine = lines[i];
        }
        if (ln[3] > height / 2.0 && ln[1] > height / 2.0 && deltah < accu - 1){
            bottomLine = lines[i];
        }
        if (ln[0] < width / 2.0 && ln[2] < width / 2.0 ){
            leftLine = lines[i];
        }
        if (ln[0] > width / 2.0 && ln[2] > width / 2.0){
            rightLine = lines[i];
        }
    }
    cout << "topLine : " << topLine << endl;
    cout << "bottomLine : " << bottomLine << endl;
    cout << "leftLine : " << leftLine << endl;
    cout << "rightLine : " << rightLine << endl;


    // 擬合四條直線方程
    float k1, c1;
    k1 = float(topLine[3] - topLine[1]) / float(topLine[2] - topLine[0]);
    c1 = topLine[1] - k1*topLine[0];
    float k2, c2;
    k2 = float(bottomLine[3] - bottomLine[1]) / float(bottomLine[2] - bottomLine[0]);
    c2 = bottomLine[1] - k2*bottomLine[0];
    float k3, c3;
    k3 = float(leftLine[3] - leftLine[1]) / float(leftLine[2] - leftLine[0]);
    c3 = leftLine[1] - k3*leftLine[0];
    float k4, c4;
    k4 = float(rightLine[3] - rightLine[1]) / float(rightLine[2] - rightLine[0]);
    c4 = rightLine[1] - k4*rightLine[0];

    // 四條直線交點
    Point p1; // 左上角
    p1.x = static_cast<int>((c1 - c3) / (k3 - k1));
    p1.y = static_cast<int>(k1*p1.x + c1);
    Point p2; // 右上角
    p2.x = static_cast<int>((c1 - c4) / (k4 - k1));
    p2.y = static_cast<int>(k1*p2.x + c1);
    Point p3; // 左下角
    p3.x = static_cast<int>((c2 - c3) / (k3 - k2));
    p3.y = static_cast<int>(k2*p3.x + c2);
    Point p4; // 右下角
    p4.x = static_cast<int>((c2 - c4) / (k4 - k2));
    p4.y = static_cast<int>(k2*p4.x + c2);
    cout << "p1(x, y)=" << p1.x << "," << p1.y << endl;
    cout << "p2(x, y)=" << p2.x << "," << p2.y << endl;
    cout << "p3(x, y)=" << p3.x << "," << p3.y << endl;
    cout << "p4(x, y)=" << p4.x << "," << p4.y << endl;

    // 顯示四個點坐標
    circle(linesImage, p1, 2, Scalar(255, 0, 0), 2, 8, 0);
    circle(linesImage, p2, 2, Scalar(255, 0, 0), 2, 8, 0);
    circle(linesImage, p3, 2, Scalar(255, 0, 0), 2, 8, 0);
    circle(linesImage, p4, 2, Scalar(255, 0, 0), 2, 8, 0);
    line(linesImage, Point(topLine[0], topLine[1]), Point(topLine[2], topLine[3]), Scalar(0, 255, 0), 2, 8, 0);
    imshow("four corners", linesImage);

    // 透視變換
    vector<Point2f> src_corners(4); // 原來的點
    src_corners[0] = p1;
    src_corners[1] = p2;
    src_corners[2] = p3;
    src_corners[3] = p4;

    vector<Point2f> dst_corners(4); // 目標點位
    dst_corners[0] = Point(0,0);
    dst_corners[1] = Point(width, 0);
    dst_corners[2] = Point(0, height);
    dst_corners[3] = Point(width , height);

    // 獲取變換矩陣
    Mat reslutImg;
    Mat warpmatrix = getPerspectiveTransform(src_corners, dst_corners);
    warpPerspective(src, reslutImg, warpmatrix, reslutImg.size(), INTER_LINEAR);
    imshow(output_image, reslutImg);


    waitKey(0);
    return 0;
}

二值化

形態(tài)學操作

輪廓查找

霍夫變換

求直線交點

透視變換

另種比較簡單的方法

如圖:


操作和以前的方法類似,處理比較簡單豫尽。

  1. 二值化
  2. 使用canny進行輪廓檢測篙梢,把輪廓進行顯著化
  3. 查找輪廓,得到旋轉的角度
  4. 進行仿射變換
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
using namespace std;
Mat src, dst, gray_src;
char input_image[] = "input image";
char output_image[] = "output image";

int threshold_value = 100;
int threshold_max = 255;
void FindROI(int, void*);
void Check_Skew(int, void *);
int main(int argc, char ** argv){

    src = imread("case12.jpg");
    if (src.empty()){
        printf("colud not load image ..\n");
        return -1;
    }

    namedWindow(input_image, CV_WINDOW_AUTOSIZE);
    namedWindow(output_image, CV_WINDOW_AUTOSIZE);
    imshow(input_image, src);

    createTrackbar("Threshold", output_image, &threshold_value, threshold_max, Check_Skew);
    Check_Skew(0, 0);


    waitKey(0);
    return 0;
}


void Check_Skew(int, void *){
    Mat  canny_output;
    cvtColor(src, gray_src, COLOR_BGR2GRAY);
    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));
    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);
        }
    }
    
    RNG rng(12345);
    for (size_t t = 0; t < contours.size(); t++){
        RotatedRect minRect = minAreaRect(contours[t]);
        if (maxw == minRect.size.width && maxh == minRect.size.height){
            degree = minRect.angle;
            Point2f pts[4];
            minRect.points(pts);
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            for (int i = 0; i < 4; i++){
                line(drawImg, pts[i], pts[(i + 1) % 4], color, 2, 8, 0);
            }
            }
    }

    imshow("roi", drawImg);


    printf("max contours width: %f\n", maxw);
    printf("max contours heigh: %f\n", maxh);
    printf("max contours angle: %f\n", degree);

    Point2f center(src.cols / 2, src.rows / 2);
    Mat rotm = getRotationMatrix2D(center, degree, 1.0);
    
    warpAffine(src, dst, rotm, src.size(), INTER_LINEAR, 0, Scalar(255, 255, 255));
    imshow(output_image, dst);
}

輪廓查找

矯正

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末美旧,一起剝皮案震驚了整個濱河市渤滞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌榴嗅,老刑警劉巖妄呕,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異嗽测,居然都是意外死亡绪励,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門唠粥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疏魏,“玉大人,你說我怎么就攤上這事晤愧〈竽” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵官份,是天一觀的道長只厘。 經(jīng)常有香客問我,道長贯吓,這世上最難降的妖魔是什么懈凹? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮悄谐,結果婚禮上介评,老公的妹妹穿的比我還像新娘。我一直安慰自己爬舰,他們只是感情好们陆,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著情屹,像睡著了一般坪仇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上垃你,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天椅文,我揣著相機與錄音喂很,去河邊找鬼。 笑死皆刺,一個胖子當著我的面吹牛少辣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播羡蛾,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼漓帅,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了痴怨?” 一聲冷哼從身側響起忙干,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎浪藻,沒想到半個月后捐迫,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡爱葵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年弓乙,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片钧惧。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖勾习,靈堂內(nèi)的尸體忽然破棺而出浓瞪,到底是詐尸還是另有隱情,我是刑警寧澤巧婶,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布乾颁,位于F島的核電站,受9級特大地震影響艺栈,放射性物質發(fā)生泄漏英岭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一湿右、第九天 我趴在偏房一處隱蔽的房頂上張望诅妹。 院中可真熱鬧,春花似錦毅人、人聲如沸吭狡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽划煮。三九已至,卻和暖如春缔俄,著一層夾襖步出監(jiān)牢的瞬間弛秋,已是汗流浹背器躏。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蟹略,地道東北人登失。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像科乎,于是被迫代替她去往敵國和親壁畸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354