Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

I wonder what would be the best way to use ImageMagick to identify a person (or a dog) walking over a grass in a sunny day, using a fixed camera.

For a cloudy day, that is not a problem, as you can see in the following image:

http://i1039.photobucket.com/albums/a47 ... vyhxjr.jpg

The image at the center is the reference image, while left image is the one with an intrusor (a man and a dog). In the right you can see the image obtained with the following command:

Code: Select all

    
    compare $f a0.jpg -fuzz 20% -metric FUZZ saida.jpg
    convert $f a0.jpg saida.jpg +append saida2_$f.jpg
$ f is the file that I want to examine.

As you can see, it worked fine, except for the situation when the grass at the background is partially iluminated by sun. See below:

http://s1039.photobucket.com/user/vpmam ... b.jpg.html

In the case above, the grass at the background is partially illuminated by sun, causing a very bright area which is identified as a feature by the difference algorithm.

I wonder if there is a way, in ImageMagick, to disregard shuch a effect using some sort of statistical technique. I would try some sort of co-variance method, but this is just a guess. Something like a variogram, or equivalent.

Is anybody interested on this topic?
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

Forgot to say that in the examples the dog and the man are inside the balcony. Just a detail.

Moreover, I forgot to say that a possible solution would be based on using a set of fixed reference images as the basis for comparison.

Here I am not concerned with camera movement, but background movement may be an issue... Right now I won't deal with background movement complexity and will consider that the background is fixed either.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

I will try. Thank you for the quick response.
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

Well, I tried your suggestion, but perhaps I used wrong technique.

See:

Image

This image is devided in 3x2 areas: the upper line was obtained from the code below (already mentioned above):

Code: Select all

    
    compare $f a0.jpg -fuzz 20% -metric FUZZ saida.jpg
    convert $f a0.jpg saida.jpg +append saida2_$f.jpg
The bottom line was obtained by the code below:

Code: Select all

convert a0.jpg -define convolve:scale='50%!' -bias 50% -morphology Convolve Sobel:90 a0_e.jpg
convert a2.jpg -define convolve:scale='50%!' -bias 50% -morphology Convolve Sobel:90 a2_e.jpg
convert a2_e.jpg a0_e.jpg saida.jpg +append s_e.jpg
As you can see, at least through the method above, using edges is not meaningful.

Any other idea?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

You need to threshold the edges or use the canny edge detector, which can remove short edges and less strong edges.
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

thank you!

would you be more specific?

how could I do this edge threshold?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

I suggest the Canny edge detector, since it creates binary edges and can be tuned to remove short edges and weak edges. See viewtopic.php?f=4&t=25405

You could also post-filter your original compare results using -connected-components processing. See http://magick.imagemagick.org/script/co ... onents.php
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

ok, I will try.

thank you!

I will post the images here later.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

EDITED: For connected-components processing you may want to use

Code: Select all

convert image1 image2 -compose difference -composite -threshold XX% result.gif
rather than compare. Then do the CCL.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

Here is an example:

Image
Image

Code: Select all

convert image1.png image2.png -compose difference -composite diff.png
Image

Code: Select all

convert diff.png -threshold 35% diff_t35.png
Image

Code: Select all

