Support the Arctic Sea Ice Forum and Blog

Author Topic: Creating Animated GIFs  (Read 69203 times)

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119
Re: Creating Animated GIFs
« Reply #100 on: July 06, 2019, 03:44:13 PM »
Let me know if you keep getting stuck in the same place. It's easy to miss out a step when writing instructions. Adjusting contrast before rgb compose may have a more striking effect, but only you know what you're aiming for :)

petm

  • Grease ice
  • Posts: 675
    • View Profile
  • Liked: 334
  • Likes Given: 27
Re: Creating Animated GIFs
« Reply #101 on: July 22, 2019, 01:47:22 AM »
Following up on: https://forum.arctic-sea-ice.net/index.php/topic,2591.msg214848.html#msg214848 . Not sure if this is the best thread for this, since I actually just use EZGif's website to make the GIFs once I've created the images, but it can always be moved if appropriate...

Repeating the summary from the thread above:
Quote
I do it using the imageJ API (Java code), basically by: for each day, reading in the 5 preceding days' images, converting each pixel color to a concentration value based on the Bremen key, taking the median for each pixel, then creating a new image. (Actually, the algorithm is easily changed -- 5 day median is quite conservative.)

The code is a bit of a hack, so I'll just post some snippets:

0. Make a key to convert colors to concentrations. The key was made by using the ImageJ GUI to manually pick indices corresponding to each color in the key of one particular image. (ImageJ LUT objects store pixel values as a 1d array; indices refer to that array.)
Quote
int[] concBuckets = {10, 20, 30, 40, 50, 60, 70, 80, 85, 90, 95, 99,100};
int[] indices = {14, 84, 92,104,110,105, 80,100,124,170, 79,177,174}; 
for (int i=0; i<concBuckets.length; i++) {
   this.rgbToConc.put(
      this.keyImage.getProcessor().getLut().getRGB(indices), concBuckets);
}

For each day do all of the following steps:
Quote
for (int n=0; n < this.nDays; n++) { ... }

1. Download the needed number of preceding images (e.g. five, if making a trailing 5-day median) from their URLs or get from cache if already downloaded. URLs are similar to (substitute the dates):
 - https://seaice.uni-bremen.de/data/amsr2/asi_daygrid_swath/n6250/2017/jul/Arctic/asi-AMSR2-n6250-20170731-v5_nic.png
Quote
ImagePlus image = null;
String filename = this.getFilename(date, bremenVersion);
File file = new File(this.cacheDir + filename);
if (file.exists()) {
            System.out.println("Loading image from file: " + this.cacheDir + filename);
            image = new ImagePlus(this.cacheDir + filename);
} else {
            String url = this.getUrl(date, filename);
            System.out.println("Downloading image from web: " + url);
            image = new ImagePlus(url);
}

2. For each preceding image, convert the colors to concentrations using the key.
Quote
int[] conc = new int[this.len];
LUT lut = ip.getLut();
byte[] pixels = (byte[]) ip.getPixels();
for (int y=0; y < ip.getHeight(); y++) {
            for (int x=0; x < ip.getWidth(); x++) {
                int i = x + y * ip.getWidth();
                int rgb = lut.getRGB(pixels);
                if (this.rgbToConc.containsKey(rgb)){
                    conc = Integer.parseInt(this.rgbToConc.get(rgb).toString());
                } else {
                    conc = Integer.MAX_VALUE;
                }
            }
}

3. Use this set of concentrations to calculate the output values; e.g. for median (i.e. quartile = 2):
Quote
for (int i=0; i<this.len; i++) {
            int filteredC = this.concentrations.get(0);
            int[] values = new int[this.concentrations.size()];
            for (int z=0; z<values.length; ++z) {
                values[z] = this.concentrations.get(z);
            }
            Arrays.sort(values);
            if (values.length % 2 == 0) {
                throw new RuntimeException("Can't use quartile for even number of ice concentrations.");
            } else {
                int q1 = values.length/4;   // for values.length == 5, q1 == 1 (floor of 1.25)
                filteredC = values[q1*quartile];
            }
            this.filteredConcs = filteredC;
}

4. Finally, convert the concentrations back into RGB color values and use that as the pixel array to generate a new image.

