Magick ++中快速/有效的像素访问

时间:2011-03-16 15:31:44

标签: c++ performance imagemagick magick++

作为我自己的教育练习,我正在编写一个可以平均一堆图像的应用程序。这通常用于Astrophotography以减少噪音。

我正在使用的库是Magick ++,我已经成功地编写了应用程序。但是,不幸的是,它的速度很慢。这是我正在使用的代码:

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);
    }
}

代码通过遍历每个像素并将每个通道的像素强度添加到向量中来平均10个图像。函数 avg 然后将矢量作为参数并对结果取平均值。然后在 stackedImage 中的相应像素处使用该平均值 - 这是结果图像。它运作得很好,但正如我所提到的,我对速度感到不满意。 Core i5机器需要2分30秒。图像是800万像素和16位TIFF。我知道它有很多数据,但我已经看到它在其他应用程序中的运行速度更快。

我的循环是慢还是 pixelColor(x,y)是一种访问图像像素的慢速方法?有更快的方法吗?

3 个答案:

答案 0 :(得分:4)

为什么要使用矢量/数组?

为什么不

double red=0.0, blue=0.0, green=0.0;
for(i=1;i<10;i++)
{
   ColorRGB rgb(image[i].pixelColor(column,row));
   red+=rgb.red();
   blue+=rgb.blue();
   green+=rgb.green();
}
red/=10;
blue/=10;
green/=10;

这可以避免每个像素对矢量对象进行36次函数调用。

通过使用整个图像的PixelCache代替原始Image对象,您可以获得更好的性能。请参阅online Magick++ documentation for Image

的“低级图像像素访问”部分

然后内循环变为

PixelPacket* pix = cache[i]+row*columns+column;
red+= pix->red;
blue+= pix->blue;
green+= pix->green;

现在,您还删除了10个PixelColor调用,10个ColorRGB构造函数和每个像素30个访问函数。

注意,这是所有理论;我还没有测试过它

答案 1 :(得分:1)

评论:

  • 为什么要使用redbluegreen的向量?因为使用push_back可以执行重新分配和瓶颈处理。你可以改为只分配三个10种颜色的阵列。
  • 你不能在循环之外声明rgb以减轻不必要的构造和破坏吗?
  • Magick ++不是一种平均图像的方法吗?

答案 2 :(得分:0)

以防其他人想要平均图像以减少噪音,并且感觉不太多“教育练习”; - )

ImageMagick可以像这样对一系列图像进行平均处理:

convert image1.tif image2.tif ... image32.tif -evaluate-sequence mean result.tif

您还可以通过将上述命令中的单词mean更改为您想要的任何内容来进行中值过滤和其他过滤,例如:

convert image1.tif image2.tif ... image32.tif -evaluate-sequence median result.tif

您可以通过以下方式获取可用操作的列表:

identify -list evaluate

<强>输出

Abs
Add
AddModulus
And
Cos
Cosine
Divide
Exp
Exponential
GaussianNoise
ImpulseNoise
LaplacianNoise
LeftShift
Log
Max
Mean
Median
Min
MultiplicativeNoise
Multiply
Or
PoissonNoise
Pow
RightShift
RMS
RootMeanSquare
Set
Sin
Sine
Subtract
Sum
Threshold
ThresholdBlack
ThresholdWhite
UniformNoise
Xor
相关问题