Accumulating brightest (darkest) pixels in decompiled video

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
Aplonis
Posts: 3
Joined: 2008-09-11T12:10:58-07:00

Accumulating brightest (darkest) pixels in decompiled video

Post by Aplonis »

I have some WMV videos decompiled into stills. The subject is a faint spray mist flickering in a cross wind. The background is dark and unchanging.

Creating a single average from all the stills of decompiled video improves it slightly. Noise is eliminated and the total spray envelope a little better defined.

However, if instead of averaging, I could accumulate the brightest pixels from each frame, then each faintly glinting droplet would be retained displacing the dark background (rather than averaging with it). That should define my total spray envelope very well.

I'd then have two images to define my spray: the average (magnitude); and the maximum area.

Anybody know how to perform the accumulation-of-brighest trick?

Gan Uesli Starling
Test Engineer
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Accumulating brightest (darkest) pixels in decompiled video

Post by fmw42 »

I believe you can do it using -compose lighten -composite

It takes the brighter pixel from two input images and puts that in the resulting output

see
http://www.imagemagick.org/Usage/compose/#lighten


convert image1 image2 -compose lighten -composite result
convert result image3 -compose lighten -composite result
etc
Aplonis
Posts: 3
Joined: 2008-09-11T12:10:58-07:00

Re: Accumulating brightest (darkest) pixels in decompiled video

Post by Aplonis »

fmw42 wrote:I believe you can do it using -compose lighten -composite

It takes the brighter pixel from two input images and puts that in the resulting output

see
http://www.imagemagick.org/Usage/compose/#lighten


convert image1 image2 -compose lighten -composite result
convert result image3 -compose lighten -composite result
etc
Yes, that works very well. It does just exactly what I was wanting. Thank you.

Here is a snippet subroutine in Perl from that part of my program which implements this functionality. The if-else test for file 'accumulated.png' allows me to recall the same script on a directory containing already-done directories in addition to newly added ones.

Code: Select all

# Create one image keeping the lightest pixels from all.
# $unique is a string like 'any descriptor'.
# $files_ref is an array ref listing BMP or PNG files decomposed from WMV (or other) video in the current working directory.
sub accumulate_pics {
	my ($unique, $files_ref) = @_;
	$unique .= ' accum.png';
	if (open PIC, './accumulated.png') {
		close PIC;
		print"\t\tFile 'accumulated.png' already exists. Skipping.\n";
		copy './accumulated.png', "../$unique";
	} else {
		print "\t\tBusy accumulating ", $#$files_ref + 1, " images for lightness...\n";
		copy "$files_ref->[0]", "./accumulated.png";
		for my $l (1..$#$files_ref) {
			rename 'accumulated.png', 'base.png';
			`convert -compose lighten base.png $files_ref->[$l] -composite accumulated.png`;
		}
		unlink 'base.png';
		print "\t\tDone accumulating. See image 'accumulated.png'\n";
	}
	return 1;
}
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Accumulating brightest (darkest) pixels in decompiled video

Post by anthony »

IM can also compose MULTIPLE images down to one image

Code: Select all

  convert  image*.png  -compose lighten -flatten  accum.png
however this will need to read all the images into memory, so a 'accumulator' version
may be a better idea.

However a speed improvement may be to write the intermediate accumulator image as a MPC image (this is saved as two files). The read will is basically instantaneous, as it just load the disk file as 'memory', and while the write image size is uncompressed, it does not need any image processing (its just a raw memory dump!).
see http://www.imagemagick.org/Usage/files/#mpc

I so something similar in a script "segment_image" in the IM Examples scripts area.
http://www.imagemagick.org/Usage/scripts/segment_image
This finds and extracts one 'segment' of the image, the removes the found segment from the temporary 'accumulator' (actually a de-accumulator).

Better still is to do this in a API, like perl magick, you can read one image in at a time an accumulate them into an in-memory image, so as to aviod multiple reads and writes to the disk.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mindsocket

Re: Accumulating brightest (darkest) pixels in decompiled video

Post by mindsocket »

Apologies if reviving old posts isn't standard practice. I just wrote a Perl Magick script to do the task you describe, and thought I'd share it...

Code: Select all

#!/usr/bin/perl
#
# Combine image stack, taking lightest pixels
# usage: image_stack.pl file1.jpg file2.jpg ... filen.jpg outputfile.jpg

use Image::Magick;

$accumulator=Image::Magick->new;
$imageIterator = Image::Magick->new;
print "Loading 1st image $ARGV[0]...\n";
$accumulator->Read("jpg:$ARGV[0]");

foreach $argnum (1 .. $#ARGV - 1) {
        print "Processing $ARGV[$argnum]...\n";
        $imageIterator->Read("jpg:$ARGV[$argnum]");

        # Call the Composite method of the accumulator image, with the imageIterator image as an argument.
        $accumulator->Composite(image=>$imageIterator, compose=>'Lighten');
        @$imageIterator = ();
}

$accumulator->Set(quality=>100);
print "Writing $ARGV[$#ARGV]...\n";
$accumulator->Write("jpg:$ARGV[$#ARGV]");
Post Reply