Derive linear distance from Image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
foruforewer
Posts: 26
Joined: 2011-02-08T04:37:21-07:00
Authentication code: 8675308

Derive linear distance from Image

Post by foruforewer »

Dear Friends

I have one image file, in which from particular points, angular distance have to be derived by program.

Illustrated file and source image file is attached here with.

The angle to be derived are marked with RED color line.

Example :
Image

Source In EMF format :
http://www.megaupload.com/?d=Q3O4U70U
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

This is just math!

The problem is probably more finding the location (pixel coordinates) of the corner points.

Assuming that is the case you can look at morphology Hit and Miss to locate the points.
http://www.imagemagick.org/Usage/morphology/#hitmiss
You will want to apply appropriate LineJunction Kernels
http://www.imagemagick.org/Usage/morpho ... ejunctions
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
foruforewer
Posts: 26
Joined: 2011-02-08T04:37:21-07:00
Authentication code: 8675308

Re: Derive linear distance from Image

Post by foruforewer »

Thank you antony..

Will try and revert back to you
foruforewer
Posts: 26
Joined: 2011-02-08T04:37:21-07:00
Authentication code: 8675308

Re: Derive linear distance from Image

Post by foruforewer »

We tried this way, but we were unable to Find the (X,Y) co ordinates of specific junction and lines, so that we can derirve the distance through formula.

Let me try to describe this query in more detail.

We are having hundrades of such kind of images where the diagram is as attached and we want to

- Identify the required point and find the distanace between those points through automated program.

Thank you in Advance
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

Basically I would use morphology to convery junctions to pixels (start by negating the image to white on black)
You can the list the image using 'txt' and extract the pixels of the appropriate color. That gives you junction locations.

The main problem then is determining how those junctions are linked, so you can then determine which junctions you are interested in. For example The center junction is probably eay to find. You then need to find the set of 'close junctions', and then pick two of those that are also 'close'. You now have three points.

Really it is not hard, just a matter of taking things slowly step by step.

For this a a few more examples, or more information on the variations in the images you are measuring can help.
be do try to read up on the previous pointers I have given. A understanding of those operations will be need.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
foruforewer
Posts: 26
Joined: 2011-02-08T04:37:21-07:00
Authentication code: 8675308

Re: Derive linear distance from Image

Post by foruforewer »

Thank you very much for your prompt reply
anthony wrote: You can the list the image using 'txt' and extract the pixels of the appropriate color. That gives you junction locations.
Will you please eloborate little more on above quoted line.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

From the previous image Without the red and thresholded to make it binary again (JPG is a horrible format for exact work)

Code: Select all

   convert image.jpeg -negate -morphology HitAndMiss LineJunctions show:
will get a pixel at each point it things there is a line junction of thin lines.

As above can get small clusters of line junctions we expand then shrink those clusters down to single points (add this before the "show:" in the above)

Code: Select all

   -morphology Dilate Disk -morphology Thinning:-1 'Corners;LineEnds'
Now Replace the "show:" with "txt: | grep white" and you get the location of the junctions.

here is my complete test command (cropped to avoid the 'red' overlay you added).

Code: Select all

convert 11h52cz.jpg -crop 140x30+105+105 -threshold 50% \
            -negate -morphology HitAndMiss LineJunctions \
            -morphology Dilate Disk -morphology Thinning:-1 'Corners;LineEnds' \
            txt: | grep white
85,11: (255,255,255) #FFFFFF white
83,20: (255,255,255) #FFFFFF white
47,21: (255,255,255) #FFFFFF white
Try it out and look at what each morphology step is doing to the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

Do you have an idea. Really the problem is more of extracting the graphing vectors of the drawn image.

This is of major interest as morphology will generate stick images (skeletons), and it is the linkages that is of interest.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Speedy
Posts: 1
Joined: 2012-02-20T07:56:57-07:00
Authentication code: 8675308

Re: Derive linear distance from Image

Post by Speedy »

Its not so easy as calculating distance between cities. Its a very complected math. So, you should take a help from any expert mathematician. Then you may get answer of your math problem.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Derive linear distance from Image

Post by fmw42 »

Speedy wrote:Its not so easy as calculating distance between cities. Its a very complected math. So, you should take a help from any expert mathematician. Then you may get answer of your math problem.
Is this relevant?

The distance between any two points on a straight line is trivial. It has nothing to do with distance between cities.

dist = sqrt ( (x1-x2)^2 + (y1-y2)^2 )
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

I believe the problem is distance with constraints... that is limitin gthe distance to paths that can be taken between points.

In the case of between cities, cars are generally limited to roads, unless you have a helecopter, in which case you are limited to the surface of the planet.

The problem is finding the distance between points within the bounds of some object or shape. That is 'constrained distances'.


As it happens I have been thinking of this problem, and a iterative distance function, with a write mask (constraint) may be able to help solve it.

Start with a all white image with a single black pixel at the start point of the distance function. Now mask it with a write mask and finally run the distance function.

Code: Select all

 convert distance_start.png -mask distance_bounds.png \
            -morphology Distance Chebyshev  \
            +mask -fill black -opaque white -auto-level \
            distance_result.png
Image Image Image Image
The mask prevents the distance function from writing distance changes to the area outside the path, while a value of white in the source image ensures it does not contribute to the results (only values influenced by the single black starting pixel).

The last part turns off the mask, and makes anything still white black, before 'normalizing' the gradient to make it more visible.

Well it almost works...

It stops because IM is actually not applying the distance function iterativally, but only as a special two pass distance function. One pass forwards (top down) through the pixels in the image and one upward, both re-using the previous results immediately they are discovered. As such in this case the 'upward' pass gets the results most of the way.

Hmm running the distance function multiple times may work

Code: Select all

 convert distance_start.png -mask distance_bounds.png \
            -morphology Distance Chebyshev  \
            -morphology Distance Chebyshev  \
            +mask -fill black -opaque white -auto-level \
            distance_result_2.png
Image Image Image Image

Yes, complete coverage. Running the distance function a few times and it can go around corners, and get 'constrainted' results. (do not normalize if you actually want to make distance measurements)

The problem is I have disabled "iterative" count option for this special distance function, as it normally did not need it.
Looks like I'll have to re-enable the iterative count for distance again :-)


Warning using a large Euclidean kernel is NOT recommended as it could 'jump over' a gap in the constraint mask, You will need to stick with smaller 1 pixel radius distance kernels, Chebyshev, Manhattan, Euclidean (knight). If the gap is more than one pixel you can also use octogonal distance function (integer knight -like)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

Stop the Presses

Sorry the above is wrong. I can not just iterate the special 2-pass distance function to generate a constrained distance function.

As the function works on a row by row method, and does not directly understand write masks, it will allow the distance function to jump across vertical gaps in an image!

You can see this in this example result where the distance jumped the small gap between the start and the end of a near complete circular stroke....
Image

I will need to re-enable the much older but simpler iterative distance function to prevent it jumping gaps. :-(
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

After a quick bit of coding (the code was already there, just disabled, so it was only a dozen or so lines to re-add it) I have now re-enabled the older Iterative Distance Morphology function. For the next release of IM v6.7.5-7. Note the different method name IterativeDistance so as not to interfere with the special FAST distance function.

This is much much slower, requiring hundreds of passes through an image to calculate the distance function, but it will let you perform a true 'Constrained Distance Function'.

Code: Select all

  convert -size 100x100 xc: -draw 'point 20,80'  distance_start.png

  convert -font Pepsi -pointsize 140 label:S \
          -trim +repage -gravity center -extent 100x100 \
          -threshold 20% distance_bounds.png

  convert distance_start.png -mask distance_bounds.png \
          -morphology IterativeDistance:200 Chebyshev  \
          +mask -fill black -opaque white -auto-level \
          distance_result_4.png
Image + Image Image Image

Code: Select all

  convert -font Casual -pointsize 140 label:D \
          -trim +repage -gravity center -extent 100x100 \
          -threshold 20% distance_bounds_3.png

  convert distance_start.png -mask distance_bounds_3.png \
          -morphology IterativeDistance:200 Chebyshev  \
          +mask -fill black -opaque white -auto-level \
          distance_result_5.png
Image + Image Image Image

Both results look a lot better. However be warned that the iteration count '200' must be larger than the largest distance to be covered, and in IMv6 morphology has no way of knowing when no more changes are bing made due to the 'hidden' nature of the write mask. As such it will always do ALL the iterations counts you specify. Do NOT use -1 for a (near) infinite iteration count! It will continue for a VERY long time!

IMv7 on the other hand can see the 'write mask' and can abort its calculations for un-writable pixels, and even will have a 'read mask' that can completely ignore pixel input from pixels. This means I should 'in theory' eventually be able to re-write the 'fast' distance function to properly handle constrained objects. (that will be some heavy programming).


Now I really do have something to write up!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Derive linear distance from Image

Post by anthony »

It is now in IM example, Morphology, Constrained Distance
http://www.imagemagick.org/Usage/morpho ... nstrainted
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Retrojil
Posts: 1
Joined: 2012-03-29T13:01:15-07:00
Authentication code: 8675308

Re: Derive linear distance from Image

Post by Retrojil »

You can take a help from any expert teacher about how you will measure distance between two locations, i mean linear distance from image. I think by this way you can get the solution about it.
Post Reply