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

时间:2013-06-23 09:10:05

标签: python opencv image-processing

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

enter image description here

然后我可以使用afine变换来取消图像的变形(this example的改编):

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

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

enter image description here

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

1 个答案:

答案 0 :(得分:2)

由于您的图像几乎是平面的,因此您可以查看从网络摄像头获取的图像与所需图像(垂直位置)之间的homography

编辑:这将使图像以直立位置旋转。一旦您注册了图像(将其置于垂直位置),您就可以进行行方式或列方式投影(沿列的所有像素求和以获得一个向量,将行中所有像素相加得到一个向量)。您可以使用这些向量来确定颜色跳跃的位置,然后在那里裁剪。

或者你可以使用Hough变换,它可以为你提供图像中的线条。如果你这样做,你可能无法注册图像。