2

In the following links it gives the result as shown below image

https://github.com/BloodAxe/opencv-ios-template-project/downloads

http://aptogo.co.uk/2011/09/opencv-framework-for-ios/

i changed the code to

COLOR_RGB2GRAY to COLOR_BGR2BGRA it give me a error says "OpenCV Error: Unsupported format or combination of formats () in cvCanny"
             (or)
CGColorSpaceCreateDeviceGray to CGColorSpaceCreateDeviceRGB

I am Totally confusing where to change the code... I need the output as "white color with black lines" instead of "black color with gray lines Please Guide me enter image description here Thanks a lot in advance

Babul
  • 1,268
  • 2
  • 15
  • 42
  • 1
    You don't need to change the colour space, it's greyscale by default and if you want white on black, you'll still be in greyscale! It looks like you need to invert the colours of the image that you're getting - unfortunately I'm not familiar with this library so I can't provide an answer on how to do that. – ttarik Oct 16 '12 at 10:38
  • Again Thanks ... I tried what u said i.e.,invert the colors.. i Got it Now.. I am trying this from last 2 days... Thank u so much @ev0lution – Babul Oct 16 '12 at 11:24
  • Ev0lution. Please state your response in an answer so this question can get marked as solved. – Josiah Oct 16 '12 at 14:51

1 Answers1

2

In OpenCVClientViewController.mm include this method (copied from https://stackoverflow.com/a/6672628/) then the image will be converted as shown below:

-(void)inverColors
{
    NSLog(@"inverColors called ");

    // get width and height as integers, since we'll be using them as
    // array subscripts, etc, and this'll save a whole lot of casting
    CGSize size = self.imageView.image.size;
    int width = size.width;
    int height = size.height;

    // Create a suitable RGB+alpha bitmap context in BGRA colour space
    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
    CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease(colourSpace);

    // draw the current image to the newly created context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [self.imageView.image CGImage]);

    // run through every pixel, a scan line at a time...
    for(int y = 0; y < height; y++)
    {
        // get a pointer to the start of this scan line
        unsigned char *linePointer = &memoryPool[y * width * 4];

        // step through the pixels one by one...
        for(int x = 0; x < width; x++)
        {
            // get RGB values. We're dealing with premultiplied alpha
            // here, so we need to divide by the alpha channel (if it
            // isn't zero, of course) to get uninflected RGB. We
            // multiply by 255 to keep precision while still using
            // integers
            int r, g, b; 
            if(linePointer[3])
            {
                r = linePointer[0] * 255 / linePointer[3];
                g = linePointer[1] * 255 / linePointer[3];
                b = linePointer[2] * 255 / linePointer[3];
            }
            else
                r = g = b = 0;

            // perform the colour inversion
            r = 255 - r;
            g = 255 - g;
            b = 255 - b;

            // multiply by alpha again, divide by 255 to undo the
            // scaling before, store the new values and advance
            // the pointer we're reading pixel data from
            linePointer[0] = r * linePointer[3] / 255;
            linePointer[1] = g * linePointer[3] / 255;
            linePointer[2] = b * linePointer[3] / 255;
            linePointer += 4;
        }
    }

    // get a CG image from the context, wrap that into a
    // UIImage
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    UIImage *returnImage = [UIImage imageWithCGImage:cgImage];

    // clean up
    CGImageRelease(cgImage);
    CGContextRelease(context);
    free(memoryPool);

    // and return
    self.imageView.image= returnImage;
}

// Called when the user changes either of the threshold sliders
- (IBAction)sliderChanged:(id)sender
{
    self.highLabel.text = [NSString stringWithFormat:@"%.0f", self.highSlider.value];
    self.lowLabel.text = [NSString stringWithFormat:@"%.0f", self.lowSlider.value];

    [self processFrame];
}

Please check the image

Community
  • 1
  • 1
ganesh manoj
  • 977
  • 10
  • 24
  • 4
    This code is copy-pasted from http://stackoverflow.com/a/6672628/ Please credit your sources when you use them verbatim. – jscs Oct 28 '12 at 17:06