OC call C++ function and openCV library 6 pit summary

Original text: http://www.jianshu.com/p/ae1896a50b0e

Project background

The company’s app products related to TCM diagnosis, TCM diagnosis is the use of Chinese medicine to diagnose the basic condition, we app companies need to integrate this feature into app, TCM diagnosis of this project is a project of Shanghai Medical University and Shanghai University in 985 and our work, as I docking technology iOS code farmers are responsible for the three party, I need to call them two companies provide C++ library, C++ source code which need to use the openCV development environment, call the C++ Xcode Library in general is not difficult, in the process also encountered some differences between C+ and openCv + and OC language problems need to be solved. The main is 6 the problem

1) when using the openCv library need to configure the Xcode configuration, see the open article about how to configure the openCV Cv configuration, Qiao brother also wrote the article, practical, openCV configuration is the last configuration, specific not presented here, but also need fine Jianai heart halfway here don’t introduce..

2) modify the library’s C++ library.Cpp file for the.Mm file, this integration of Baidu maps also so whole, so easy. will not say more, this modified, Xcode from the beginning of a red into, you can compile through

3) C++ image processing function of tcmPro image processing time is received received IplImage* type pictures from the camera, and we get the picture is UIImage type picture, you need to UIImage image into IPIImage* type picture

Int tcmPro (IplImage*, input, int, type)

The following is a function of the transformation of the picture type. There is the UIImage (UIImage_OpenCV) on the GitHub. This category contains the function of + (IplImage*) convertToIplImage: (UIImage*) image

(IplImage*) + convertToIplImage: (UIImage*) image
imageRef {CGImageRef = image.CGImage; CGColorSpaceRef = colorSpace; *iplImage = IplImage (CGColorSpaceCreateDeviceRGB) cvCreateImage (cvSize (image.size.width, image.size.height), IPL_DEPTH_8U, 4); CGContextRef contextRef = CGBitmapContextCreate (iplImage-> imageData, iplImage-> width, iplImage-> height, iplImage-> depth, iplImage-> widthStep colorSpace, kCGImageAlphaPremultipliedLast, kCGBitmapByteOrderDefault |; CGContextDrawImage (contextRef), CGRectMake (0, 0, image.size.width, image.size.height, imageRef)); CGContextRelease (contextRef); CGColorSpaceRelease (colorSpace); IplImage *ret = cvCreateImage (cvGetSize (iplImage), IPL_DEPTH_8U, 3); cvCvtColor (iplImage, RET, CV_RGB2 BGR); cvReleaseImage (& iplImage); return ret;}

4) for image processing, involves the algorithm, algorithm of template file is loaded in the.Xml file, when people engage in C++, using the method of C++ code loading xml file directly, we use other people’s library, use the C++ method to load the XML file in Xcode, certainly not loaded to XML file
.

C++ loaded XML used directly inside the openCV function cvLoad can be loaded, when debugging this method to Xcode cascade into the air

Char* cascade_name = “haarcascade_frontalface_alt2.xml”; char* cascade_name = “../assets/haarcascade_frontalface_alt2.xml”; (Xcode null) = cascade (CvHaarClassifierCascade*) cvLoad (cascade_name, 0, 0, 0);

Xcode uses the C++ method, cannot load XML, uses the OC method to load XML, loads the XML path to openCv, needs to carry on the path to NSASCIIStringEncoding, otherwise has not loaded successfully

NSString *path = [[NSBundle mainBundle] pathForResource:@ “haarcascade_frontalface_alt2” ofType:@ “XML”];
cascade = (CvHaarClassifierCascade*) cvLoad ([path cStringUsingEncoding:NSASCIIStringEncoding], 0, 0, 0);

5) the above several pits filled, XML loaded, loaded cascade value. But still and the expected effect is not consistent, What should I do? To a period of time, I downloaded the opencV is the latest C++, with openCv and our version is not consistent the? With this question Pidianpidian to call others, asking them to the openCV version, they are 2.4.9, I look at myself with the version number, version number found inconsistent, immediately download the 2.4.9 opencv2.framework tried again, still did not reach the expected effect, C++ the function call returns a value, but the return value is wrong.

6) the return value is wrong, check the int tcmPro (IplImage* input, int type) this function pointer array they return directly to a int type, then C++ developers a return multiple values of the pointer, but OC except swift are not at the same time to receive multiple return parameter values.

How to solve this problem, only the underlying function to modify the c++ function after modification is the core code that would have been needed to return to the returnResult[0], returnResult[1], returnResult[2], returnResult[3] of the four values, now the four values are combined into a four digit result2, OC and returnResult[0] returnResult[1] analysis. How much, returnResult[2], for the returnResult[3].

The following is the process of transforming returnResult[0], returnResult[1], returnResult[2], and returnResult[3] into result2

Result2 = returnResult[0]*1000 + returnResult[1]*100 +returnResult[2]*10 +returnResult[3];
printf (“shetou—-%d%d%d%d”, returnResult[0], returnResult[1], returnResult[2], returnResult[3]);

Now all the problems have been solved, this can not write a demo, the relevant source is the company’s confidential ah, sorry, you
!

If love is to don’t need RMB ha CHAN!! a reward.