gpt4 book ai didi

Python - 从图像中检测二维码并使用 OpenCV 进行裁剪

转载 作者:行者123 更新时间:2023-12-02 16:11:56 24 4
gpt4 key购买 nike

我正在使用 Python(3.7) 和 OpenCV 处理一个项目,其中我有一个文档的图像(使用相机捕获),上面放置了 QR 码。

此二维码有 6 个变量,分别为:

  • 二维码图片尺寸
  • 顶部
  • 底部
  • 单位


  • 最新更新:

    Here are the steps I need to perform in the same order:

    1. Detect the qr code & decode it to read size values
    2. So, if the size of QR-code(image) is not equal to the size which is mentioned inside it then scale the image to equal both size values.
    3. Then crop the image towards all sides from QR code image according to the values mentioned inside qr code.

    I have tried this code:


    def decodeAndCrop(inputImage):
    print(str(inputImage))
    image = cv2.imread(str(inputImage))
    qrCodeDetector = cv2.QRCodeDetector()
    decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
    qr_data = decodedText.split(",")
    print("qr data from fucntion: {}".format(qr_data))
    if points is not None:
    pts = len(points)
    # print(pts)
    for i in range(pts):
    nextPointIndex = (i + 1) % pts
    if str(inputImage) == "scaled_img.jpg":
    cv2.line(
    image,
    tuple(points[i][0]),
    tuple(points[nextPointIndex][0]),
    (255, 0, 0),
    5,
    )
    print(points[i][0])
    width = int(
    math.sqrt(
    (points[0][0][0] - points[1][0][0]) ** 2
    + (points[0][0][1] - points[1][0][1]) ** 2
    )
    )
    height = int(
    math.sqrt(
    (points[1][0][0] - points[2][0][0]) ** 2
    + (points[1][0][1] - points[2][0][1]) ** 2
    )
    )
    print("height and width after scaling: {} {}".format(height, width))
    if not str(inputImage) == "scaled_img.jpg":
    scaled_img = None
    if width == qr_data[0] and height == qr_data[0]:
    print("Sizes are equal")
    # Add the extension values to points and crop
    y = int(points[0][0][1]) - int(qr_data[1])
    x = int(points[0][0][0]) - int(qr_data[4])
    roi = image[
    y : y + height + int(qr_data[3]), x : x + width + int(qr_data[2])
    ]
    scaled_img = cv2.imwrite("scaled_img.jpg", roi)
    return scaled_img
    else:
    print(
    "Width and height "
    + str(width)
    + "x"
    + str(height)
    + " not equal to "
    + str(qr_data[0])
    + "x"
    + str(qr_data[0])
    )
    if height > int(qr_data[0]):
    scale_width = int(width) - int(qr_data[0])
    scale_height = int(height) - int(qr_data[0])
    print(f"scaled width: {scale_width} scaled height: {scale_height}")
    dimension = (scale_width, scale_height)
    scaled_img = cv2.resize(
    image, dimension, interpolation=cv2.INTER_AREA
    )
    print("new img dims: {}".format(scaled_img.shape))
    cv2.imshow("scaled image:", scaled_img)
    cv2.imwrite("scaled_img.jpg", scaled_img)
    elif height < int(qr_data[0]):
    scale_width = int(qr_data[0]) - width
    scale_height = int(qr_data[0] - height)
    print(f"scaled width: {scale_width} scaled height: {scale_height}")
    dimension = (scale_width, scale_height)
    scaled_img = cv2.resize(
    image, dimension, interpolation=cv2.INTER_AREA
    )
    print("new img dims: {}".format(scaled_img.shape))
    cv2.imshow("scaled image:", scaled_img)
    cv2.imwrite("scaled_img.jpg", scaled_img)
    cv2.imshow("final output:", roi)
    return scaled_img

    else:
    y = int(points[0][0][1]) - int(qr_data[1])
    x = int(points[0][0][0]) - int(qr_data[4])
    print(" x and y")
    print(x)
    print(y)
    roi = image[
    y : y + height + int(qr_data[3]), x : x + width + int(qr_data[2])
    ]
    final_img = cv2.imwrite("finalized_image.jpg", roi)
    cv2.imshow("finalized image:", final_img)
    return final_img


    if __name__ == "__main__":
    image_to_crop = decodeAndCrop("example_input_1.jpg")
    final_image = decodeAndCrop("scaled_img.jpg")
    cv2.imshow("Cropped:", image_to_crop)
    # cv2.imshow("Final: ", final_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    The code above gives an error as: final_img = cv2.imwrite("finalized_image.jpg", roi) cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'



    最新更新结束:

    二维码的解码信息示例为:100, 20, 40, 60, 20, px

    现在,我需要从这个文档图像中检测二维码,在第一步中,我需要将捕获的文档图像中二维码的大小与解码信息中提到的大小进行比较,例如,如果在捕获的图像中QR 图像的大小为 90X90 像素,解码信息的大小为 100X100 像素,我们需要进行比较。

    然后,在第二步中,我必须相应地使用 Top、Right、Bottom 和 Left 变量来裁剪完整的图像。根据上面的示例,我们需要将图像从检测到的二维码位置裁剪为 20 像素顶部、40 像素右侧、60 像素底部和 20 像素右侧。我在下面添加了一个示例图片。

    我已经完成了对二维码信息的解码,但是如何将检测到的二维码区域作为单独的图像并将其大小与提到的大小进行比较,然后相应地裁剪图像?

    Here's what I have tried so far:


    import cv2

    image = cv2.imread('/Users/abdul/PycharmProjects/QScanner/images/second.jpg')

    qrCodeDetector = cv2.QRCodeDetector()
    decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
    qr_data = decodedText.split(',')
    qr_size = qr_data[0]
    top = qr_data[1]
    right = qr_data[2]
    bottom = qr_data[3]
    left = qr_data[4]

    print(f'Size: {qr_size}' + str(qr_data[5]))
    print(f'Top: {top}')
    print(f'Right: {right}')
    print(f'Bottom: {bottom}')
    print(f'Left: {left}')
    if points is not None:
    pts = len(points)
    print(pts)
    for i in range(pts):
    nextPointIndex = (i+1) % pts
    cv2.line(image, tuple(points[i][0]), tuple(points[nextPointIndex][0]), (255,0,0), 5)
    print(points[i][0])
    print(decodedText)
    cv2.imshow("Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    else:
    print("QR code not detected")

    Here's an example Image:



    enter image description here

    and here's a sample of input image:



    enter image description here

    最佳答案

    这是使用阈值、形态学操作和轮廓过滤的简单方法。

  • 获取二值图像。 Load image , grayscale , Gaussian blur , Otsu's threshold
  • 连接各个 QR 轮廓。 使用 cv2.getStructuringElement() 创建一个矩形结构内核然后执行 morphological operationscv2.MORPH_CLOSE .
  • 过滤二维码。 Find contours
    并使用 contour approximation 过滤, contour area , 和 aspect ratio .

  • 检测到的二维码
    enter image description here
    提取的二维码
    enter image description here
    从这里您可以将二维码与您的引用信息进行比较
    代码
    import cv2
    import numpy as np

    # Load imgae, grayscale, Gaussian blur, Otsu's threshold
    image = cv2.imread('1.jpg')
    original = image.copy()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (9,9), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Morph close
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)

    # Find contours and filter for QR code
    cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.04 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    area = cv2.contourArea(c)
    ar = w / float(h)
    if len(approx) == 4 and area > 1000 and (ar > .85 and ar < 1.3):
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)
    ROI = original[y:y+h, x:x+w]
    cv2.imwrite('ROI.png', ROI)

    cv2.imshow('thresh', thresh)
    cv2.imshow('close', close)
    cv2.imshow('image', image)
    cv2.imshow('ROI', ROI)
    cv2.waitKey()

    关于Python - 从图像中检测二维码并使用 OpenCV 进行裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60359398/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com