如何使用python查找和读取数据矩阵代码 [英] How to locate and read Data Matrix code with python

查看:490
本文介绍了如何使用python查找和读取数据矩阵代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取微管底部的数据矩阵条形码.我尝试了 libdmtx ,它具有python绑定,并且在矩阵的点为正方形时工作得很好,但是当它们的点为正方形时,效果会更差像这里一样圆:

I'm trying to read a datamatrix barcodes on the bottom of microtubes. I tried libdmtx which has python bindings and works reasonably well when the dots of the matrix are square but much worse when they are round as here:

另一个复杂之处是闪耀,在某些情况下会到达代码区域.

Another complication is a shine which in some cases reaches the code area.

条形码在平板扫描仪上的机架中进行扫描,因此条形码具有恒定的大小并大致居中.方向是随机的.

The barcodes are scanned in a rack on a flatbed scanner so they have constant size and are roughly centered. The orientation is random.

我得出一个结论,我必须找到代码并自己改善图像.我使用python和OpenCV 3.1.我已经尝试过阈值化,轮廓:

I came to a conclusion I'd have to locate the codes and improve the image myself. I use python and OpenCV 3.1. I have already tried thresholding, contours:

import matplotlib.pyplot as plt
import numpy as np
import cv2

well = plt.imread('https://i.stack.imgur.com/kqHkw.png')
well = cv2.cvtColor(well, cv2.COLOR_BGRA2GRAY)
plt.subplot(151); plt.imshow(well)

x, thr = cv2.threshold(well, .4[enter image description here][2], 1, cv2.THRESH_BINARY)
thr = np.uint8(thr)
plt.subplot(152); plt.imshow(thr)

dst, contours, hierarchy = cv2.findContours(thr.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = cv2.drawContours(np.zeros_like(thr), contours, -1, 255, 1)
plt.subplot(153); plt.imshow(c)

areas = map(lambda x: cv2.contourArea(cv2.convexHull(x)), contours)
max_i = areas.index(max(areas))
d = cv2.drawContours(np.zeros_like(thr), contours, max_i, 255, 1)
plt.subplot(154); plt.imshow(d)

rect = cv2.minAreaRect(contours[max_i])
box = cv2.boxPoints(rect)
box = np.int0(box)
e = cv2.drawContours(np.zeros_like(thr),[box],0,255,1)
plt.subplot(155); plt.imshow(e)

plt.show()

结果

推荐答案

事实证明,Harris拐角检测器(B)通过适当的设置很好地找到了圆形元素.

It turns out that the Harris corner detector (B) finds the round elements very well with proper settings.

阈值(C)之后,我们检测出结果区域的轮廓.我们选择最大的轮廓线(D)并找到最小的边界框(E).

After thresholding (C) we detect contours of the resulting areas. We select the largest contour (D) and find a minimal bounding box (E).

import matplotlib.pyplot as plt
import numpy as np
import cv2

well = plt.imread('https://i.stack.imgur.com/kqHkw.png')
well = cv2.cvtColor(well, cv2.COLOR_BGRA2GRAY)
plt.subplot(151); plt.title('A')
plt.imshow(well)

harris = cv2.cornerHarris(well,4, 1,0.00)
plt.subplot(152); plt.title('B')
plt.imshow(harris)

x, thr = cv2.threshold(harris, 0.1 * harris.max(), 255, cv2.THRESH_BINARY)
thr = thr.astype('uint8')
plt.subplot(153); plt.title('C')
plt.imshow(thr)

dst, contours, hierarchy = cv2.findContours(thr.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
areas = map(lambda x: cv2.contourArea(cv2.convexHull(x)), contours)
max_i = areas.index(max(areas))
d = cv2.drawContours(np.zeros_like(thr), contours, max_i, 255, 1)
plt.subplot(154); plt.title('D')
plt.imshow(d)

rect =cv2.minAreaRect(contours[max_i])
box = cv2.boxPoints(rect)
box = np.int0(box)
e= cv2.drawContours(well,[box],0,1,1)
plt.subplot(155); plt.title('E')
plt.imshow(e)

plt.show()

这篇关于如何使用python查找和读取数据矩阵代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