1

I want to detect colors from Image, I am getting images frame by frame with the help of AVFoundation's classes for live camera. Now I want to detect red color from image and to make operation after getting red color. I read this Post but couldn't find any healthy solution. Is this possible for not using external library? Any tutorial or helping material would be helpful for me. Thanks in advance.

Community
  • 1
  • 1
josh
  • 1,681
  • 4
  • 28
  • 61

1 Answers1

0

Try this code. Call this method when touch move and touch begin occurs in screen:

-(void) getPixelColorAtLocation:(CGPoint)point {

unsigned char pixel[4] = {0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(context, -point.x, -point.y);
[self.layer renderInContext:context];
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
        if((pixel[0]=255) && (pixel[1] = 0) && (pixel[2] = 0)){
     //if pixel are red then this condition become true

}

Use this method for getting image:

- (UIImage *)drawLineFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint image:(UIImage *)image
{

}

You can call it like this:

self.image = [self drawLineFromPoint:previousPoint  toPoint:point image:self.image];
neowinston
  • 7,584
  • 10
  • 52
  • 83
kb920
  • 3,039
  • 2
  • 33
  • 44
  • Thanks Keyur, at this code i am passing CGPoint but where and how I can set UIImage to this method in which it detects colors ratio? Thanks again. – josh Mar 15 '13 at 11:11
  • I have edit my code pls try if you will face any prb let me know – kb920 Mar 15 '13 at 11:17
  • Thanks, Buddy I already have UIImage but I want to ask that how can I pass this image to "-(void) getPixelColorAtLocation:(CGPoint)point" method as you are just passing CGPoint but where is UIImage? how you are getting pixels from Image? – josh Mar 15 '13 at 11:22
  • I think here no need to pass uiimage you just pass you touch point – kb920 Mar 15 '13 at 11:25
  • I don't think so I need UITouch, I already have UIImage, how to read pixels from already Image? – josh Mar 15 '13 at 11:30
  • -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; [myPath addLineToPoint:[mytouch locationInView:self]]; [self setNeedsDisplay]; [img setNeedsDisplay]; */ UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self]; [self getPixelColorAtLocation:currentPoint]; } – kb920 Mar 15 '13 at 11:33
  • I know the touch moved method but there is no role of touches in my app. I just have an output Image now I want to play with this image, is to fetch colors from it. Are you getting my problem? – josh Mar 15 '13 at 12:21