1

我有一个 UIImageView,我想分析图像。分析器方法最能检测 5 个最常用的像素。我有两个类在其中实现像素:

第一课:

RGBA像素.h

@interface RGBAPixel : NSObject

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

@end

第二类:Pixel.h

@interface Pixel : NSObject

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

@end

RGBAPixels 包含像素红色、绿色和蓝色分量。

我使用 key 属性来计算图像中像素出现的次数。

分析方法:

-(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);

}

}

我的代码真的不起作用!提前致谢!

4

0 回答 0