Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing RGB color image to Grayscale image using Objective C

I was developing a application that changes color image to gray image. However, some how the picture comes out wrong. I dont know what is wrong with the code. maybe the parameter that i put in is wrong please help.

 UIImage *c = [UIImage imageNamed:@"downRed.png"];
 CGImageRef cRef = CGImageRetain(c.CGImage);

 NSData* pixelData = (NSData*) CGDataProviderCopyData(CGImageGetDataProvider(cRef));

 size_t w = CGImageGetWidth(cRef);
 size_t h = CGImageGetHeight(cRef);

 unsigned char* pixelBytes = (unsigned char *)[pixelData bytes];

 unsigned char* greyPixelData = (unsigned char*) malloc(w*h);

 for (int y = 0; y < h; y++) {
 for(int x = 0; x < w; x++){

 int iter = 4*(w*y+x);
 int red = pixe lBytes[iter];
 int green = pixelBytes[iter+1];
 int blue = pixelBytes[iter+2];
 greyPixelData[w*y+x] = (unsigned char)(red*0.3 + green*0.59+ blue*0.11);
     int value = greyPixelData[w*y+x];

 }
 }

 CFDataRef imgData = CFDataCreate(NULL, greyPixelData, w*h);


 CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData(imgData);


 size_t width = CGImageGetWidth(cRef);
 size_t height = CGImageGetHeight(cRef);
 size_t bitsPerComponent = 8;
 size_t bitsPerPixel = 8;
 size_t bytesPerRow = CGImageGetWidth(cRef);
 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
 CGBitmapInfo info = kCGImageAlphaNone;
 CGFloat *decode = NULL;
 BOOL shouldInteroplate = NO;
 CGColorRenderingIntent intent = kCGRenderingIntentDefault;
  CGDataProviderRelease(imgDataProvider);

 CGImageRef throughCGImage = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpace, info, imgDataProvider, decode, shouldInteroplate, intent);


 UIImage* newImage = [UIImage imageWithCGImage:throughCGImage];

CGImageRelease(throughCGImage);
 newImageView.image = newImage;
like image 561
Younduk Nam Avatar asked Jan 07 '11 16:01

Younduk Nam


People also ask

How do I convert RGB image to grayscale?

I = rgb2gray( RGB ) converts the truecolor image RGB to the grayscale image I . The rgb2gray function converts RGB images to grayscale by eliminating the hue and saturation information while retaining the luminance. If you have Parallel Computing Toolbox™ installed, rgb2gray can perform this conversion on a GPU.

How do I convert a color image to grayscale?

Change a picture to grayscale or to black-and-whiteRight-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

Why we convert RGB image to grayscale in image processing?

The main reason why grayscale representations are often used for extracting descriptors instead of operating on color images directly is that grayscale simplifies the algorithm and reduces computational requirements.


1 Answers

If someone wants to gray scale images that contain transparent parts the solution offered by digipeople will have a black mask around it. Use this instead to leave the transparent as it is.

- (UIImage *)imageToGreyImage:(UIImage *)image {
    // Create image rectangle with current image width/height
    CGFloat actualWidth = image.size.width;
    CGFloat actualHeight = image.size.height;

    CGRect imageRect = CGRectMake(0, 0, actualWidth, actualHeight);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    CGContextRef context = CGBitmapContextCreate(nil, actualWidth, actualHeight, 8, 0, colorSpace, kCGImageAlphaNone);
    CGContextDrawImage(context, imageRect, [image CGImage]);

    CGImageRef grayImage = CGBitmapContextCreateImage(context);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);

    context = CGBitmapContextCreate(nil, actualWidth, actualHeight, 8, 0, nil, kCGImageAlphaOnly);
    CGContextDrawImage(context, imageRect, [image CGImage]);
    CGImageRef mask = CGBitmapContextCreateImage(context);
    CGContextRelease(context);

    UIImage *grayScaleImage = [UIImage imageWithCGImage:CGImageCreateWithMask(grayImage, mask) scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(grayImage);
    CGImageRelease(mask);

    // Return the new grayscale image
    return grayScaleImage;
}
like image 197
Groot Avatar answered Oct 17 '22 06:10

Groot