Polynomial expression use

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
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Polynomial expression use

Post by geoland »

I'm interested in what is happening in the polynomial below. I am not sure that the expression is correct, given the following image set

0.tif 1.tif 2.tif max.tif min.tif - where

Code: Select all

convert 0.tif 1.tif 2.tif -evaluate-sequence Min min.tiff
convert 0.tif 1.tif 2.tif -evaluate-sequence Max max.tiff
With reference to the source "To remove outliers and noise... summing up all the files 0.tif 1.tif 2.tif and subtracting max.tif and min.tif; and then dividing the result by the number n of files minus 2. If we put 1/(n-2) = w, then the following ImageMagick command needs to be given:"

Code: Select all

convert 0.tif 1.tif 2.tif max.tiff min.tiff -poly '1,1,1,-1,-1' nr.tiff
or

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -function Polynomial "w,1,w,1,w,1,-w,1,-w,1" 
Which produces a bright featureless result
Should the expression be instead

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -function Polynomial "w,1,w,1,w,1,w,1,w,1" 
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polynomial expression use

Post by snibgo »

geoland wrote:Which produces a bright featureless result
Please show your inputs and output. Please also say what version of IM you are using, on what platform.

Please also show the exact command you used. "w" is invalid in this context. Perhaps you set w=1/(n-2) so w=1/(3-2)=1.

The page you linked explains the theory. At each pixel, the output is set to the mean of the inputs apart from the maximum or minimum.

For "-poly", see http://www.imagemagick.org/script/comma ... s.php#poly . It needs two numbers per input image. The second is an exponent, commonly 1.0.
snibgo's IM pages: im.snibgo.com
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-05-26 http://www.imagemagick.org
xubuntu 16.04

If I reword my question.

I have 3 sRGB images and the min and max of the image set to combine using -poly. The idea is to reject outliers and reduce noise in the image stack.

I am unsure of the correct syntax... I have looked around for examples but found nothing that helped and I am not following the poly description and usage.

This is the expression I used, as posted. And as you mention 'w' is not valid.

Code: Select all

convert 0.tif 1.tif 2.tif max.tiff min.tiff -poly '1,1,1,-1,-1' nr.tiff
This produced the same result as above with 'w' and without error... a bit confusing

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -function Polynomial "w,1,w,1,w,1,-w,1,-w,1" nr.tiff
input images 0.tif 1.tif 2.tif

Code: Select all

Colorspace: sRGB
  Depth: 16-bit
  Channel depth:
    red: 16-bit
    green: 16-bit
    blue: 16-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 11793570
    Red:
      min: 0 (0)
      max: 65535 (1)
      mean: 7989.53 (0.121912)
      standard deviation: 3511.34 (0.0535796)
      kurtosis: 173.845
      skewness: 11.908
    Green:
      min: 0 (0)
      max: 65535 (1)
      mean: 6222.62 (0.0949511)
      standard deviation: 3359.31 (0.0512597)
      kurtosis: 220.112
      skewness: 13.7577
    Blue:
      min: 0 (0)
      max: 65535 (1)
      mean: 4883.85 (0.0745228)
      standard deviation: 3348.95 (0.0511017)
      kurtosis: 222.072
      skewness: 13.6168
    Alpha:
      min: 65535 (1)
      max: 65535 (1)
      mean: 65535 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
Output is grayscale

Code: Select all

Image: nr.tiff
  Format: TIFF (Tagged Image File Format)
  Mime type: image/tiff
  Class: DirectClass
  Geometry: 4215x2798+0+0
  Resolution: 150x150
  Print size: 28.1x18.6533
  Units: PixelsPerInch
  Type: Bilevel
  Endianess: LSB
  Colorspace: Gray
  Depth: 16/1-bit
  Channel depth:
    gray: 1-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 11793570
    Gray:
      min: 65535 (1)
      max: 65535 (1)
      mean: 65535 (1)
      standard deviation: -nan (-nan)
      kurtosis: -nan
      skewness: -nan
    Alpha:
      min: 65535 (1)
      max: 65535 (1)
      mean: 65535 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
  Colors: 1
  Histogram:
  11793570: (65535,65535,65535,65535) #FFFFFFFFFFFF graya(255,1)
Last edited by geoland on 2017-06-16T05:34:36-07:00, edited 1 time in total.
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polynomial expression use

Post by snibgo »

You have three inputs: 0.tif, 1.tif and 3.tif. So 1/(3-2) = 1. Instead of "w", use "1".

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -function Polynomial "1,1,1,1,1,1,-1,1,-1,1" nr.tiff
snibgo's IM pages: im.snibgo.com
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

Thanks. The syntax makes sense now...

Output with your command produces high pixel values

Code: Select all

 Colorspace: sRGB
  Depth: 16-bit
  Channel depth:
    red: 16-bit
    green: 16-bit
    blue: 16-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 11793570
    Red:
      min: 48158 (0.734844)
      max: 65535 (1)
      mean: 58314.6 (0.889824)
      standard deviation: 1251.39 (0.019095)
      kurtosis: 14.4974
      skewness: -1.30943
    Green:
      min: 48158 (0.734844)
      max: 65535 (1)
      mean: 59935.3 (0.914554)
      standard deviation: 1092.36 (0.0166683)
      kurtosis: 31.0636
      skewness: -3.32421
    Blue:
      min: 48158 (0.734844)
      max: 65535 (1)
      mean: 61156.3 (0.933185)
      standard deviation: 1214.31 (0.0185292)
      kurtosis: 26.3659
      skewness: -3.21527
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

