1

I have a UIImageView and I want to analyze the image. The analyzer method most to detect the 5 most used pixels. I have two class where I implement the pixels:

The first class:

RGBAPixel.h

@interface RGBAPixel : NSObject

   @property unsigned int reds;
   @property unsigned int greens;
   @property unsigned int blues;

@end

The second class: Pixel.h

@interface Pixel : NSObject

   @property (nonatomic, strong) RGBAPixel *rgbpixel;
   @property unsigned int key;

@end

RGBAPixels contains the pixels red, green, and blue components.

I use the key property to count how many times occurs the pixel in the image.

The analyze method :

-(IBAction)analyzeImage:(id)sender {

    UIImage * inImage = imageView.image;
    CGImageRef rawImageRef = inImage.CGImage;

    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(rawImageRef));
    const UInt8 *rawPixelData = CFDataGetBytePtr(data);

    NSUInteger imageHeight = CGImageGetHeight(rawImageRef);
    NSUInteger imageWidth  = CGImageGetWidth(rawImageRef);
    NSUInteger bytesPerRow = CGImageGetBytesPerRow(rawImageRef);
    NSUInteger stride = CGImageGetBitsPerPixel(rawImageRef) / 8;

    NSMutableArray *pixelsarray2 = [[NSMutableArray alloc] init];
    NSMutableArray *pixelsarray3 = [[NSMutableArray alloc] init];
    NSMutableArray *pixelsarray4 = [[NSMutableArray alloc] init];

    for (int row = 0; row < imageHeight; row++) {
        const UInt8 *rowPtr = rawPixelData + bytesPerRow * row;
        for (int column = 0; column < imageWidth; column++) {
            RGBAPixel *pixel = [[RGBAPixel alloc] init];
            pixel.reds = rowPtr[0];
            pixel.greens = rowPtr[1];
            pixel.blues = rowPtr[2];
            rowPtr += stride;
            Pixel *pix = [[Pixel alloc] init];
            pix.rgbpixel = pixel;
            pix.key = 1;
            [pixelsarray2 addObject:pixel];

            if (![pixelsarray3 containsObject:pixel]) {
             [pixelsarray2 addObject:pix];
             [pixelsarray3 addObject:pixel];

             }

             else {
             NSInteger index = [pixels3 indexOfObject:pixel];
             Pixel *helperPixel = [pixelsarray2 objectAtIndex:index];
             helperPixel.key ++;
             [pixelsarray4 addObject:helperPixel];
             helperPixel = nil;

             }
        }
    }

    NSArray *unsortedPixels = pixelsarray4;
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:NO];
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *sortedPixels;
    sortedPixels = [unsortedPixels sortedArrayUsingDescriptors:sortDescriptors];

    Pixel *pix1 = [sortedPixels objectAtIndex:1];
    RGBAPixel *pixel1 = pix1.rgbpixel;
    NSLog(@"color1: %i %i %i:", pixel1.reds, pixel1.greens, pixel1.blues);
    UIColor *color1 = [[UIColor alloc]initWithRed:(pixel1.reds/255.0f) green:(pixel1.greens/255.0f) blue:(pixel1.blues/255.0f) alpha:1];

    Pixel *pix2 = [sortedPixels objectAtIndex:2];
    RGBAPixel *pixel2 = pix2.rgbpixel;
    NSLog(@"color2: %i %i %i:", pixel2.reds, pixel2.greens, pixel2.blues);
    UIColor *color2 = [[UIColor alloc]initWithRed:(pixel2.reds/255.0f) green:(pixel2.greens/255.0f) blue:(pixel2.blues/255.0f) alpha:1];

    Pixel *pix3 = [sortedPixels objectAtIndex:3];
    RGBAPixel *pixel3 = pix3.rgbpixel;
    NSLog(@"color3: %i %i %i:", pixel3.reds, pixel3.greens, pixel3.blues);
    UIColor *color3 = [[UIColor alloc]initWithRed:(pixel3.reds/255.0f) green:(pixel3.greens/255.0f) blue:(pixel3.blues/255.0f) alpha:1];

    Pixel *pix4 = [sortedPixels objectAtIndex:4];
    RGBAPixel *pixel4 = pix4.rgbpixel;
    NSLog(@"color4: %i %i %i:", pixel4.reds, pixel4.greens, pixel4.blues);
    UIColor *color4 = [[UIColor alloc]initWithRed:(pixel4.reds/255.0f) green:(pixel4.greens/255.0f) blue:(pixel4.blues/255.0f) alpha:1];

    Pixel *pix5 = [sortedPixels objectAtIndex:5];
    RGBAPixel *pixel5 = pix5.rgbpixel;
    NSLog(@"color5: %i %i %i:", pixel5.reds, pixel5.greens, pixel5.blues);
    UIColor *color5 = [[UIColor alloc]initWithRed:(pixel5.reds/255.0f) green:(pixel5.greens/255.0f) blue:(pixel5.blues/255.0f) alpha:1];

    colors = [[NSArray alloc] initWithObjects:color1,color2,color3,color4,color5, nil];

    myColors = colors;

    CFRelease(data);

}

}

My code doesn't really works! Thanks in advance!

4
  • 1
    creating an objc object for every pixel is going to take a llllllllllllloooooooooooooooooonnnnnnnnnnnnnggggggggggggg time on ios devices, unless you are always dealing with tiny bitmaps.
    – justin
    Sep 25, 2012 at 7:42
  • "My code doesn't really work". In what way? It would be helpful if you told us what it currently does and how you expect it to work.
    – Nick Bull
    Sep 25, 2012 at 8:43
  • Don't return the 5 most used pixel color, just the first 5 pixels color.
    – mate1229
    Sep 25, 2012 at 8:53
  • @MellauMate So can't you just sort through and find the 5 most used? Seems like that should be the simple part
    – Nick Bull
    Oct 2, 2012 at 7:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.