一、边缘轮廓检测并框出来
import os
import cv2
def dmdecode(filename):
image = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
# 使用边缘检测算法检测边缘
edges = cv2.Canny(image, 100, 200) # 示例使用Canny边缘检测算法
# 查找轮廓
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(contours[0],"......................")
# 遍历轮廓,找到二维码的位置和边界框
# 遍历轮廓,找到二维码的位置和边界框
for contour in contours:
area = cv2.contourArea(contour)
if area > 100: # 根据实际情况设置面积阈值
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (200, 255, 135), 2)
# 显示结果图像
cv2.imshow('Result', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 遍历文件夹
file_path="img\erweima_xiaoguo\demo"
if __name__ == '__main__':
for filename in os.listdir(file_path):
dmdecode(os.path.join(file_path, filename))