As a training exercise for myself, I am writing an application that can average a bunch of images. This is often used in astrophotography to reduce noise.
The library I use is Magick ++, and I managed to write an application. But unfortunately, it is slow. This is the code I'm using:
for(row=0;row<rows;row++) { for(column=0;column<columns;column++) { red.clear(); blue.clear(); green.clear(); for(i=1;i<10;i++) { ColorRGB rgb(image[i].pixelColor(column,row)); red.push_back(rgb.red()); green.push_back(rgb.green()); blue.push_back(rgb.blue()); } redVal = avg(red); greenVal = avg(green); blueVal = avg(blue); redVal = redVal*MaxRGB; greenVal = greenVal*MaxRGB; blueVal = blueVal*MaxRGB; Color newRGB(redVal,greenVal,blueVal); stackedImage.pixelColor(column,row,newRGB); } }
The code averages 10 images by looking at each pixel and adding the intensity of each channel pixel to the double vector. The avg function then transfers the vector as a parameter and averages the result. This average value is then used in the corresponding pixel in stackedImage - this is the resulting image. It works fine, but, as I said, speed does not suit me. On a Core i5, it takes 2 minutes and 30 seconds. Images are 8-megapixel and 16-bit TIFF. I understand that he has a lot of data, but I saw how it was done faster in other applications.
Is my loop slow or pixelColor (x, y) a slow way to access image pixels? Is there a faster way?
source share