【发布时间】:2020-06-07 02:35:38
【问题描述】:
我正在使用 Python(3.7) 和 OpenCV 开展一个项目,其中我有一个带有 QR 码的文档的图像(使用相机捕获)。
这个二维码有6个变量分别为:
二维码图片尺寸
顶部
正确
底部
左
单位
最新更新:
以下是我需要按相同顺序执行的步骤:
- 检测二维码并对其进行解码以读取大小值
- 因此,如果 QR 码(图像)的大小不等于其中提到的大小,则将图像缩放为等于两个大小值。
- 然后根据二维码里面提到的数值,从二维码图像向四周裁剪图像。
我试过这段代码:
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()
上面的代码给出了一个错误: 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:错误:(-215:断言失败)!_img.empty () 在函数'imwrite'中
最新更新结束:
二维码的解码信息示例为:100,20,40,60,20,px
现在,我需要从该文档图像中检测 QR 码,并且在第一步中,我需要将捕获的文档图像中 QR 码的大小与解码信息中提到的大小进行比较,例如,如果在捕获的图像 QR 图像的大小为 90X90px,解码信息的大小为 100X100px,我们需要进行比较。
然后,在第二步中,我必须相应地使用 Top、Right、Bottom 和 Left 变量来裁剪整个图像。根据上面的例子,我们需要将图像从检测到的二维码位置裁剪为上20px、右40px、下60px和右20px。我在下面添加了一个示例图片。
我已经完成了二维码信息的解码,但是如何将检测到的二维码区域作为单独的图像并将其大小与提到的大小进行比较,然后相应地裁剪图像?
这是我迄今为止尝试过的:
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")
这是一个示例图片:
这是输入图像的示例:
【问题讨论】:
-
在您的示例中,似乎您正在获取图像,我无法完全理解问题所在
-
@YunusTemurlenk 实际上我需要实现两件事:1):裁剪 QR 码并将其与其中提到的尺寸进行比较 2):根据 Top、Right、Bottom 的值裁剪图像&左
-
我上面提到的第一张图片就是想要的东西。
-
您已经有检测到的矩形点。因此,通过减去点 x 和 y 的值,可以得到检测到的 QR 的大小。然后你也可以根据这些点来操作裁剪图像点。
-
@YunusTemurlenk 你能放一个代码示例吗!
标签: python image opencv computer-vision qr-code