提问人:kotai2003 提问时间:5/2/2023 最后编辑:kotai2003 更新时间:5/2/2023 访问量:259
如何使用 OpenCV python 读取 Data Matrix 代码
How to read the Data Matrix Code with OpenCV python
问:
我试图读取金属表面上的数据矩阵代码。数据矩阵代码是用激光焊接雕刻的。我尝试了带有图像预处理的pylibdmtx。
这是我的代码和结果。不幸的是,我无法从预处理的图像中读取数据。
import cv2
import numpy as np
'''
1. Image Read
2. Image Process
a. gray scale
b. crop image
c. EqualizeHist
d. Thres Binaray 152
e. Blur
f. Threshod otsu
g. bitwise not
3. Result image save
'''
# crop image
def crop_image(image, min_x, max_x, min_y, max_y):
# Load the image
# Get image dimensions
height, width = image.shape[:2]
print(height, width)
# Calculate the crop sizes
crop_left = int(min_x * width)
crop_right = int((1 - max_x) * width)
crop_top = int(min_y * height)
crop_bottom = int((1 - max_y) * height)
print(crop_left, crop_right)
# Crop the image
cropped_image = image[crop_top:crop_bottom, crop_left:crop_right]
print(cropped_image.shape)
# Return the cropped image
return cropped_image
# cv2 createTrackbar
def empty(x):
pass
cv2.namedWindow('Parameters')
cv2.resizeWindow('Parameters', width=600, height=400)
cv2.createTrackbar('min_x', 'Parameters', 38, 100, empty) # crop x
cv2.createTrackbar('max_x', 'Parameters', 36, 100, empty) # crop x
cv2.createTrackbar('min_y', 'Parameters', 43, 100, empty) # crop x
cv2.createTrackbar('max_y', 'Parameters', 10, 100, empty) # crop x
cv2.createTrackbar('threshold', 'Parameters', 136, 255, empty) # threshold
cv2.createTrackbar('k_size_set', 'Parameters', 18, 20, empty) # blur
cv2.createTrackbar('k_mat_set', 'Parameters', 2, 5, empty) #k_mat
cv2.createTrackbar('iter_d', 'Parameters', 2, 30, empty) # 5
cv2.createTrackbar('iter_e', 'Parameters', 4, 30, empty) # 5
img = cv2.imread('./image/live.png')
# Gray Scale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# resize
img = cv2.resize(img, dsize=None, fx=0.5, fy=0.5)
# -----------------------------------------------
#
# -----------------------------------------------
while True:
## 00. Make a Copy
img_copy = img.copy()
img_black = np.zeros_like(img)
# 1. Crop Image
dx_min = cv2.getTrackbarPos('min_x', 'Parameters')
dx_max = cv2.getTrackbarPos('max_x', 'Parameters')
dy_min = cv2.getTrackbarPos('min_y', 'Parameters')
dy_max = cv2.getTrackbarPos('max_y', 'Parameters')
dx_min, dx_max = dx_min / 100, dx_max / 100
dy_min, dy_max = dy_min / 100, dy_max / 100
img_crop = crop_image(image=img_copy, min_x=dx_min, max_x=dx_max, min_y=dy_min, max_y=dy_max)
# 3 Equalize hist
img_hist = cv2.equalizeHist(img_crop)
# 4. Thres Binaray 152
thres = cv2.getTrackbarPos('threshold', 'Parameters')
_, img_thresh = cv2.threshold(img_hist, thres, 255, cv2.THRESH_BINARY)
# 5. Gaussian Blur
kernel = cv2.getTrackbarPos('k_size_set', 'Parameters')
kernel = (kernel * 2) + 1
img_blur = cv2.GaussianBlur(img_thresh, (kernel, kernel), None)
# 6. Threshod otsu
_, img_thresh2 = cv2.threshold(img_blur, 0, 255, cv2.THRESH_OTSU)
# 7kernel : dilate , erodeのkernelの定義に注意
kernel2 = cv2.getTrackbarPos('k_mat_set', 'Parameters')
kernel2 = (kernel2 * 2) + 1
k_mat = np.ones((kernel2, kernel2), np.uint8)
# 8. Dilate
iter_dilate = cv2.getTrackbarPos('iter_d', 'Parameters')
img_dilate = cv2.dilate(img_thresh2, kernel=k_mat, iterations=iter_dilate)
# 9. Erode
iter_erode = cv2.getTrackbarPos('iter_e', 'Parameters')
img_erode = cv2.erode(img_dilate, kernel=k_mat, iterations=iter_erode)
# Plotting
cv2.imshow('result2' ,img_erode)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imwrite('./image/result2.png', img_erode)
print('saved')
cv2.destroyAllWindows()
这是我的图像预处理程序:
图像读取
图像处理 A. 灰度 B. 裁剪图像 c. 均衡历史 d. Thres Binaray e. 模糊 f. Threshod otsu g. 按位不
结果图像保存
答: 暂无答案
评论