• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

usingOpencvObjective-Ccode

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

In this post I'll use GLImageProcessing sample demo from Apple. Also you will need precompiled OpenCV for iPhone. How to make it read here.

I've copied all OpenCV stuff to "opencv" folder into the GLImageProcessing. Here is project directory structure:

Now, let's add OpenCV to our project.

First step - specify the place, where the OpenCV headers are located. Add the path to OpenCV headers to the “Header Search Paths” property:

Second step - set up path where the OpenCV libraries are. Please note: you will need to repeat this step for each configuration (debug/release, device/simulator) because of directory structure of the OpenCV libs. Add path of the OpenCV libraries to the “Library Search Paths” property (for release device this will be “opencv/lib/iphoneos/Release” if you follow directory structure listed above).

Third step: Tell linker to include OpenCV to the output executable:

As you can see i prefer use linker flags instead using XCode UI (Targets -> GLImageProcessing -> Link Binary With Libraries -> Add -> Existing Files...). There are several reasons: i prefer this way because i don't trust XCode  (Actually because for all my projects i use cross-platform build system which allows to do such things much easies, but this is a topic for another article).

Now, add #include <opencv2/opencv.hpp> to header files . I’ve added this include to Imaging.h header right after #include <OpenGLES/ES1/glext.h>. Crossing fingers and hitting "Build and Run".....Oooops, compilation errors. Let's see, what we can do with all those errors. First of all - change extensions of all .m files which include OpenCV headers to .mm. This tells compiler that such file contains mixed ObjectiveC and C++ code. Also change extensions of all .c files to .cpp, because OpenCV uses STL, which is absent for pure C and thus you'll get compilation errors.

Now, only one compilation error left:

opencv/include/opencv2/core/core.hpp:432: error: statement-expressions are allowed only inside functions
opencv/include/opencv2/core/core.hpp:432: confused by earlier errors, bailing out

These errors are caused by MIN symbol, which is a macro in OpenCV and a function, defined somewhere in UIKit or other frameworks..Simple solution - include OpenCV before iOS frameworks.

So...with all these fixes all builds fine. Now it's time to add some OpenCV code.. Let's change the matrix multiplication code!

File Imaging.cpp

// Matrix Utilities for Hue rotation
static void matrixmult(float a[4][4], float b[4][4], float c[4][4])
{
int x, y;
float temp[4][4];
for(y=0; y&lt;4; y++)
	for(x=0; x&lt;4; x++)
		temp[y][x] = b[y][0] * a[0][x] +
			     b[y][1] * a[1][x] +
                             b[y][2] * a[2][x] +
                             b[y][3] * a[3][x];
for(y=0; y&lt;4; y++)
	for(x=0; x&lt;4; x++)
		c[y][x] = temp[y][x];
}

A little crappy code, isn't it? Let's rewrite it!

// Matrix Utilities for Hue rotation
static void matrixmult(float a[4][4], float b[4][4], float c[4][4])
{
	cv::Mat(4,4,CV_32FC1, c) = cv::Mat(4,4,CV_32FC1, a) * cv::Mat(4,4,CV_32FC1,b);
}

A bit of comments. cv::Mat is a type of OpenCV matrix. We initialize a matrix view, which takes data from the float[4][4] array, but not copy it. Result of multiplication of two views (a and b) assigned to view c which is a result.

Hit "Build and Run", and here we go:

This is a very simple example which demonstrates that you can use OpenCV even with ObjectiveC without any problems.

Good luck in your projects!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C中基于RunTime实现的反射发布时间:2022-07-12
下一篇:
Objective-C中的单例模式(工具类)发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap