0

My computer (i5-6500 3.2 GHZ, 8 GB RAM) takes a long time: something like 10 minutes (havent yet measured exactly).

i currently have to

  1. read 400 images. (*.gif format, should all be b&w, resolution of approx. 200*400 px.) (3520 images in total)

  2. i want to "add" all images "cell-wise".

here is how im doing it at the moment: Read image with raster than turn it into matrix, then sum it.

library(rgdal)
library(raster)
library(magrittr)

oldPic <- raster("initalImage.gif") %>% as.matrix

for (pat_IND in currSide) {
    newPic <- raster(pat_IND) %>% as.matrix
    oldPic <- oldPic + newPic
}

This takes for ever. I used caTools::read.gif() which was even much slower. Do i have a bottle neck in my code? Is there a faster implementation?

Edit: Image Properties

enter image description here

i use "no dither", mono palette (b&w).

Edit2

i want to add the images pixel-wise. Lets take pic A and pic B.

A + B = C. If A(1,1) = 1 and B(1,1) = 1, C(1,1) should be 2. Its a simple matrix addition.

test image:

img

  1. reading with raster takes 0.03699994 secs
  2. reading with raster + as.matrix takes: 0.201 secs
Spektre
  • 49,595
  • 11
  • 110
  • 380
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • not sure what are the times of ... by #1 you mean decoding time of GIF and by #2 decoding time of GIF + conversion from raster to matrix ? if yes then booth times are too big (5 times bigger then mine GIF loader probably not well optimized loader) but it looks like the matrix stuff is your problem try to write the conversion your self ... also check the data type of your matrix if it is floating point there may be the problem use integers if you can so you do not need to convert so much ... – Spektre Apr 12 '17 at 08:47
  • 1. its the time r takes to read the GIF-file and create a raster object. "as matrix" makes it 6 times slower. – Andre Elrico Apr 12 '17 at 08:49

1 Answers1

1

you need to measure... without any sample image is hard to say and we can only guess. You need to take into account that loading/decoding JPG take time in milliseconds and encoding of GIF can be time consuming even 200 ms. Depends on kind of encoding. To speed up GIF encoding you can:

  1. use single global palette + dithering

    GIF is 8 bpp and JPG is 24 bpp so your encoder needs to do the transformation. That is called color quantization and is the most expensive operation while encoding which can take even ~200 ms per frame on average PC machine in well optimized C++ code. for more info see:

    To remedy this you can use single palette dedicated to dithering (like default VGA or use some WEB palette they have the same purpose) and use dithering with is much much faster. See:

    btw if you need to preserve colors take a look at this:

    So try to find out how to configure your encoder to force dithering instead of color quantization based on K-means or similar ....

  2. limit encoding dictionary to less then 4096

    The encoding/decoding is based on creating dictionary and encoding need to search it more than once on per pixel basis. So lovering its size to 1024 gets significant boost to speed. Of coarse you need to access to encoding code to change this unless this can be configured somehow in it... The compression will be decreased by this however and more clear codes will be present in the stream.

  3. use multi-threading

    you can fully parallelize this and encode with each core present in your system.

I strongly recommend you to measure how long it take to encode single frame of GIF. If you take advantage on both bullets #1,#2 then I estimate you can get near times around ~5 ms per frame with dithering and ~60 ms per frame with fast quantization. So with 3520 frames it would take around 17.6 or 211.2 seconds just to encode GIF so add the file memory and JPG manipulation and take into account all is heavily guessed/estimated as you did not provide sample data. And divide by number of cores if you use #3 +/- shared disc access waits.

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • thank you for your answer. I think i will indeed, try to find a way to go parallel. My gifs are all b&w indexed with values 0-black and 1-white. i believe there is also an transparent layer. I believe the GIF format in it self is a bottle neck. Can you recommend a "faster" format for b&w images? so i can use it in the next project? – Andre Elrico Apr 11 '17 at 08:07
  • @AndreElrico with BW the color quantization should not be a problem but you should check it with color picker or something so you do not have 128 colors near white and 128 near black. Also you can set the number of colors to minimum I think 1 bpp is supported. GIF should be fast enough for this the only question is if your implementation is I do not use 3th party libs for GIFs as I got my own ... But you have to measure the times to be sure in this case may be the JPG is bad choice as it is not suited for BW images ... If you share few sample images I can measure so you have comparison times. – Spektre Apr 11 '17 at 08:19
  • There is no JPG. I made a mistake in my post. They are all gifs with the above properties. I added a foto of my image properties, is this in a "fast" format? – Andre Elrico Apr 11 '17 at 08:39
  • @AndreElrico yes the format and config looks ok share at least one frame so I can measure how fast it will load. You may have another problem as you wrote you create matrix first and then sum it ... may be it is too large for your memory and swapping get involved ... Also by summing you mean creating single image or add a frame to result animation? – Spektre Apr 11 '17 at 14:39
  • look in my question on "edit2". thank you for taking your time! – Andre Elrico Apr 12 '17 at 08:21
  • @AndreElrico OK I copied the image here on SO. the decoding (single thread pure SW on CPU side C++ 32 bit app) time of that **GIF** is `~7.5ms` on my setup (AMD A8-5500 APU 3.2GHz Win7 x64) file access included. the operation you are performing should take a fraction of that lets say 2.5ms per frame just to be safe so `10ms per frame -> 32.500 sec` If you got a lot bigger time you got a bottleneck somewhere so test how long you get to decode GIF and how long to add frame to your result. If your times varying too much from mine (scaled by your CPU power) then you should clearly see what part – Spektre Apr 12 '17 at 08:37