Opencv4新屬性-二維碼識別


int main()
{
    Mat img = imread("C:\\Users\\24731\\Desktop\\000\\001.jpg");
    if (img.empty())
    {
        cout << "請確認圖像文件名稱是否正確" << endl;
        return -1;
    }
    Mat gray, qrcode_bin;
    cvtColor(img, gray, COLOR_BGR2GRAY);
    QRCodeDetector qrcodedetector;
    vector<Point> points;
    string information;
    bool isQRcode;
    isQRcode = qrcodedetector.detect(gray, points);  //識別二維碼
    if (isQRcode)
    {
        //解碼二維碼
        information = qrcodedetector.decode(gray, points, qrcode_bin);
        cout << points << endl;  //輸出二維碼四個頂點的坐標
    }
    else
    {
        cout << "無法識別二維碼,請確認圖像時候含有二維碼" << endl;
        return -1;
    }
    //繪制二維碼的邊框
    for (int i = 0; i < points.size(); i++)
    {
        if (i == points.size() - 1)
        {
            line(img, points[i], points[0], Scalar(0, 0, 255), 2, 8);
            break;
        }
        line(img, points[i], points[i + 1], Scalar(0, 0, 255), 2, 8);
    }
    //將解碼內容輸出到圖片上
    putText(img, information.c_str(), Point(20, 30), 0, 1.0, Scalar(0, 0, 255), 2, 8);

    //利用函數直接定位二維碼並解碼
    string information2;
    vector<Point> points2;
    information2 = qrcodedetector.detectAndDecode(gray, points2);
    cout << points2 << endl;
    putText(img, information2.c_str(), Point(20, 55), 0, 1.0, Scalar(0, 0, 0), 2, 8);

    //輸出結果
    imshow("result", img);
    namedWindow("qrcode_bin", WINDOW_NORMAL);
    imshow("qrcode_bin", qrcode_bin);
    waitKey(0);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM