Page 1 of 1

reading an analog meter face

Posted: 2016-05-16T18:41:55-07:00
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.

Re: reading an analog meter face

Posted: 2016-05-16T18:48:48-07:00
by snibgo
Upload to anywhere, like dropbox.com, and paste the URL(s) here. Two samples would be better than one.

Re: reading an analog meter face

Posted: 2016-05-17T08:59:09-07:00
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

Re: reading an analog meter face

Posted: 2016-05-17T18:23:06-07:00
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.

Re: reading an analog meter face

Posted: 2016-05-17T18:34:40-07:00
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.

Re: reading an analog meter face

Posted: 2016-05-17T18:36:21-07:00
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.

Re: reading an analog meter face

Posted: 2016-05-18T06:43:22-07:00
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.