簡體   English   中英

iPhone應用程序中圖像處理中的RGB

[英]RGB in image processing in iphone app

我正在mp應用程序中進行圖像處理。 我從圖像中獲得了像素顏色,然后通過觸摸將其應用到圖像上。我的代碼獲得了像素顏色,但是它將整個圖像更改為藍色,並將該藍色應用於圖像處理。 我被困在代碼中。 但是不知道我的代碼出了什么問題,請幫助我。

我的代碼是:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
UITouch *touch = [touches anyObject];
CGPoint coordinateTouch = [touch locationInView:[self view]];//where image was tapped

if (value == YES) {
    self.lastColor = [self getPixelColorAtLocation:coordinateTouch]; 
    value =NO;
}

NSLog(@"color %@",lastColor);
//[pickedColorDelegate pickedColor:(UIColor*)self.lastColor];



ListPoint point;
point.x = coordinateTouch.x;
point.y = coordinateTouch.y;

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor whiteColor];
button.frame = CGRectMake(coordinateTouch.x-5, coordinateTouch.y-5, 2, 2);
//[descImageView addSubview:button];

[bgImage addSubview:button];

// Make image blurred on ImageView
if(bgImage.image)
{

    CGImageRef imgRef = [[bgImage image] CGImage];
    CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(imgRef)); 
    const unsigned char *sourceBytesPtr = CFDataGetBytePtr(dataRef);
    int len = CFDataGetLength(dataRef);
    NSLog(@"length = %d, width = %d, height = %d, bytes per row = %d, bit per pixels = %d", 
          len, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef), CGImageGetBytesPerRow(imgRef), CGImageGetBitsPerPixel(imgRef));

    int width = CGImageGetWidth(imgRef);
    int height = CGImageGetHeight(imgRef);
    int widthstep = CGImageGetBytesPerRow(imgRef);
    unsigned char *pixelData = (unsigned char *)malloc(len);
    double wFrame = bgImage.frame.size.width;
    double hFrame = bgImage.frame.size.height;

    Image_Correction(sourceBytesPtr, pixelData, widthstep, width, height, wFrame, hFrame, point);

    NSLog(@"finish");


    NSData *data = [NSData dataWithBytes:pixelData length:len];

    NSLog(@"1");
    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);

    NSLog(@"2");
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();

    NSLog(@"3");
    CGImageRef imageRef = CGImageCreate(width, height, 8, CGImageGetBitsPerPixel(imgRef), CGImageGetBytesPerRow(imgRef),
                                        colorSpace2,kCGImageAlphaNoneSkipFirst|kCGBitmapByteOrder32Host,
                                        provider, NULL, false, kCGRenderingIntentDefault);

    NSLog(@"Start processing image");
    UIImage *ret = [UIImage imageWithCGImage:imageRef scale:1.0 orientation:UIImageOrientationUp];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace2);
    CFRelease(dataRef);
    free(pixelData);
    NSLog(@"4");
    bgImage.image = ret;
    [button removeFromSuperview];
}   
}




- (UIColor*) getPixelColorAtLocation:(CGPoint)point {


UIColor* color = nil;
CGImageRef inImage = self.image.CGImage;
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
if (cgctx == NULL) { return nil; /* error */ }

size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}}; 

// Draw the image to the bitmap context. Once we draw, the memory 
// allocated for the context for rendering will then contain the 
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage); 

// Now we can get a pointer to the image data associated with the bitmap
// context.
unsigned char* data = CGBitmapContextGetData (cgctx);
if (data != NULL) {
    //offset locates the pixel in the data from x,y. 
    //4 for 4 bytes of data per pixel, w is width of one row of data.
    int offset = 4*((w*round(point.y))+round(point.x));
     alpha =  data[offset]; 
     red = data[offset+1]; 
     green = data[offset+2]; 
     blue = data[offset+3]; 
    NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
    color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
}

// When finished, release the context
CGContextRelease(cgctx); 
// Free image data memory for the context
if (data) { free(data); }

return color;
}





- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

CGContextRef    context = NULL;
CGColorSpaceRef colorSpace;
void *          bitmapData;
int             bitmapByteCount;
int             bitmapBytesPerRow;

// Get image width, height. We'll use the entire image.
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);

// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow   = (pixelsWide * 4);
bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

// Use the generic RGB color space.
colorSpace = CGColorSpaceCreateDeviceRGB();

if (colorSpace == NULL)
{
    fprintf(stderr, "Error allocating color space\n");
    return NULL;
}

// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL) 
{
    fprintf (stderr, "Memory not allocated!");
    CGColorSpaceRelease( colorSpace );
    return NULL;
}

// Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
// per component. Regardless of what the source image format is 
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
context = CGBitmapContextCreate (bitmapData,
                                 pixelsWide,
                                 pixelsHigh,
                                 8,      // bits per component
                                 bitmapBytesPerRow,
                                 colorSpace,
                                 kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
    free (bitmapData);
    fprintf (stderr, "Context not created!");
}

