如何优化这种图像处理,用最接近的可用RGB替换图像上的所有像素



Im'试图用最接近的可用RGB替换输入图像的所有像素。我有一个包含颜色和输入图像的数组。这是我的代码,它给了我一个预期的输出图像,但处理一个图像需要很长时间(大约一分钟)。有人能帮我改进代码吗?或者,如果你有其他建议,请帮忙。

 UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGImageGetWidth(sourceImage),CGImageGetHeight(sourceImage)), NO, 0.0f);
 //Context size I keep as same as original input image size
 //Otherwise, the output will be only a partial image
 CGContextRef context; 
 context = UIGraphicsGetCurrentContext();
 //This is for flipping up sidedown
 CGContextTranslateCTM(context, 0, self.imageViewArea.image.size.height);
 CGContextScaleCTM(context, 1.0, -1.0);

        // init vars
        float d   = 0;                           // squared error
        int idx = 0;                             // index of palette color
        int min = 1000000;                       // min difference
        UIColor *oneRGB;                         // color at a pixel
        UIColor *paletteRGB;                     // palette color

        // visit each output color and determine closest color from palette
        for(int y=0; y<sizeY; y++) {
            for(int x=0; x<sizeX; x++) {
                // desired (avg) color is one pixel of scaled image
                oneRGB = [inputImgAvg colorAtPixel:CGPointMake(x,y)];

                // find closest color match in palette: init idx with index
                // of closest match; keep track of min to find idx
                min = 1000000;
                idx = 0;
                CGContextDrawImage(context,CGRectMake(xx, yy, 1, 1),img);
            }
        }
        UIImage *output = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.imageViewArea.image = output;

这是一个类似的问题(没有明确的答案),但答案有直接访问图像像素的代码。

量化图像,保存剩余颜色列表

您应该这样做,而不是对每个获取和设置像素使用CG函数。将一个图像的1个像素绘制到另一个图像上比改变阵列中的3个字节慢得多。

此外,ColorDiff中的内容——只要最接近的像素具有最小的差异,您就不需要完美的差异。可能有预处理此列表的空间,以便对于每个调色板条目,您与最接近的其他调色板条目具有最小的差分。然后,在循环浏览像素时,我可以快速检查下一个像素是否在距离刚刚找到的颜色的一半以内(因为照片之间往往有共同的颜色)。

如果这不匹配,那么在遍历调色板时,如果我距离任何条目都在这个距离的一半以内,就不需要进一步检查了。

基本上,这会在每个调色板条目周围设置一个区域,您可以确定这个区域是最接近的。

通常的答案是使用k-d树或其他八叉树结构来减少在每个像素上必须进行的计算和比较的数量。

我还成功地将颜色空间划分为一个规则的网格,并为网格的每个部分保留一个可能最接近的匹配列表。例如,您可以将R、G、B的(0-255)值除以16,最终得到一个由(16,16,16)或4096个元素组成的网格。最好的情况是,对于特定的网格元素,列表中只有一个成员,根本不需要遍历列表。

最新更新