convert diff_t35.png -define connected-components:verbose=true -connected-components 4 null:
Objects (id: bounding-box centroid area mean-color):
  0: 317x240+0+0 158.1,116.0 72563 gray(0)
  100: 92x68+109+172 156.2,207.7 2475 gray(255)
  101: 43x21+130+219 152.5,230.9 647 gray(0)
  35: 6x10+134+8 136.6,12.6 39 gray(255)
  5: 6x6+57+0 59.9,3.0 19 gray(255)
  7: 6x8+104+0 106.6,3.3 18 gray(255)
  6: 9x3+90+0 94.1,0.6 16 gray(255)
  11: 4x6+201+0 202.5,2.2 13 gray(255)
  9: 4x8+118+0 119.8,4.1 12 gray(255)
  104: 5x4+164+234 165.5,235.3 12 gray(255)
  28: 4x4+86+7 87.7,8.7 11 gray(255)
  57: 5x4+199+14 201.3,15.9 11 gray(255)
  19: 7x4+181+3 183.7,5.0 10 gray(255)
  3: 3x3+28+0 29.1,0.9 8 gray(255)
  20: 3x3+69+4 69.9,5.1 8 gray(255)
  1: 4x3+24+0 25.5,1.1 8 gray(255)
  27: 4x3+76+7 78.0,7.8 8 gray(255)
  44: 4x2+202+10 203.7,10.6 7 gray(255)
  16: 3x5+103+3 104.0,5.0 7 gray(255)
  33: 3x4+110+8 111.0,9.3 7 gray(255)
  62: 3x2+252+16 253.0,16.5 6 gray(255)
  42: 4x2+274+9 275.5,9.7 6 gray(255)
  55: 3x2+165+13 166.0,13.5 6 gray(255)
  82: 4x2+205+23 206.6,23.2 5 gray(255)
  105: 2x3+125+236 125.6,237.2 5 gray(0)
  91: 4x2+271+29 272.6,29.8 5 gray(255)
  79: 4x1+199+20 200.5,20.0 4 gray(255)
  75: 3x2+189+19 190.2,19.2 4 gray(255)
  86: 2x3+235+28 235.8,29.0 4 gray(255)
  77: 2x2+265+19 265.5,19.5 4 gray(255)
  60: 2x3+166+16 166.5,17.0 4 gray(255)
  80: 4x1+275+20 276.5,20.0 4 gray(255)
  84: 4x1+198+25 199.5,25.0 4 gray(255)
  74: 2x2+136+19 136.5,19.5 4 gray(255)
  39: 2x2+99+9 99.5,9.5 4 gray(255)
  76: 3x1+205+19 206.0,19.0 3 gray(255)
  87: 3x1+265+28 266.0,28.0 3 gray(255)
  81: 3x1+173+21 174.0,21.0 3 gray(255)
  17: 2x2+116+3 116.3,3.3 3 gray(255)
  95: 3x1+290+32 291.0,32.0 3 gray(255)
  65: 1x3+271+16 271.0,17.0 3 gray(255)
  98: 3x1+314+39 315.0,39.0 3 gray(255)
  78: 3x1+282+19 283.0,19.0 3 gray(255)
  51: 2x2+277+12 277.7,12.7 3 gray(255)
  64: 3x1+264+16 265.0,16.0 3 gray(255)
  31: 3x1+261+7 262.0,7.0 3 gray(255)
  45: 1x2+233+10 233.0,10.5 2 gray(255)
  43: 1x2+287+9 287.0,9.5 2 gray(255)
  48: 1x2+199+11 199.0,11.5 2 gray(255)
  73: 2x1+314+18 314.5,18.0 2 gray(255)
  32: 2x1+101+8 101.5,8.0 2 gray(255)
  41: 1x2+271+9 271.0,9.5 2 gray(255)
  2: 1x2+27+0 27.0,0.5 2 gray(0)
  70: 1x2+269+17 269.0,17.5 2 gray(255)
  10: 1x2+162+0 162.0,0.5 2 gray(255)
  88: 2x1+301+28 301.5,28.0 2 gray(255)
  34: 2x1+116+8 116.5,8.0 2 gray(255)
  66: 2x1+301+16 301.5,16.0 2 gray(255)
  58: 1x2+191+15 191.0,15.5 2 gray(255)
  12: 2x1+256+0 256.5,0.0 2 gray(255)
  103: 1x2+109+233 109.0,233.5 2 gray(255)
  102: 1x2+164+230 164.0,230.5 2 gray(255)
  46: 1x2+119+11 119.0,11.5 2 gray(255)
  107: 1x1+131+238 131.0,238.0 1 gray(255)
  4: 1x1+34+0 34.0,0.0 1 gray(255)
  8: 1x1+110+0 110.0,0.0 1 gray(255)
  13: 1x1+167+1 167.0,1.0 1 gray(255)
  14: 1x1+40+2 40.0,2.0 1 gray(255)
  15: 1x1+165+2 165.0,2.0 1 gray(255)
  18: 1x1+158+3 158.0,3.0 1 gray(255)
  21: 1x1+113+4 113.0,4.0 1 gray(255)
  22: 1x1+92+5 92.0,5.0 1 gray(255)
  23: 1x1+163+6 163.0,6.0 1 gray(255)
  24: 1x1+167+6 167.0,6.0 1 gray(255)
  25: 1x1+175+6 175.0,6.0 1 gray(255)
  26: 1x1+72+7 72.0,7.0 1 gray(255)
  29: 1x1+103+7 103.0,7.0 1 gray(255)
  30: 1x1+125+7 125.0,7.0 1 gray(255)
  36: 1x1+175+8 175.0,8.0 1 gray(255)
  37: 1x1+191+8 191.0,8.0 1 gray(255)
  38: 1x1+201+8 201.0,8.0 1 gray(255)
  40: 1x1+200+9 200.0,9.0 1 gray(255)
  47: 1x1+122+11 122.0,11.0 1 gray(255)
  49: 1x1+117+12 117.0,12.0 1 gray(255)
  50: 1x1+201+12 201.0,12.0 1 gray(255)
  52: 1x1+118+13 118.0,13.0 1 gray(255)
  53: 1x1+120+13 120.0,13.0 1 gray(255)
  54: 1x1+159+13 159.0,13.0 1 gray(255)
  56: 1x1+183+14 183.0,14.0 1 gray(255)
  59: 1x1+159+16 159.0,16.0 1 gray(255)
  61: 1x1+232+16 232.0,16.0 1 gray(255)
  63: 1x1+262+16 262.0,16.0 1 gray(255)
  67: 1x1+313+16 313.0,16.0 1 gray(255)
  68: 1x1+171+17 171.0,17.0 1 gray(255)
  69: 1x1+256+17 256.0,17.0 1 gray(255)
  71: 1x1+300+18 300.0,18.0 1 gray(255)
  72: 1x1+303+18 303.0,18.0 1 gray(255)
  83: 1x1+295+23 295.0,23.0 1 gray(255)
  85: 1x1+242+26 242.0,26.0 1 gray(255)
  89: 1x1+143+29 143.0,29.0 1 gray(255)
  90: 1x1+225+29 225.0,29.0 1 gray(255)
  92: 1x1+294+30 294.0,30.0 1 gray(255)
  93: 1x1+247+31 247.0,31.0 1 gray(255)
  94: 1x1+305+31 305.0,31.0 1 gray(255)
  96: 1x1+136+34 136.0,34.0 1 gray(255)
  97: 1x1+287+34 287.0,34.0 1 gray(255)
  99: 1x1+286+120 286.0,120.0 1 gray(255)
  106: 1x1+131+237 131.0,237.0 1 gray(0)

