我正在尝试创建部分灰度图像

时间:2013-10-28 14:19:29

标签: ios core-graphics

我正在尝试创建一个部分灰度图像,其中我正在读取该图像中的每个像素并将像素数据替换为灰色,如果像素颜色与所需颜色匹配,则限制它应用于此特定的像素颜色不会改变。我不知道我哪里出错了它将整个图像改为灰度并将图像旋转90度。有人可以提前帮助我解决这个问题。

-(UIImage *) toPartialGrayscale{
const int RED = 1;
const int GREEN = 2;
const int BLUE = 3;

initialR=255.0;
initialG=0.0;
initialB=0.0;//218-112-214//0-191-255
float r;
float g;
float b;
tollerance=50;

// Create image rectangle with current image width/height
CGRect imageRect = CGRectMake(0, 0, originalImageView.image.size.width * scale, originalImageView.image.size.height * scale);

int width = imageRect.size.width;
int height = imageRect.size.height;

// the pixels will be painted to this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace,
                                             kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [originalImageView.image CGImage]);

for(int y = 0; y < height; y++)
{
    for(int x = 0; x < width; x++)
    {
        uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];

        // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
        uint8_t gray = (uint8_t) ((30 * rgbaPixel[RED] + 59 * rgbaPixel[GREEN] + 11 * rgbaPixel[BLUE]) / 100);

        // set the pixels to grayi

        r= initialR-rgbaPixel[RED];
        g= initialG-rgbaPixel[GREEN];
        b= initialB-rgbaPixel[BLUE];

        if ((r<tollerance&&r>-tollerance)&&(g<tollerance&&g>-tollerance)&&(b<tollerance&&b>-tollerance))
        {
            rgbaPixel[RED] = (uint8_t)r;
            rgbaPixel[GREEN] = (uint8_t)g;
            rgbaPixel[BLUE] = (uint8_t)b;
        }
        else
        {
            rgbaPixel[RED] = gray;
            rgbaPixel[GREEN] = gray;
            rgbaPixel[BLUE] = gray;
        }


    }
}

// create a new CGImageRef from our context with the modified pixels
CGImageRef image = CGBitmapContextCreateImage(context);

// we're done with the context, color space, and pixels
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);

// make a new UIImage to return
UIImage *resultUIImage = [UIImage imageWithCGImage:image
                                             scale:scale
                                       orientation:UIImageOrientationUp];

// we're done with image now too
CGImageRelease(image);

return resultUIImage;
}

这是我正在使用的代码任何类型的帮助将在此之前再次感谢。

1 个答案:

答案 0 :(得分:0)

希望通过播放创建最终图像时传入的UIImageOrientationUp常量,可以很容易地解决方向片。向左或向右试试,直到得到你需要的东西。

至于阈值不起作用,你可以验证你的“容忍”是否真的像你期望的那样。将其更改为255并查看整个图像是否保留其颜色(应该)。如果它仍然是灰色的,那么你知道你的条件语句就是问题所在。