5. Now I have a set of images, each being the (e.g.) 5-day trailing median. I just use EZGif online to convert them to an animation. To use a different algorithm, just change step 3.

« Last Edit: July 22, 2019, 02:05:58 AM by petm »

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119
Re: Creating Animated GIFs
« Reply #102 on: July 22, 2019, 10:41:18 PM »
thanks petm, useful stuff

Sterks

  • Guest
Re: Creating Animated GIFs
« Reply #103 on: July 25, 2019, 09:46:01 PM »
Following up on: https://forum.arctic-sea-ice.net/index.php/topic,2591.msg214848.html#msg214848 . Not sure if this is the best thread for this, since I actually just use EZGif's website to make the GIFs once I've created the images, but it can always be moved if appropriate...

Repeating the summary from the thread above:
Quote
I do it using the imageJ API (Java code), basically by: for each day, reading in the 5 preceding days' images, converting each pixel color to a concentration value based on the Bremen key, taking the median for each pixel, then creating a new image. (Actually, the algorithm is easily changed -- 5 day median is quite conservative.)

The code is a bit of a hack, so I'll just post some snippets:

I don’t think I have the Java skills to achieve this in a reasonable time. Thank you anyway and hope you post more filtered gifs when you find the time. I really enjoy them, I don’t think five day average is conservative, actually it leaves the shortest characteristic time that makes sense. Weather patterns change with that pace usually, not faster.

petm

  • Grease ice
  • Posts: 675
    • View Profile
  • Liked: 334
  • Likes Given: 27
Re: Creating Animated GIFs
« Reply #104 on: August 08, 2019, 01:50:40 AM »
Oof, it turns out I made an error in calculating the index for getting median values from a sorted array. The calculation was only correct for 5-day medians (zero-based index 2). For 3-day medians it was returning the minimum (index 0; should be index 1).

However, this may have been a fortunate error, as it turns out that I actually like the minimum algorithm. It's not conservative, but if it's true that the biggest artifacts are clouds, then this does the best job of eliminating them. On the other hand, maybe it overestimates the amount of melt too much?

Attached is a gif that cycles through each index for today's result. Index 0 is minimum, 2 is median, 4 is maximum, and 1 and 3 are intermediate (something like Q1 and Q3).

Click to animate.

Any comments?

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119
Re: Creating Animated GIFs
« Reply #105 on: August 08, 2019, 02:03:59 AM »
Any comments?
Run all 5 over the last 5years and anecdotally compare them to available clear worldview days. Then satisfy yourself which is best. I'll be very grateful :)

petm

  • Grease ice
  • Posts: 675
    • View Profile
  • Liked: 334
  • Likes Given: 27
Re: Creating Animated GIFs
« Reply #106 on: August 08, 2019, 02:05:09 AM »
Any comments?
Run all 5 over the last 5years and anecdotally compare them to available clear worldview days. Then satisfy yourself which is best. I'll be very grateful :)

Good idea. I'd be interested to see as well. It'll have to wait for the weekend though...

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119
Re: Creating Animated GIFs
« Reply #107 on: August 08, 2019, 02:10:28 AM »
well, maybe not 5yrs

petm

  • Grease ice
  • Posts: 675
    • View Profile
  • Liked: 334
  • Likes Given: 27
Re: Creating Animated GIFs
« Reply #108 on: August 08, 2019, 02:13:30 AM »
lol  :D

I'll find some good examples and see if there's a clear trend.

El Cid

  • Grease ice
  • Posts: 500
    • View Profile
  • Liked: 142
  • Likes Given: 39
Re: Creating Animated GIFs
« Reply #109 on: August 08, 2019, 08:06:50 AM »
If you find out which one is best, please tell us about it and you could also post it on the main thread as it is important and educative

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119
Re: Creating Animated GIFs
« Reply #110 on: August 08, 2019, 12:08:04 PM »
Thinking about it overnight, a median has its uses but I prefer the detail of the daily. Weather artifacts are easy to see in an animation and add further information.

uniquorn

  • Nilas ice
  • Posts: 1350
    • View Profile
  • Liked: 657
  • Likes Given: 119