Ask Your Question
0

iOS and OpenCV - Object Detection seems to be skipping detectMultiScale Method

asked 2013-01-27 21:41:08 -0600

kaipyroami gravatar image

updated 2020-10-05 08:34:18 -0600

Has anyone had any trouble using object detection and iOS? This code works on eclipse with OSX and a webcam. I am able to get the "detection box" drawn on the image so the image is being passed properly to the processImage and detectAndDisplay methods. By adding break points I am able to see that the code appears to execute as expected. I am however not able to get an object that is detected on the webcam to be detected on the iPad.

I am thinking that since the image is passed by reference it might be getting written over before the detection is completed, but I am not sure.

In ViewDidLoad I load the haar files:

  TF_cascade_name = "haarcascade_TF.xml";
    FF_cascade_name = "haarcascade_FF.xml";

    TF_cascade.load( TF_cascade_name );
    FF_cascade.load( FF_cascade_name );

Then here is my image recognition code:

    - (void)processImage:(Mat&)image
{  
    offset_x = 400;
    offset_y = 150;

    cv::Rect myROI(offset_x, offset_y, 500, 200);

    cv::Mat croppedImage = image(myROI);
    [self detectAndDisplay:image detectionROI:croppedImage];     
}

/** detectAndDisplay */
- (void)detectAndDisplay:(Mat&) frame
             detectionROI:(Mat&) ROI_frame
{
    std::vector<cv::Rect> FFs;
    std::vector<cv::Rect> TFs;

    cv::Mat frame_gray;

    cvtColor( ROI_frame, frame_gray, CV_BGR2GRAY );//convert to grayscale
    equalizeHist( frame_gray, frame_gray );

    //-- Make Detection Box
    rectangle(frame, cvPoint(offset_x,offset_y), cvPoint(offset_x + 500,offset_y + 200), cvScalar(0,255,0),2, 1);


    //-- Detect syringe

    TF_cascade.detectMultiScale( frame_gray, TFs, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );

    for( int i = 0; i < TFs.size(); i++ )
    {
        cv::Point center( (TFs[i].x + TFs[i].width*0.5) + offset_x, (TFs[i].y + TFs[i].height*0.5) + offset_y );
        ellipse( frame, center, cv::Size( TFs[i].width*0.5, TFs[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 0 ), 4, 8, 0 );

        Mat faceROI = frame_gray( TFs[i] );
        std::vector<cv::Rect> eyes;        
    }


    FF_cascade.detectMultiScale( frame_gray, FFs, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30) );

    for( int i = 0; i < FFs.size(); i++ )
    {
        cv::Point center( (FFs[i].x + FFs[i].width*0.5) + offset_x, (FFs[i].y + FFs[i].height*0.5) + offset_y );
        cv::ellipse( frame, center, cv::Size( FFs[i].width*0.5, FFs[i].height*0.5), 0, 0, 360, Scalar( 0, 0, 255 ), 4, 8, 0 );

        Mat faceROI = frame_gray( FFs[i] );
        std::vector<cv::Rect> eyes;
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-10 12:39:09 -0600

kaipyroami gravatar image

This works. By using "pathForResource" of NSBundle.

NSString * const TFCascadeFilename = @"haarcascade_TF";//Strings of haar file names

NSString *TF_cascade_name = [[NSBundle mainBundle] pathForResource:TFCascadeFilename ofType:@"xml"];

 //Load haar file, return error message if fail
 if (!TF_cascade.load( [TF_cascade_name UTF8String] )) {
     NSLog(@"Could not load TF cascade!");
 }
edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-01-27 21:41:08 -0600

Seen: 1,527 times

Last updated: Mar 10 '13