Creating a blank REFERENCE IMAGE from several images that have objects

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?".
Post Reply
RippleRatt
Posts: 8
Joined: 2016-01-14T15:05:46-07:00
Authentication code: 1151

Creating a blank REFERENCE IMAGE from several images that have objects

Post by RippleRatt »

this seemed easy but i just cant seem to get it to work.

I have a traffic camera video file output which I extracted frames at 1 second intervals using ffmpeg

now I want to create a background only image to be used as the reference image by which all others will be compared to in order to find the objects in each image .

how?

[frustrated and stupid remarks removed]
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Creating a blank REFERENCE IMAGE from several images that have objects

Post by fmw42 »

First, please always provide your Imagemagick version and platform when asking questions (and if possible a sample image). Please read viewtopic.php?f=1&t=9620, which is the very topmost post in the Users forum.

What do you mean by blank reference image? Totally white?, black? Or do you want one image from your video frames? And average of a number of images from the video? A video frame that has no moving objects in it?

Seems like you want an image which has no objects of interest in it from your video sequence. Can you not find one like that?

To make such a frame, you would have to draw regions around the objects you want to remove and use some software like inpainting to draw in similar texture to the surroundings to replace the drawn regions. Imagemagick does not yet have inpainting. But user snibgo has developed a script (Magick Filter) to do something like inpainting (hole removal). See his hole filling routines at http://im.snibgo.com/

Please clarify further what it is you actually want in the reference image?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Creating a blank REFERENCE IMAGE from several images that have objects

Post by snibgo »

This seems to be the same question as your other thread Extracting change events from a video into separate images.
You have a number of video frames that contain an almost constant background, with moving objects.

By "reference image" I think you mean an image of the constant background.

My advice remains as I said on the other thread. I'll try to simplify it. I will assume that objects always move between frames.

Suppose you have 5 frames, numbered 0 to 4.

Make a difference between frames 0 and 1. Call this d1.

Make a difference between frames 1 and 2. Call this d2.

Make a difference between frames 2 and 3. Call this d3.

Make a difference between frames 3 and 4. Call this d4.

Frame 1 contains background pixels except where d1 is white. Hence, we can negate d1 and copy-opacity to d1 to make an image that has background pixels, and transparent pixels where there was an object. Call this result b1.

Code: Select all

convert fr1.tiff ( d1.tiff -negate ) -compose CopyOpacity -composite b1.tiff
Repeat for d2, d3 and d4.

We have results b1 to b4. Each has opaque pixels (representing the background) and transparent pixels (where there were objects). The corresponding opaque pixels in each b1..b4 are the same. If we have used enough frames, every pixel is opaque in at least one result b1..b4. So we can simply flatten them to get the background.

Code: Select all

convert b1.tiff b2.tiff b3.tiff b4.tiff -background Red -layers flatten bckgrnd.tiff
Any pure red pixels in bckgrnd.tiff had a moving object in all of the source frames.
snibgo's IM pages: im.snibgo.com
RippleRatt
Posts: 8
Joined: 2016-01-14T15:05:46-07:00
Authentication code: 1151

Re: Creating a blank REFERENCE IMAGE from several images that have objects

Post by RippleRatt »

think i got it... I have some red in the darkest areas like the tires which I think will come out if i can get -fuzz to work.. no matter what i do and where i put -fuzz it just does not seem to have any effect.

some code lines:

Code: Select all

convert  "!FILE0!!EXT!"  "!FILE1!!EXT!"   -compose Difference    -composite   "REF_FILE.create.diff.1!EXT!"  

convert "!FILE0!!EXT!" ( "REF_FILE.create.diff.1!EXT!" -negate ) -compose CopyOpacity -composite "REF_FILE.create.Bkg.0!EXT!" 


convert ^
    "REF_FILE.create.Bkg.0!EXT!"  ^
    "REF_FILE.create.Bkg.1!EXT!"  ^
    "REF_FILE.create.Bkg.2!EXT!"  ^
    "REF_FILE.create.Bkg.3!EXT!"  ^
    "REF_FILE.create.Bkg.4!EXT!"  ^
    -background Red -layers flatten ^
    "!FILE0!.REF_FILE.!EXT!"   &    "!FILE0!.REF_FILE.!EXT!"  


images at https://photos.google.com/u/0/share/AF1 ... d6N0pVRnJ3

OH, I also noticed that IM appended a .number to the output filenames if i did not remove them before running each time..
I think IM did it... they were there but now i cant generate them.. ?? um?



windows 7
Version: ImageMagick 6.9.0-7 Q16 x86 2015-02-18
Features: DPC OpenMP
Delegates (built-in): bzlib cairo freetype jbig jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Creating a blank REFERENCE IMAGE from several images that have objects

Post by snibgo »

As I said on the other thread, you should do some processing on the difference. For example, this works well on the examples you supplied. Windows BAT syntax.

Code: Select all

set SRC="source image.jpg"
set REF="reference image.jpg"

%IM%convert ^
  %SRC% ^
  %REF% ^
  -compose Difference -composite ^
  -grayscale Brightness ^
  -threshold 50%% ^
  -morphology open disk:1.5 ^
  -morphology close disk:20 ^
  -morphology Thicken:-1 ConvexHull ^
  d.png
The three morphologies: (1) remove small blobs, (2) merge close blobs with each other and (3) make a convex hull from each white blob.

(2) is slow and (3) is very slow. A program such as qhull could be used instead.
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Creating a blank REFERENCE IMAGE from several images that have objects

Post by anthony »

You will need to becareful with video as the image quality (on individual frames) is typically quite low. That is why snibgo used a very high threshold for the difference comparison.

Also remember that outdoor cameras tend to have very wide ranging global changes. For example a cloud does past, and the sun (light source) moves, and disappears (night). So a single background reference image may not work for all situations.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply