Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking UIImage Pixel by Pixel

i am creating an application in which i am trying to get the color of current touched point, if this point is not black then it will run an iteration/recursion and will store all the non black points in array. I am using this function for Iteration:

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    if (canDoPositiveX == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX+1 :positiveY :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = NO;
        }
    }

    if (canDoPositiveY == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 0"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY+1 :negativeX :negativeY];
        }
        else
        {
            canDoPositiveY = NO;
        }
    }

    if (canDoNegativeX == YES)
    {
        //Checking in negative
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX-1 :negativeY];
        }
        else
        {
            canDoNegativeX = NO;
        }
    }

    if (canDoNegativeY == YES)
    {
        //Checking in negative        
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            negativeY -= 1;
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX :negativeY-1];
        }
        else
        {
            canDoNegativeY = NO;
        }
    }

    NSLog(@"%u",[array count]);

}

Now, i am changing the color of those points which are in array, But it only colors straight line of points, not every pixel. Any help will be really appreciated. Thanks!

like image 201
EXC_BAD_ACCESS Avatar asked Mar 26 '13 09:03

EXC_BAD_ACCESS


3 Answers

Try this. I can't actually test it but it should work.

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    // Check your current point 
    if ([[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
    {
        // reached the dead end (break recursion)
        return;
    }

    CGPoint point = CGPointMake(positiveX, positiveY);
    [array addObject:[NSValue valueWithCGPoint:point]];

    BOOL canDoPositiveX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX+1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoPositiveY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY+1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeX = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX-1, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];
    BOOL canDoNegativeY = ![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY-1)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"];

    if (canDoPositiveX == YES)
    {
        [self function:positiveX+1 :positiveY :negativeX :negativeY];
    }

    if (canDoPositiveY == YES)
    {
        [self function:positiveX :positiveY+1 :negativeX :negativeY];
    }

    if (canDoNegativeX == YES)
    {
        [self function:positiveX :positiveY :negativeX-1 :negativeY];
    }

    if (canDoNegativeY == YES)
    {
        [self function:positiveX :positiveY :negativeX :negativeY-1];
    }

    NSLog(@"%u",[array count]);
}

Also remove these variables from your class canDoPositiveX, canDoPositiveY, canDoNegativeX, canDoNegativeY

like image 173
Sohaib Avatar answered Nov 13 '22 02:11

Sohaib


Check this question:

How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?

You should be able to easily use/modify the code on the accepted answer to return only the black (or nearly black) pixels coordinates for a given UIImage.

like image 21
atxe Avatar answered Nov 13 '22 01:11

atxe


Try with this modified code:

-(void)function:(CGFloat)positiveX:(CGFloat)positiveY:(CGFloat)negativeX:(CGFloat)negativeY
{
    if (canDoPositiveX == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX+1 :positiveY :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = NO;
            canDoPositiveY = YES;
            canDoNegativeX = YES;
            canDoNegativeY = YES;
        }
    }

    if (canDoPositiveY == YES)
    {
        //Checking in positive
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(positiveX, positiveY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 0"])
        {
            CGPoint point = CGPointMake(positiveX, positiveY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY+1 :negativeX :negativeY];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = NO;
            canDoNegativeX = YES;
            canDoNegativeY = YES;
        }
    }

    if (canDoNegativeX == YES)
    {
        //Checking in negative
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX-1 :negativeY];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = YES;
            canDoNegativeX = NO;
            canDoNegativeY = YES;
        }
    }

    if (canDoNegativeY == YES)
    {
        //Checking in negative        
        if (![[NSString stringWithFormat:@"%@",[self colorOfPoint:CGPointMake(negativeX, negativeY)]] isEqualToString:@"UIDeviceRGBColorSpace 0 0 0 1"])
        {
            CGPoint point = CGPointMake(negativeX, negativeY);
            [array addObject:[NSValue valueWithCGPoint:point]];
            [self function:positiveX :positiveY :negativeX :negativeY-1];
        }
        else
        {
            canDoPositiveX = YES;
            canDoPositiveY = YES;
            canDoNegativeX = YES;
            canDoNegativeY = NO;
        }
    }

    NSLog(@"%u",[array count]);
}
like image 20
Apurv Avatar answered Nov 13 '22 00:11

Apurv