This works

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -poly "1,1,1,1,1,1,-1,1,-1,1" nr.tif
This is not working, though it probably should

Code: Select all

convert 0.tif 1.tif 2.tif min.tif max.tif -function Polynomial "1,1,1,1,1,1,1,-1,1,-1" nr.tif
My next question is how should I write this for 30 images - is there short hand?
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polynomial expression use

Post by snibgo »

"-function Polynomial" is entirely different. It processes only one input image.

There is no shorthand.
snibgo's IM pages: im.snibgo.com
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

Thanks for the pointers.

This is a preliminary and working bash implementation of -poly combination of multiple images, rattled up over the weekend.

I am interested in any IM tweaks that might improve the process; and while my bash scripting is quite basic, I am not asking for help with the script.

Code: Select all

#!/bin/bash

# Polystack - ImageMagick polynomial image combination. Reject outliers - noise reduction
# very basic script - copy to the folder containing the images to be stacked and move all other unnecessary files,

# remove temporary min max miff and old tiff stack from previous run - testing

rm -f *.miff *.tiff

# get the file extension

files=$(basename -a *.*)

	ext="${files##*.}"

	echo "		extension" $ext

# number of files in folder

	numfiles=$(find . -type f | wc -l)

# number of files $ext - minus this script

 	((imagefiles=$numfiles-1))

# poly weight image set 9 decimal places - how many do I need?

	w=$(echo "scale=9; 1/$imagefiles" | bc)

	echo "		image files $imagefiles	weight $w"

# poly weight variables to make -poly string

	str0="$w,1," 		# print string $imagefile times - combine any number of images
	str1="-$w,1," 		# print one time
	str2="-$w,1" 		# print one time

	echo $str0

# print $str0 $imagefile times
	wstr=$(for i in $(seq 1 $imagefiles); do printf "$str0"; done)

	echo "		wstr $wstr"

# compute image set min

	convert *.$ext -evaluate-sequence Min min.miff

# compute image set max

	convert *.$ext -evaluate-sequence Max max.miff

# print poly string

	polystack=$(printf "convert *."$ext" min.miff max.miff -poly ""$wstr""$str1""$str2"" stack.tiff")

	echo "		Polystack $polystack"

# run polynomial stacking routine

	$polystack

	display stack.tiff

exit
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polynomial expression use

Post by snibgo »

geoland wrote:I am interested in any IM tweaks that might improve the process
Performance can be improved, at the expense of complexity and memory usage. For example, combine the first two converts ...

Code: Select all

convert *.$ext -evaluate-sequence Min min.miff
convert *.$ext -evaluate-sequence Max max.miff
... into ...

Code: Select all

convert \
(clone 0--1 *.$ext -evaluate-sequence Min +write min.miff +delete ) \
-evaluate-sequence Max max.miff
With more work, all three converts could be combined, so the input files would be read just once.
snibgo's IM pages: im.snibgo.com
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

Most likely a bash thing but it complains about the brackets. Removing the brackets, file max.miff is not found and is not being used. I have tried this various ways, including swapping the Max and Min order.

No max.miff file visible in folder and not being used.

Code: Select all

convert \
clone 0--1 *.$ext -evaluate-sequence Min +write min.miff +delete \
-evaluate-sequence Max max.miff
Of course, removing +delete, max.miff is visible in folder but not used - evidenced by no rejection of outliers, such as satellite trails in test images.

Code: Select all

convert \
clone 0--1 *.$ext -evaluate-sequence Min +write min.miff \
-evaluate-sequence Max max.miff
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Polynomial expression use

Post by fmw42 »

Snibgo has some typos. There must be spaces between parenthesis and enclosing and outside text. Also parenthesis must be escaped in unix. So his Unix command should be

Code: Select all

convert *.$ext  \
\( clone 0--1 -evaluate-sequence Min +write min.miff +delete \) \
-evaluate-sequence Max max.miff
Or as one line

Code: Select all

convert *.$ext  \( clone 0--1 -evaluate-sequence Min +write min.miff +delete \) -evaluate-sequence Max max.miff

In Windows this would be

Code: Select all

convert *.$ext ^
( clone 0--1 -evaluate-sequence Min +write min.miff +delete ) ^
-evaluate-sequence Max max.miff
Or as one line

Code: Select all

convert *.$ext ( clone 0--1 -evaluate-sequence Min +write min.miff +delete ) -evaluate-sequence Max max.miff
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Polynomial expression use

Post by snibgo »

fmw42 wrote:Snibgo has some typos.
True. Thanks for the corrections.
snibgo's IM pages: im.snibgo.com
geoland
Posts: 74
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Polynomial expression use

Post by geoland »

Thank you both. Forgot about escape parenthesis. The script works very well.

Code: Select all

-clone
Version: ImageMagick 7.0.7 (latest compiled from source) Q32 x86_64 (as of) 2018 - 04 - 31 xubuntu
Post Reply