reading an analog meter face

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Post Reply
alpinekid
Posts: 8
Joined: 2016-05-16T16:38:29-07:00
Authentication code: 1151

reading an analog meter face

Post by alpinekid »

I'm working on a project that requires the reading of the needle of a meter. It a basic needle like in the old days. red, fat the pivit point and tapering to a point. The problem reduces to getting the angle of the needle.

I have created a workflow that filters the image and gets a clean mask of the needle. Ive been playing with different morphologies to clean up the shape and reduce it to a skeleton. I've got a wiggle skeleton but I'm at a loss on how to get a straight line or the get the angle of the line. If I can get clean end point then I'm ok. The accuracy is very loose so anything close is good enough.
I need to read and analyze an image every few seconds.

I'm new to computer vision and looking for guidance. I'd attach the skeleton image if I knew how.
Thanks for your time and effort,
Al
gnu/linux, ubuntu 14.4 distribution, ImageMagick 7.0.1-3 Q16 x86_64; compiled from source
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reading an analog meter face

Post by snibgo »

Upload to anywhere, like dropbox.com, and paste the URL(s) here. Two samples would be better than one.
snibgo's IM pages: im.snibgo.com
alpinekid
Posts: 8
Joined: 2016-05-16T16:38:29-07:00
Authentication code: 1151

Re: reading an analog meter face

Post by alpinekid »

Thanks for your interest. here is a sample file. I'll try to get another one prepared today.

The little triangle thing is going to be removed. I'm only interested in the long structure. I'm trying to get a clean line from the hub to the tip. I really just need the angle relative to the bottom of the image.

the result of masking and morphologies Image

I started with this image.
Image

These files were generated by
echo "select just the dial face by using a mask" >&2;
convert ${assetDir}/Meter-test1.png ${assetDir}/dialMask.gif \
-alpha Off -compose CopyOpacity -composite \
-colorspace HSB -separate \
${tmpDir}/separate_HSB_%d.gif

echo "blur and threshold to get needle mask" >&2
convert ${tmpDir}/separate_HSB_0.gif \
-morphology Convolve Gaussian:0x2 \
-white-threshold 90% -black-threshold 90% \
-morphology Close Diamond \
${tmpDir}/needle_mask.gif

echo "convert to skeleton" >&2
convert ${tmpDir}/needle_mask.gif \
-morphology Close Diamond \
-morphology Thinning:-1 Skeleton \
${tmpDir}/needle.gif
Thanks for your time and effort,
Al
gnu/linux, ubuntu 14.4 distribution, ImageMagick 7.0.1-3 Q16 x86_64; compiled from source
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reading an analog meter face

Post by snibgo »

Code: Select all

convert needle.gif -hough-lines 3x3+100 h.mvg
h.mvg then contains:

Code: Select all

# Hough line transform: 3x3+100
viewbox 0 0 1024 1024
line 796.308,0 181.027,1024  # 114
line 798.641,0 183.36,1024  # 112
line 791.438,0 200.231,1024  # 334
line 782.086,0 214.474,1024  # 106
The longest line (334), when extended to the image boundaries, has ends at (791.438,0) and (200.231,1024).

Simple trig gives the angle, eg atan2(dx,dy).

If the needle can traverse more than 180 degrees, this doesn't tell you which way round the needle is pointing. One way to get that would be to take needle.gif, draw a big black circle centred at the image centre, and find the coordinates of the first white pixel. This tells you which quadrant the needle is in.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reading an analog meter face

Post by snibgo »

An alternative method is to de-polar the second image (needle_mask.gif), using the needle axis as the centre. See "-distort depolar" docs. Then the x-coordinate of the bottom-most white pixel, divided by that image width, multiplied by 360 is the number of degrees anti-clockwise from north. See my "Polar distortion" page for details.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: reading an analog meter face

Post by fmw42 »

atan2() should provide full 360 degree results if the signs of dx and dy are taken into account such that dx=(x2-x1) and dy=(y2-y1), where x1,y1 is the center and x2,y2 is the endpoint or arrow end. Assume the closest of the two points to the image center is x1,y1.
alpinekid
Posts: 8
Joined: 2016-05-16T16:38:29-07:00
Authentication code: 1151

Re: reading an analog meter face

Post by alpinekid »

Thanks that is exactly what I needed. I had seen hough stuff but didnt know how to use it .

I'm using
magick -version
Version: ImageMagick 7.0.1-3 Q16 x86_64 2016-05-16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): bzlib fftw fontconfig freetype jbig jng jpeg lcms lzma openexr pangocairo png tiff webp wmf x xml zlib

and

magick -list command | grep hough

didnt show that command so I figured it was some special lib I was missing and didn't investigate. but I tried it and it works.
I have some more code to write to use it effectively but magick has done its job.


Thanks.
Thanks for your time and effort,
Al
gnu/linux, ubuntu 14.4 distribution, ImageMagick 7.0.1-3 Q16 x86_64; compiled from source
Post Reply