如何以编程方式查找图像中特定要素的像素位置?

Jon*_*age 6 python opencv image-processing

我正在使用OpenCV和Python构建自动电/气表读数器.我已经使用网络摄像头拍摄:

在此输入图像描述

然后我可以使用afine变换来取消图像的变形(这个例子的改编):

def unwarp_image(img):
    rows,cols = img.shape[:2]
    # Source points
    left_top = 12
    left_bottom = left_top+2
    top_left = 24
    top_right = 13
    bottom = 47
    right = 180
    srcTri = np.array([(left_top,top_left),(right,top_right),(left_bottom,bottom)], np.float32)

    # Corresponding Destination Points. Remember, both sets are of float32 type
    dst_height=30
    dstTri = np.array([(0,0),(cols-1,0),(0,dst_height)],np.float32)

    # Affine Transformation
    warp_mat = cv2.getAffineTransform(srcTri,dstTri)   # Generating affine transform matrix of size 2x3
    dst = cv2.warpAffine(img,warp_mat,(cols,dst_height))     # Now transform the image, notice dst_size=(cols,rows), not (rows,cols)

    #cv2.imshow("crop_img", dst)
    #cv2.waitKey(0)

    return dst
Run Code Online (Sandbox Code Playgroud)

..这给我一个像这样的图像:

在此输入图像描述

我仍然需要使用某种OCR例程来提取文本,但首先我想自动化识别应用仿射变换的像素位置的部分.因此,如果有人敲击网络摄像头,它不会阻止软件正常工作.

Dia*_*ana 2

由于您的图像几乎是平面的,因此您可以寻找从网络摄像头获得的图像和所需图像(处于直立位置)之间的单应性。

编辑:这会将图像旋转到直立位置。一旦您注册了图像(将其置于直立位置),您就可以进行行向或列向投影(将列上的所有像素相加得到一个向量,将行上的所有像素相加得到一个向量)向量)。您可以使用这些向量来找出颜色跳跃的位置,并在那里进行裁剪。

或者,您可以使用霍夫变换,它可以为您提供图像中的线条。如果您这样做,您可能可以不用注册图像。