Vijay
Vijay

Reputation: 1016

OpenCV:image processing,objective C/C++

My goal is to find golf ball using iPhone camera, So I did same steps in photoshop and I want to achieve same steps in openCV on image/live video frame start with the original picture.

--Input Image:

enter image description here

--Output Image:

enter image description here

Would you please help me or give me some hints related image processing with OpenCV in iOS?

Thanks in advance!

Edit I used following code and got the below output Image,

  - (UIImage*) applyToneCurveToImage:(UIImage*)image
    {
     CIImage* ciImage = [[CIImage alloc] initWithImage:image];
     CIFilter*   filter =
     [CIFilter filterWithName:@"CIToneCurve"
               keysAndValues:
     kCIInputImageKey, ciImage,
     @"inputPoint0",[CIVector vectorWithX:0.00 Y:0.3]
     ,@"inputPoint1",[CIVector vectorWithX:0.25 Y:0.4]
     ,@"inputPoint2",[CIVector vectorWithX:0.50 Y:0.5]
     ,@"inputPoint3",[CIVector vectorWithX:0.75 Y:0.6]
     ,@"inputPoint4",[CIVector vectorWithX:1.00 Y:0.7]
     ,nil];

    //CIFilter* filter2 = [filter copy];

    //step1
    filter = [CIFilter filterWithName:@"CIColorControls"
                        keysAndValues:kCIInputImageKey,
              [filter valueForKey:kCIOutputImageKey], nil];


    [filter setValue:[NSNumber numberWithFloat:0]
              forKey:@"inputBrightness"];
    [filter setValue:[NSNumber numberWithFloat:6]
              forKey:@"inputContrast"];

    CIImage* result = [filter valueForKey:kCIOutputImageKey];
    CIContext *context = [CIContext contextWithOptions:nil];

    CGImageRef cgImage = [context createCGImage:result
                                       fromRect:[result extent]];

    UIImage* filteredImage = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);

    ciImage=nil;
    context=nil;
    cgImage=nil;
    result=nil;

    return filteredImage;
   }

    - (void)didCaptureIplImage:(IplImage *)iplImage
    {
    @autoreleasepool
    {
        IplImage *orgimage = cvCreateImage(cvGetSize(iplImage), IPL_DEPTH_8U, 3);

        orgimage=[self CreateIplImageFromUIImage:[self applyToneCurveToImage:[UIImage imageNamed:@"GolfImage.jpeg"] ] ];

        Mat matRGB = Mat(orgimage);

        //ipl imaeg is also converted to HSV; hue is used to find certain color
        IplImage *imgHSV = cvCreateImage(cvGetSize(orgimage), 8, 3);       //2

        cvCvtColor(orgimage, imgHSV, CV_BGR2HSV);

        IplImage *imgThreshed = cvCreateImage(cvGetSize(orgimage), 8, 1);     //3

    //    cvInRangeS(imgHSV, cvScalar(_Hmin, _Smin, _Vmin), cvScalar(_Hmax , _Smax, _Vmax), imgThreshed);

        cvInRangeS(imgHSV, cvScalar(0.00, 0.00, 34.82), cvScalar(180.00 , 202.54, 256.00), imgThreshed);

        Originalimage=nil;

         cvReleaseImage(&iplImage);
         cvReleaseImage(&orgimage);
         cvReleaseImage(&imgHSV);

         [self didFinishProcessingImage:imgThreshed];
    }

Output Image:

enter image description here

Upvotes: 1

Views: 1712

Answers (1)

foundry
foundry

Reputation: 31745

You don't need openCV for any of this. You should be able to get this result using Core Image

See this question How to change minimum or maximum value by CIFilter in Core image?

Where I give a fairly detailed answer on the manipulation of tone curves.

This will cover your steps 2 and 4. For step 1 (saturation) try CIColorControls. For step 3 (convert to grayscale) you could also use CIColorControls, but that would involove dropping saturation to 0, not what you want. Instead you can use CIMaximumComponentor CIMinimumComponent. For step 5, you could use the result of 1-4 as a mask with a flat colour.

OpenCV will allow you to pick out the ball from the rest of the image (I guess this is what you want to achieve, you didn't mention it in your question). You can refer to this question: How does the HoughCircle function works? I can't get it to work properly which I answered with an accompanying demo project: https://github.com/foundry/OpenCVCircles. You can pass the result of your Core Image processing in to openCV by converting to the openCV Mat format from UIImage (that linked project shows you how to do this).

Upvotes: 2

Related Questions