Now remove everything but the largest white/gray(255) area

Code: Select all

convert diff_t35.png -define connected-components:area-threshold=2475 -connected-components 4 -auto-level dog.gif
Image
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

WOW!

Great!

I wonder if the threshold selected would work for all contrast conditions?

I will make a try with several images, different conditions, and will return here.

Thanks a lot! Hope this questions are useful for other people.
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

I am getting:

convert.im6: unrecognized option `-connected-components' @ error/convert.c/ConvertImageCommand/1107

Perhaps a problem with the IM version?
vpmammana
Posts: 37
Joined: 2015-12-06T14:13:46-07:00
Authentication code: 1151

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by vpmammana »

I tried the following code:

Code: Select all

#!/bin/bash
for f in D*.jpg
do 
    echo "Processing $f file.."
    convert $f a0.jpg -compose difference -composite saida.jpg
    convert saida.jpg -threshold 3% saida35.jpg
    convert saida35.jpg -define connected-components:area-threshold=2000 -connected-components 4 -auto-level saidafinal.jpg
    convert $f a0.jpg saida.jpg saida35.jpg saidafinal.jpg +append saida2_$f.jpg
    convert saida2_$f.jpg -append saida3.jpg
done;
Since the part related to "connected-components" is not working right now (see post above and the error message), I got only 4 files that I +appended to a row with 4 images.

See what I got with 3% of threshold.

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using ImageMagick to identify a person walking over grass for a fixed camera in a sunny day

Post by fmw42 »

What version of IM are you using? CCL was only added at 6.8.9.10
Post Reply