如何优化此图像处理用最接近的可用RGB替换图像上的所有像素? [英] how to optimized this image processing replace all pixels on image with closest available RGB?

查看:142
本文介绍了如何优化此图像处理用最接近的可用RGB替换图像上的所有像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im' trying to replace all pixels of input image with closest available RGB. I have a array contain color and input image. Here is my code, it give me an output image as expected, BUT it take very LONG time( about a min) to process one image. Can anybody help me improve the code? Or if you have any other suggestions, please help.

 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;


推荐答案

通常的答案是使用 kd tree 或其他一些八叉树结构,以减少每个像素必须完成的计算和比较次数。

The usual answer is to use a k-d tree or some other Octree structure to reduce the number of computations and comparisons that have to be done at each pixel.

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

I've also had success with partitioning the color space into a regular grid and keeping a list of possible closest matches for each part of the grid. For example you can divide the (0-255) values of R,G,B by 16 and end up with a grid of (16,16,16) or 4096 elements altogether. Best case is that there's only one member of the list for a particular grid element and no need to traverse the list at all.

这篇关于如何优化此图像处理用最接近的可用RGB替换图像上的所有像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