fanfuhan OpenCV 教學120 ~ opencv-120-二維碼檢測與識別
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/25/opencv-120/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
C++
#include#include using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src = imread("D:/images/qrcode.png"); imshow("image", src); Mat gray, qrcode_roi; cvtColor(src, gray, COLOR_BGR2GRAY); QRCodeDetector qrcode_detector; vector pts; string detect_info; bool det_result = qrcode_detector.detect(gray, pts); if (det_result) { detect_info = qrcode_detector.decode(gray, pts, qrcode_roi); } vector< vector > contours; contours.push_back(pts); drawContours(src, contours, 0, Scalar(0, 0, 255), 2); putText(src, detect_info.c_str(), Point(20, 200), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, 8); printf("qrcode info %s \n", detect_info.c_str()); imshow("result", src); waitKey(0); return 0; }
Python
"""
二维码检测与识别
"""
import cv2 as cv
import numpy as np
src = cv.imread("images/fanfan.jpg")
cv.imshow("image", src)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
qrcoder = cv.QRCodeDetector()
codeinfo, points, straight_qrcode = qrcoder.detectAndDecode(gray)
print(points)
result = np.copy(src)
cv.drawContours(result, [np.int32(points)], 0, (0, 0, 255), 2)
print("qrcode : %s" % codeinfo)
cv.imshow("result", result)
code_roi = np.uint8(straight_qrcode)
cv.namedWindow("qrcode roi", cv.WINDOW_NORMAL)
cv.imshow("qrcode roi", code_roi)
cv.waitKey(0)
cv.destroyAllWindows()