// Make sure and release colorspace before returning
CGColorSpaceRelease( colorSpace );

return context;
}



int Image_Correction(const unsigned char *pImage, unsigned char *rImage, int widthstep, int nW, int nH, double wFrame, double hFrame, ListPoint point)              

{
double ratiox = nW/wFrame;
double ratioy = nH/hFrame;
double newW, newH, ratio;
if(ratioy > ratiox)
{
    newH = hFrame;
    newW = nW/ratioy;
    ratio = ratioy;
}
else 
{
    newH = nH/ratiox;
    newW = wFrame;
    ratio = ratiox;
}
NSLog(@"new H, W = %f, %f", newW, newH);
NSLog(@"ratiox = %f; ratioy = %f", ratiox, ratioy);

ListPoint real_point;
real_point.x = (point.x - wFrame/2 + newW/2) *ratio;
real_point.y = (point.y - hFrame/2 + newH/2)*ratio;

for(int h = 0; h < nH; h++)
{
    for(int k = 0; k < nW; k++)
    {
        rImage[h*widthstep + k*4 + 0] = pImage[h*widthstep + k*4 + 0];
        rImage[h*widthstep + k*4 + 1] = pImage[h*widthstep + k*4 + 1];
        rImage[h*widthstep + k*4 + 2] = pImage[h*widthstep + k*4 + 2];
        rImage[h*widthstep + k*4 + 3] = pImage[h*widthstep + k*4 + 3];
    }
}

// Modify this parameter to change Blurred area
int iBlurredArea = 6;
for(int h = -ratio*iBlurredArea; h <= ratio*iBlurredArea; h++)
    for(int k = -ratio*iBlurredArea; k <= ratio*iBlurredArea; k++)
    {
        int tempx = real_point.x + k;
        int tempy = real_point.y + h;
        if (((tempy - 3) > 0)&&((tempy+3) >0)&&((tempx - 3) > 0)&&((tempx + 3) >0)) 
        {
            double sumR = 0;
            double sumG = 0;
            double sumB = 0;
            double sumA = 0; 
            double count = 0;
            for(int m = -3; m < 4; m++)
                for (int n = -3; n < 4; n++) 
                {                       
                    sumR = red;//sumR + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 0];
                    sumG = green;//sumG + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 1];
                    sumB = blue;//sumB + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 2];
                    sumA = alpha;//sumA + pImage[(tempy + m)*widthstep + (tempx + n)*4 + 3];
                    count++;
                }



            rImage[tempy*widthstep + tempx*4 + 0] = red;//sumR/count;
            rImage[tempy*widthstep + tempx*4 + 1] = green;//sumG/count;
            rImage[tempy*widthstep + tempx*4 + 2] = blue;//sumB/count;
            rImage[tempy*widthstep + tempx*4 + 3] = alpha;//sumA/count;
        }
    }
return 1;
}

謝謝你看這個代碼..我想我做錯了什么。 提前謝謝。

這似乎為我工作。

UIImage* modifyImage(UIImage* image)
{
    size_t w = image.size.width;
    size_t h = image.size.height;
    CGFloat scale = image.scale;

    // Create the bitmap context
    UIGraphicsBeginImageContext(CGSizeMake(w*scale, h*scale));
    CGContextRef context = UIGraphicsGetCurrentContext();

    // NOTE you may have to setup a rotation here based on image.imageOrientation
    // but I didn't need to consider that for my images.
    CGContextScaleCTM(context, scale, scale);
    [image drawInRect:CGRectMake(0, 0, w, h)];

    unsigned char* data = CGBitmapContextGetData (context);
    if (data != NULL) {
        size_t height = CGBitmapContextGetHeight(context);
        size_t width = CGBitmapContextGetWidth(context);
        size_t bytesPerRow = CGBitmapContextGetBytesPerRow(context);

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // Not sure why the color info is in BGRA format
                // Look at CGBitmapContextGetBitmapInfo(context) if this format isn't working for you
                int offset = y * bytesPerRow + x * 4;
                unsigned char* blue =  &data[offset];
                unsigned char* green = &data[offset+1];
                unsigned char* red = &data[offset+2];
                unsigned char* alpha = &data[offset+3];

                int newRed = ...; // color calculation code here
                int newGreen = ...;
                int newBlue = ...;

                // Assuming you don't want to change the original alpha value.
                *red = (newRed * *alpha)/255;
                *green = (newGreen * *alpha)/255;
                *blue = (newBlue * *alpha)/255; 
            }               
        }
    }

    CGImageRef newImage = CGBitmapContextCreateImage(context);

    UIImage *done = [UIImage imageWithCGImage:newImage scale:image.scale orientation: image.imageOrientation];
    CGImageRelease(newImage);
    UIGraphicsEndImageContext();

    return done;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM