如何在iPhone中获得肤色像素?

时间:2010-04-27 10:22:14

标签: iphone

在我的应用程序中,我使用以下方式获取图像中的红色像素

//
//  PixelsAccessAppDelegate.h
//  PixelsAccess
//
//  Created by Fortune1 on 14/04/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import <UIKit/UIKit.h>
@class clsPixelAccess;
NSUInteger numberOfRedPixels;
NSUInteger numberOfRedPixels1;
NSUInteger numberOfRedPixels2;
NSUInteger numberOfRedPixels3;
NSUInteger numberOfRedPixels4;
NSUInteger numberOfRedPixels5;
NSUInteger numberOfRedPixels6;
NSUInteger numberOfRedPixels7;
NSUInteger numberOfRedPixels8;
NSUInteger numberOfRedPixels9;
NSUInteger numberOfRedPixels10;
NSUInteger numberOfRedPixels11;
NSUInteger numberOfRedPixels12;
NSUInteger numberOfRedPixels13;
NSUInteger numberOfRedPixels14;
NSUInteger numberOfRedPixels15;
NSUInteger numberOfRedPixels16;
NSUInteger numberOfRedPixels17;
NSUInteger numberOfRedPixels18;
NSUInteger numberOfRedPixels19;
NSUInteger numberOfRedPixels20;
NSUInteger numberOfRedPixels21;
NSUInteger numberOfRedPixels22;
NSUInteger numberOfRedPixels23;
NSUInteger numberOfRedPixels24;
NSUInteger numberOfRedPixels25;
NSUInteger numberOfRedPixels26;
NSUInteger numberOfRedPixels27;
NSUInteger numberOfRedPixels28;
NSUInteger numberOfRedPixels29;
NSUInteger numberOfRedPixels30;
NSUInteger numberOfRedPixels31;

@interface PixelsAccessAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    clsPixelAccess *obj;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
-(IBAction)processImage:(id)sender;

@end

////////////////////
//
//  PixelsAccessAppDelegate.m
//  PixelsAccess
//
//  Created by Fortune1 on 14/04/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "PixelsAccessAppDelegate.h"
#import "clsPixelAccess.h"

@implementation PixelsAccessAppDelegate

@synthesize window;

- (IBAction)processImage:(id)sender
{  
    NSUInteger retVal;
    obj = [[clsPixelAccess alloc] init];

    NSInteger imageSend =[obj processImage1:[UIImage imageNamed:@"s.jpg"]];
    NSInteger iamgeCall =[obj getPixelData:retVal];
    NSUInteger *numberOfRedPixels = retVal; 
    //lblPixelCount.text = [NSString stringWithFormat: @"There are %d red pixels in the image", numberOfRedPixels];

}

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

    // Override point for customization after application launch
    [window makeKeyAndVisible];
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end
///////////////

//
//  clsPixelsAccess.h
//  PixelsAccess
//
//  Created by Fortune1 on 14/04/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface clsPixelAccess : NSObject {

}
-(NSInteger) processImage1: (UIImage*) image;
-(NSInteger)getPixelData:(NSUInteger *)pixelCount;
@end
/////////


//
//  clsPixelsAccess.m
//  PixelsAccess
//
//  Created by Fortune1 on 14/04/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "clsPixelAccess.h"
#import "PixelsAccessAppDelegate.h"

@implementation clsPixelAccess

struct pixel
{
    //unsigned char r, g, b,a;
    Byte r, g, b, a;
    int count;
};


-(NSInteger)getPixelData:(NSUInteger *)pixelCount
{
    *pixelCount =numberOfRedPixels;
    return 1;

}
// Process the image and return the number of pure red pixels in it.

- (NSInteger) processImage1: (UIImage*) image
{

    // Allocate a buffer big enough to hold all the pixels

    struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap
        CGContextRef context = CGBitmapContextCreate(
                                                     (void*) pixels,
                                                     image.size.width,
                                                     image.size.height,
                                                     8,
                                                     image.size.width * 4,
                                                     CGImageGetColorSpace(image.CGImage),
                                                     kCGImageAlphaPremultipliedLast
                                                     );
        //NSLog(@"1=%d, 2=%d, 3=%d", CGImageGetBitsPerComponent(image), CGImageGetBitsPerPixel(image),CGImageGetBytesPerRow(image));
        if (context != NULL)
        {
            // Draw the image in the bitmap
            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);
            NSUInteger numberOfPixels = image.size.width * image.size.height;
            NSMutableArray *numberOfPixelsArray = [[[NSMutableArray alloc] initWithCapacity:numberOfPixelsArray] autorelease];
            NSLog( @"Pixel data %d", numberOfPixelsArray);

            /*  
             NSMatrix      *newMatrix = [[NSMatrix alloc] 
             initWithFrame:NSMakeRect(138.0f, 85.0f, 0.0f, 0.0f)
             mode:NSRadioModeMatrix
             prototype:prototypeButtonCell 
             numberOfRows: 

             numberOfColumns:];
             */ 
            while (numberOfPixels > 0)
            {
                if (pixels->r > 0 && pixels->r <= 7)
                {
                    numberOfRedPixels++;
                }
                NSLog( @"Red pixel data %d",numberOfRedPixels);

                if (pixels->r >= 8 && pixels->r <= 15)
                {
                    numberOfRedPixels1++;
                }

                NSLog( @"Red pixel data1 %d",numberOfRedPixels1);

                if (pixels->r >= 16 && pixels->r <=23 )
                {
                    numberOfRedPixels2++;
                }
                NSLog( @"Red pixel data2 %d",numberOfRedPixels2);

                if (pixels->r >= 24 && pixels->r <=31 )
                {
                    numberOfRedPixels3++;
                }
                NSLog( @"Red pixel data3 %d",numberOfRedPixels3);

                if (pixels->r >= 32 && pixels->r <= 39)
                {
                    numberOfRedPixels4++;
                }
                NSLog( @"Red pixel data4 %d",numberOfRedPixels4);

                if (pixels->r >= 40 && pixels->r <= 47)
                {
                    numberOfRedPixels5++;
                }
                NSLog( @"Red pixel data5 %d",numberOfRedPixels5);
                if (pixels->r >= 48 && pixels->r <= 55)
                {
                    numberOfRedPixels6++;
                }
                NSLog( @"Red pixel data6 %d",numberOfRedPixels6);

                if(pixels->r >= 56 && pixels->r <= 63)
                {
                    numberOfRedPixels7++;
                }
                NSLog( @"Red pixel data7 %d",numberOfRedPixels7);

                if (pixels->r >= 64 && pixels->r <= 71)
                {
                    numberOfRedPixels8++;
                }
                NSLog( @"Red pixel data8 %d",numberOfRedPixels8);

                if (pixels->r >=  72 && pixels->r <= 79)
                {
                    numberOfRedPixels9++;
                }
                NSLog( @"Red pixel data9 %d",numberOfRedPixels9);

                if (pixels->r >= 80 && pixels->r <= 87)
                {
                    numberOfRedPixels10++;
                }
                NSLog( @"Red pixel data10 %d",numberOfRedPixels10);

                if (pixels->r >= 88 && pixels->r <= 95)
                {
                    numberOfRedPixels11++;
                }
                NSLog( @"Red pixel data11 %d",numberOfRedPixels11);

                if (pixels->r >= 96 && pixels->r <= 103)
                {
                    numberOfRedPixels12++;
                }
                NSLog( @"Red pixel data12 %d",numberOfRedPixels12);

                if (pixels->r >= 104  && pixels->r <= 111)
                {
                    numberOfRedPixels13++;
                }
                NSLog( @"Red pixel data13 %d",numberOfRedPixels13);

                if (pixels->r >= 112 && pixels->r <= 119)
                {
                    numberOfRedPixels14++;
                }
                NSLog( @"Red pixel data14 %d",numberOfRedPixels14);

                if (pixels->r >= 120 && pixels->r <= 127)
                {
                    numberOfRedPixels15++;
                }
                NSLog( @"Red pixel data15 %d",numberOfRedPixels15);
                if (pixels->r > 128 && pixels->r <= 135)
                {
                    numberOfRedPixels16++;
                }
                NSLog( @"Red pixel data16 %d",numberOfRedPixels16);

                if (pixels->r >= 136 && pixels->r <= 143)
                {
                    numberOfRedPixels17++;
                }

                NSLog( @"Red pixel data17 %d",numberOfRedPixels17);

                if (pixels->r >= 144 && pixels->r <=151)
                {
                    numberOfRedPixels18++;
                }
                NSLog( @"Red pixel data18 %d",numberOfRedPixels18);

                if (pixels->r >= 152 && pixels->r <=159 )
                {
                    numberOfRedPixels19++;
                }
                NSLog( @"Red pixel data19 %d",numberOfRedPixels19);

                if (pixels->r >= 160 && pixels->r <= 167)
                {
                    numberOfRedPixels20++;
                }
                NSLog( @"Red pixel data20 %d",numberOfRedPixels20);

                if (pixels->r >= 168 && pixels->r <= 175)
                {
                    numberOfRedPixels21++;
                }
                NSLog( @"Red pixel data21 %d",numberOfRedPixels21);
                if (pixels->r >= 176 && pixels->r <= 199)
                {
                    numberOfRedPixels22++;
                }
                NSLog( @"Red pixel data22 %d",numberOfRedPixels22);

                if(pixels->r >= 184 && pixels->r <= 191)
                {
                    numberOfRedPixels23++;
                }
                NSLog( @"Red pixel data23 %d",numberOfRedPixels23);

                if (pixels->r >= 192 && pixels->r <= 199)
                {
                    numberOfRedPixels24++;
                }
                NSLog( @"Red pixel data24 %d",numberOfRedPixels24);

                if (pixels->r >= 200 && pixels->r <= 207)
                {
                    numberOfRedPixels25++;
                }
                NSLog( @"Red pixel data25 %d",numberOfRedPixels25);

                if (pixels->r >= 208 && pixels->r <= 215)
                {
                    numberOfRedPixels26++;
                }
                NSLog( @"Red pixel data26 %d",numberOfRedPixels26);

                if (pixels->r >= 216 && pixels->r <= 223)
                {
                    numberOfRedPixels27++;
                }
                NSLog( @"Red pixel data27 %d",numberOfRedPixels27);

                if (pixels->r >= 224 && pixels->r <= 231)
                {
                    numberOfRedPixels28++;
                }
                NSLog( @"Red pixel data28 %d",numberOfRedPixels28);

                if (pixels->r >= 232  && pixels->r <= 239)
                {
                    numberOfRedPixels29++;
                }
                NSLog( @"Red pixel data29 %d",numberOfRedPixels29);

                if (pixels->r >= 240 && pixels->r <= 247)
                {
                    numberOfRedPixels30++;
                }
                NSLog( @"Red pixel data30 %d",numberOfRedPixels30);

                if (pixels->r >= 248)
                {
                    numberOfRedPixels31++;
                }
                NSLog( @"Red pixel data31 %d",numberOfRedPixels31);
                pixels++;

                numberOfPixels--;
            }

            CGContextRelease(context);
        }

        free(pixels);
    }

    return 1;
}           

@end

我的问题是我想要皮肤Tone Pixel如何可能

请帮帮我。

先谢谢

2 个答案:

答案 0 :(得分:1)

所有这些代码正在做的是确定在特定范围内具有红色分量的像素数。如果您只想查找肤色像素,请删除所有比较但只更改

if (pixels->r >= 192 && pixels->r <= 199)

if ( (pixels->r >= ? && pixels->r <= ?) && (pixels->g >= ? && pixels->g <= ?) && (pixels->b >= ? && pixels->b <= ?) )

其中问号是三个颜色组件中每个颜色组件的实际值范围的占位符,这些颜色组件将定义应用程序中的肤色。

答案 1 :(得分:0)

你现在正在做的是根据红色成分的值对像素进行分级,如果你想到颜色空间,你基本上将它分成沿红轴的矩形片段。

要识别肤色,您需要做类似的事情,在颜色空间中定义一个您认为是肤色的区域,并进行类似的包含测试。虽然因为我不希望它会很好而且长方形,但它会有点诡计。

这也完全忽略了确定你所谈论的肤色的问题,这是一个完全不同的问题。