QR码定位方法--适用背景简单的QR码

#-*-coding:utf-8-*- 
__author__ = 'kavin.zhang'
import os
import cv2
import math
from matplotlib import pyplot as plt
import numpy as np

def show_rgb(img, code=cv2.COLOR_BGR2RGB):
    cv_rgb = cv2.cvtColor(img, code)
    cv2.namedWindow("image")
    cv2.imshow("image", cv_rgb)
    cv2.waitKey()
def show(img):
    cv2.namedWindow("image")
    cv2.imshow("image", img)
    cv2.waitKey()


image_path = ".\\QRImage"
img_list = os.listdir(image_path)
for image in img_list:
    img = os.path.join(image_path, image)
    img = cv2.imread(img)
    show_rgb(img)

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # cv2.resize(img_gray,img_gray,(512,512))
    # img_gb = cv2.GaussianBlur(img_gray, (5, 5), 0)
    # show(img_gb) 
    edges = cv2.Canny(img_gray, 80, 150)
    show(edges)

    img_fc, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    hierarchy = hierarchy[0]
    found = []
    for i in range(len(contours)):
        k = i
        c = 0
        while hierarchy[k][2] != -1:
            k = hierarchy[k][2]
            c = c + 1
        if c >= 5:
            found.append(i)

    for i in found:
        img_dc = img.copy()
        cv2.drawContours(img_dc, contours, i, (0, 255, 0), 3)
        show(img_dc)

    # 寻找定位标记的顶点
    draw_img = img.copy()
    for i in found:
        rect = cv2.minAreaRect(contours[i])
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(draw_img,[box], 0, (0,0,255), 2)
    show(draw_img)

    # 定位筛选,一个QR码有三个Position Detection Pattern 组
    # 寻找定位标记的顶点

    draw_img = img.copy()
    for i in found:
        rect = cv2.minAreaRect(contours[i])
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(draw_img,[box], 0, (0,0,255), 2)
    show(draw_img)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值