OpenCV QR Kod Tarayıcı
Son zamanlarda, OpenCV 4.0 ile birçok iyileştirme ve yeni özellik ile piyasaya sürüldü. Bunlardan biri QR kod tarayıcıdır. Harici bir kitaplık olan ZBar’ı kullanacağız. Bu nedenle, OpenCV tarayıcısının nasıl çalıştığını ve Zbar tarayıcıdan daha iyi olup olmadığını kontrol etmek istedik. Bu yazıda, OpenCV ile yeni QR kod tarayıcının nasıl kullanılacağını göreceğiz.
OpenCV’de QR Kod Tarayıcı
QR Kodu nedir ? hakkında daha fazla ayrıntı almak istiyorsanız, Wikipedia’yı ziyaret etmenizi öneririm. Şimdi QR Kod Algılama koduna geçelim. Lütfen aşağıdaki bağlantıdan kodu indirin. İndirme bölümünde ZBar ve OpenCV QR Code Detector’ı karşılaştırmak için kod da ekledik.
C++ Kodu
Kütüphane ve gerekli yöntemleri ekledik
#include <opencv2/objdetect.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
Algılanan QR Kodunun etrafındaki kutuyu çizmek için fonksiyon
void display(Mat &im, Mat &bbox)
{
int n = bbox.rows;
for(int i = 0 ; i < n ; i++)
{
line(im, Point2i(bbox.at<float>(i,0),bbox.at<float>(i,1)), Point2i(bbox.at<float>((i+1) % n,0), bbox.at<float>((i+1) % n,1)), Scalar(255,0,0), 3);
}
imshow("Result", im);
}
Ana işlevde, önce resmi okuruz. Ardından, bir QRCodeDetector Nesnesi oluşturuyoruz ve verileri ve QR Kodunun konumunu bulmak için DetectAndDecode yöntemini kullanıyoruz. Son olarak sonuçları görüntülüyoruz.
int main(int argc, char* argv[])
{
// Read image
Mat inputImage;
if(argc>1)
inputImage = imread(argv[1]);
else
inputImage = imread("qrcode-learnopencv.jpg");
QRCodeDetector qrDecoder = QRCodeDetector::QRCodeDetector();
Mat bbox, rectifiedImage;
std::string data = qrDecoder.detectAndDecode(inputImage, bbox, rectifiedImage);
if(data.length()>0)
{
cout << "Decoded Data : " << data << endl;
display(inputImage, bbox);
rectifiedImage.convertTo(rectifiedImage, CV_8UC3);
imshow("Rectified QRCode", rectifiedImage);
waitKey(0);
}
else
cout << "QR Code not detected" << endl;
}
Python Kodu
Önce modülleri ekledik
import cv2
import numpy as np
import sys
import time
Ardından, giriş resmini okuyoruz, kendi resminizi komut satırından belirtebilirsiniz.
if len(sys.argv)>1:
inputImage = cv2.imread(sys.argv[1])
else:
inputImage = cv2.imread("qrcode-learnopencv.jpg")
QR kodun etrafındaki kutuyu görüntülüyoruz.
# Display barcode and QR code location
def display(im, bbox):
n = len(bbox)
for j in range(n):
cv2.line(im, tuple(bbox[j][0]), tuple(bbox[ (j+1) % n][0]), (255,0,0), 3)
# Display results
cv2.imshow("Results", im)
Bir QRCodeDetector Nesnesi oluşturduk ve detectAndDecode yöntemini kullanarak kodu ve konumunu tespit ettik.
qrDecoder = cv2.QRCodeDetector()
# Detect and decode the qrcode
data,bbox,rectifiedImage = qrDecoder.detectAndDecode(inputImage)
if len(data)>0:
print("Çözülen Veri : {}".format(data))
display(inputImage, bbox)
rectifiedImage = np.uint8(rectifiedImage);
cv2.imshow("Rectified QRCode", rectifiedImage);
else:
print("QR kod bulunamadı")
cv2.imshow("Results", inputImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Kod çıktısı :
Time Taken for Detect and Decode : 0.044 seconds
Çözülen Veri : Information :
Found Type : QRCODE Barcode : BEGIN:VCARD
VERSION:3.0
N:Bayram;Bayram
ORG:Eker
EMAIL;TYPE=INTERNET:eker600@gmail.com
TEL:05**
END:VCARD