首頁 > 後端開發 > C++ > 如何使用OpenCV準確偵測並提取一張紙的角點?

如何使用OpenCV準確偵測並提取一張紙的角點?

Susan Sarandon
發布: 2024-12-31 12:29:09
原創
481 人瀏覽過

How to Accurately Detect and Extract Corner Points of a Sheet of Paper Using OpenCV?

如何使用OpenCV 檢測紙張並提取角點

在本文中,我們將改進廣泛使用的OpenCV 方形檢測過濾掉無關結果並從影像中檢索一張紙的準確角點的範例。

原始內容OpenCV 範例無法有效濾除噪聲,導致輸出混亂且難以處理。為了解決這個問題,我們提出了一個修改後的實作:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

void find_squares(Mat&amp; image, vector<vector<Point> >&amp; squares)

{

    // Blur the image for enhanced edge detection

    Mat blurred(image);

    medianBlur(image, blurred, 9);

 

    // Convert to grayscale

    Mat gray0(blurred.size(), CV_8U), gray;

 

    // Detect contours for each color plane in the image

    for (int c = 0; c < 3; c++)

    {

        // Isolate a single color plane

        int ch[] = {c, 0};

        mixChannels(&amp;blurred, 1, &amp;gray0, 1, ch, 1);

 

        // Iterate through multiple threshold levels

        const int threshold_level = 2;

        for (int l = 0; l < threshold_level; l++)

        {

            if (l == 0)

            {

                // Use Canny instead of zero threshold to improve detection

                Canny(gray0, gray, 10, 20, 3);

                dilate(gray, gray, Mat(), Point(-1, -1)); // Remove potential holes

            }

            else

            {

                gray = gray0 >= (l + 1) * 255 / threshold_level;

            }

 

            // Find contours for each threshold level

            vector<vector<Point> > contours;

            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

 

            // Test contours to identify valid squares

            vector<Point> approx;

            for (size_t i = 0; i < contours.size(); i++)

            {

                approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true);

                if (approx.size() == 4

                    && abs(contourArea(Mat(approx))) > 1000

                    && isContourConvex(Mat(approx)))

                {

                    double maxCosine = 0;

                    for (int j = 2; j < 5; j++)

                    {

                        double cosine = fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]));

                        maxCosine = MAX(maxCosine, cosine);

                    }

 

                    if (maxCosine < 0.3)

                        squares.push_back(approx);

                }

            }

        }

    }

}

登入後複製

應用此改進的實作後,產生的正方形向量將包含偵測到的最大正方形,代表紙張。若要擷取紙張的角點,請從正方形向量中辨識面積最大的正方形。這個正方形的四個角點就是紙張所需的角點。

總之,我們增強的 OpenCV 實作透過消除誤報和準確提取角點,實現了對紙張的可靠檢測,使其成為影像處理應用程式的強大工具。

以上是如何使用OpenCV準確偵測並提取一張紙的角點?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板