I don't understand color calculation [solved]
I don't understand color calculation [solved]
I'm trying to calculate the ndvi index using imagemagick in spite of proprietary software, but I obtain different results using different methods with the same mathematical expression.
In a linux console:
convert d020171013.jpg separate d0%d.png
convert d00.png d01.png d02.png fx '(u[2])(u[0]) / (u[2])+(u[0])' d0ndvi.png
0, 1 and 2 images correspond to rgb channels; u[0] correspond to the first file and u[2] to the third
I obtain a b/n image that should correspond to the ndvi index.
Typing
convert d020171013.jpg fx "(u.bu.r)/(u.b+u.r)" d0ndvi2.png,
where u.b is the blue channel and u.r is the red channel, I obtain a total black image.
Can you help me to understand why?
Thank you in advance.
This is the image
https://www.flickr.com/photos/152450284 ... edpublic/
ndvi index
https://it.wikipedia.org/wiki/Normalize ... tion_Index
In a linux console:
convert d020171013.jpg separate d0%d.png
convert d00.png d01.png d02.png fx '(u[2])(u[0]) / (u[2])+(u[0])' d0ndvi.png
0, 1 and 2 images correspond to rgb channels; u[0] correspond to the first file and u[2] to the third
I obtain a b/n image that should correspond to the ndvi index.
Typing
convert d020171013.jpg fx "(u.bu.r)/(u.b+u.r)" d0ndvi2.png,
where u.b is the blue channel and u.r is the red channel, I obtain a total black image.
Can you help me to understand why?
Thank you in advance.
This is the image
https://www.flickr.com/photos/152450284 ... edpublic/
ndvi index
https://it.wikipedia.org/wiki/Normalize ... tion_Index
Last edited by supermax on 20171022T09:41:2407:00, edited 1 time in total.

 Posts: 10493
 Joined: 20100123T23:01:3307:00
 Authentication code: 1151
 Location: England, UK
Re: I don't understand color calculation
Is that what you intended? Look at the parentheses.supermax wrote:(u[2])(u[0]) / (u[2])+(u[0])
Where blue is less than red, the value will be negative, which clamps to black. In your image, blue is mostly less than red. Try adding 0.5.supermax wrote:(u.bu.r)/(u.b+u.r)
snibgo's IM pages: im.snibgo.com
 fmw42
 Posts: 23617
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: I don't understand color calculation
try
If the index can be negative, then you need to use HDRI imagemagick and file format such as PFM or TIFF that supports negative values.
Or as snibgo said, add a bias of 0.5.
Also
or
Code: Select all
(u[2]u[0]) / (u[2]+u[0])
Or as snibgo said, add a bias of 0.5.
Also
You do not use d01.png, so you don't need to include it.supermax wrote:convert d00.png d01.png d02.png fx '(u[2])(u[0]) / (u[2])+(u[0])' d0ndvi.png
Code: Select all
convert d00.png d02.png fx (u[1]u[0]) / (u[1]+u[0])' d0ndvi.png
Code: Select all
convert d020171013.jpg separate delete 1 fx (u[1]u[0]) / (u[1]+u[0])' d0ndvi.png
Re: I don't understand color calculation
Thank you for your reply.
Yes, adding 0.5 gives a brighter images but with different contrast.
The range of Ndvi index should be between 1 and 1; I was wondering how the different commands 'convert' it in the range of 0255 values.
I'm going to compare a series of images of differently fertilized plants and I would like to avoid unexpected gamma or contrast corrections at first.
Yes, adding 0.5 gives a brighter images but with different contrast.
The range of Ndvi index should be between 1 and 1; I was wondering how the different commands 'convert' it in the range of 0255 values.
I'm going to compare a series of images of differently fertilized plants and I would like to avoid unexpected gamma or contrast corrections at first.
 fmw42
 Posts: 23617
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: I don't understand color calculation
try adding 1 and dividing by 2, so that the range is 0 to 1.
Re: I don't understand color calculation
Near the plants, I putted a white target that I cropped in white.jpg.
convert white.jpg resize 1x1\! format "%[pixel:u]\n" info: >white.txt
white.txt contain this text line:
srgb(255,170,231)
I tried to make a white balance adding *255/231 to the blue channel and follow fmw42 suggestion.
convert d020171013.jpg separate delete 1 fx '((u[1]*255/231u[0]/ u[1]*255/231+u[0])+1)/2' d0ndvi.png
convert d020171013.jpg fx "((u.b*255/231u.r)/(u.b*255/231+u.r)+1)/2" d0ndvi2.png
I expected the two images to be identical. Why are they different?
https://www.flickr.com/photos/152450284@N03/
The first is d0ndvi.png
The second is d0ndvi2.png
convert white.jpg resize 1x1\! format "%[pixel:u]\n" info: >white.txt
white.txt contain this text line:
srgb(255,170,231)
I tried to make a white balance adding *255/231 to the blue channel and follow fmw42 suggestion.
convert d020171013.jpg separate delete 1 fx '((u[1]*255/231u[0]/ u[1]*255/231+u[0])+1)/2' d0ndvi.png
convert d020171013.jpg fx "((u.b*255/231u.r)/(u.b*255/231+u.r)+1)/2" d0ndvi2.png
I expected the two images to be identical. Why are they different?
https://www.flickr.com/photos/152450284@N03/
The first is d0ndvi.png
The second is d0ndvi2.png
 fmw42
 Posts: 23617
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: I don't understand color calculation
Parenthesis problem. Divides happen before adds or subtracts. In the above you have u[0]/u[1] which is not what you want. So you need to put paretheses around your subtracts before dividing.'((u[1]*255/231u[0]/u[1]*255/231+u[0])+1)/2
Code: Select all
'((u[1]*255/231u[0])/(u[1]*255/231+u[0])+1)/2
Re: I don't understand color calculation
It is not a parenthesis is problem. It gives the same solution because subtraction come later than division.
I think I found the solution converting to text file:
convert image.png image.txt
replies an image in which values are proportionally ranged between 0 and 255.
0.5 become 128
thanke you all!
I think I found the solution converting to text file:
convert image.png image.txt
replies a b/n image in which the minimum value become '0' and the maximum '255'.
the range of the results is between 0 and 1
replies an image in which values are proportionally ranged between 0 and 255.
0.5 become 128
thanke you all!
Last edited by supermax on 20171023T02:59:1907:00, edited 1 time in total.
 fmw42
 Posts: 23617
 Joined: 20070702T17:14:5107:00
 Authentication code: 1152
 Location: Sunnyvale, California, USA
Re: I don't understand color calculation [solved]
In this statement, the order of processing is something like:'((u[1]*255/231u[0]/u[1]*255/231+u[0])+1)/2
u[1]*255/231
u[0]/u[1]*255/231
u[0]
then subtract the first and second terms and add the third. That is not what you want.
You want
((u[1]*255/231u[0])
(u[1]*255/231+u[0]))
then divide the first by the second.
That is why I added the extra parens in these last two expressions