OpenCV errors for iOS / detecting Hough Circles

2019-04-13 13:23发布

问题:

I have been trying for hours to run an xcode project with openCV. I have built the source, imported it into the project and included #ifdef __cplusplus #import opencv2/opencv.hpp> #endif in the .pch file.

I followed the instructions from http://docs.opencv.org/trunk/doc/tutorials/introduction/ios_install/ios_install.html

Still I am getting many Apple Mach-O linker errors when I compile.

Undefined symbols for architecture i386:
 "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from:

Please help me I am really lost..

UPDATE:

Errors all fixed and now I am trying to detect circles..

Mat src, src_gray;

cvtColor( image, src_gray, CV_BGR2GRAY );

vector<Vec3f> circles;

/// Apply the Hough Transform to find the circles
HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, image.rows/8, 200, 100, 0, 0 );


/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ )
{
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    // circle center
    circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
    // circle outline
    circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );


}

I am using the code above, however no circles are being drawn on the image.. is there something obvious that I am doing wrong?

回答1:

Try the solution in my answer to this question...

How to resolve iOS Link errors with OpenCV

Also on github I have a couple of simple working samples - with recently built openCV framework.

NB - OpenCVSquares is simpler than OpenCVSquaresSL. The latter was adapted for Snow Leopard backwards compatibility - it contains two builds of the openCV framework and 3 targets, so you are better off using the simpler OpenCVSquares if it will run on your system.

To adapt OpenCVSquares to detect circles, I suggest that you start with the Hough Circles c++ sample from the openCV distro, and use it to adapt/replace CVSquares.cpp and CVSquares.h with, say CVCircles.cpp and CVCicles.h

The principles are exactly the same:

  • remove UI code from the c++, the UI is provided on the obj-C side
  • transform the main() function into a static member function for the class declared in the header file. This should mirror in form an Objective-C message to the wrapper (which translates the obj-c method to a c++ function call).

From the objective-C side, you are passing a UIImage to the wrapper object, which:

  • converts the UIImage to a cv::Mat image
  • pass the Mat to a c++ class for processing
  • converts the result from Mat back to UIImage
  • returns the processed UIImage back to the objective-C calling object

update

The adapted houghcircles.cpp should look something like this at it's most basic (I've replaced the CVSquares class with a CVCircles class):

cv::Mat CVCircles::detectedCirclesInImage (cv::Mat img)
{   
    //expects a grayscale image on input
    //returns a colour image on ouput
    Mat cimg;
    medianBlur(img, img, 5);
    cvtColor(img, cimg, CV_GRAY2RGB);
    vector<Vec3f> circles;
    HoughCircles(img, circles, CV_HOUGH_GRADIENT, 1, 10,
                 100, 30, 1, 60 // change the last two parameters
                 // (min_radius & max_radius) to detect larger circles
                 );
    for( size_t i = 0; i < circles.size(); i++ )
        {
        Vec3i c = circles[i];
        circle( cimg, Point(c[0], c[1]), c[2], Scalar(255,0,0), 3, CV_AA);
        circle( cimg, Point(c[0], c[1]), 2, Scalar(0,255,0), 3, CV_AA);
        }
    return cimg;
    }

Note that the input parameters are reduced to one - the input image - for simplicity. Shortly I will post a sample on github which will include some parameters tied to slider controls in the iOS UI, but you should get this version working first.

As the function signature has changed you should follow it up the chain...

Alter the houghcircles.h class definition:

    static cv::Mat detectedCirclesInImage (const cv::Mat image);

Modify the CVWrapper class to accept a similarly-structured method which calls detectedCirclesInImage

    + (UIImage*) detectedCirclesInImage:(UIImage*) image
    {
        UIImage* result = nil;
        cv::Mat matImage = [image CVGrayscaleMat];
        matImage = CVCircles::detectedCirclesInImage (matImage);
        result = [UIImage imageWithCVMat:matImage];
        return result;
    }

Note that we are converting the input UIImage to grayscale, as the houghcircles function expects a grayscale image on input. Take care to pull the latest version of my github project, I found an error in the CVGrayscaleMat category which is now fixed . Output image is colour (colour applied to grayscale input image to pick out found circles).

If you want your input and output images in colour, you just need to ensure that you make a grayscale conversion of your input image for sending to Houghcircles() - eg cvtColor(input_image, gray_image, CV_RGB2GRAY); and apply your found circles to the colour input image (which becomes your return image).

Finally in your CVViewController, change your messages to CVWrapper to conform to this new signature:

    UIImage* image = [CVWrapper detectedCirclesInImage:self.image];

If you follow all of these details your project will produce circle-detected results.

update 2
OpenCVCircles now on Github
With sliders to adjust HoughCircles() parameters



标签: ios opencv cmake