在Xcode使用openCV

原文:http://objectivecbeginner.blogspot.com/2010/11/using-opencv-for-mac-os-in-xcode.html
转帖:http://blog.csdn.net/eminia/article/details/6635040
译文:http://blog.csdn.net/eminia/article/details/6635117
翻译:eminia

在Xcode使用openCV

第1段略.
安装openCV,有很多方法.有的是用Fink安装外部unix库,比如这篇.官网wiki也说要装外部库,但没说具体装哪一个.我试过,但费时费力.种种安装尝试后,碰巧发现一个私有框架.Dr. Patterson. Scroll所写,网页最底下的链接.先下载,双击打开虚拟盘.打开xcode,创建新项目.导入框架,选择下载的框架.最后结果如下:

正确安装好框架后,你一定汹涌澎湃想大展宏图跃跃欲试吧.但最让我郁闷的是,openCV帮助很烂.所用的范例要么跑不动,要么版本不对.羡慕嫉妒恨!下面这些是我找到的少量优秀教程:

Image Processing: OpenCV: 该网站讲了openCV的主要函数,并给出代码实例.但是大多数例子和他们给的API对不上 .
Introduction to OpenCV Programming: 该网站和上面类似,但还讲了对象的结构方面的知识.如 IplImage,并给使用openCV的详细范例.
Power Point Slide on Programming in OpenCV: 这个ppt中的代码示例有很好的注释.

装好openCV,肯定要做项目了.我呢,做的是生物计量方面的东东.下面我就来解释基本的openCV程序.先加张图片.把任意图片拖到xcode.我放了自己的照片一张me.jpg.

图片导入后,想看看图片可否调用,复制以下代码,粘贴到main.m.

#import "OpenCV/cv.h"
#import "OpenCV/highgui.h"

int main()
{
	//get the image from the directed path
	IplImage* img = cvLoadImage("/Users/wang/Desktop/me1.jpg", 1);
	//NSLog(img);
	//create a window to display the image
	cvNamedWindow("picture", 1);
	//show the image in the window
	cvShowImage("picture", img);
	//wait for the user to hit a key
	cvWaitKey(0);
	//delete the image and window
	cvReleaseImage(&img);
	cvDestroyWindow("picture");
	//return
	return 0;
}

测试前先确定图片路径,右键单击图片->getInfo,将path复制到代码.如果顺利,会显示图片,并打开picture窗口.主要导入2个文件:cv.h和highgui..h.其中.cv.h写的是openCV的所有主要函数,highgui是窗口和图形界面库.上面代码注释已经写的很清楚了,如果有问题可以留言.

因为我要做的是生物计量方面的东东,所以要用到mac的摄像头.下面代码是从摄像头截图.

#import "OpenCV/cv.h"
#import "OpenCV/highgui.h"

int main()
{
	//capture the image from device 0
	CvCapture* capture = cvCaptureFromCAM(0);
	//if the camera cannot take pictures, exit
	if(!cvGrabFrame(capture))
	{
		exit(0);
	}
	//get the image from the directed path
	IplImage* img = cvRetrieveFrame(capture,0);
	//create a window to display the image
	cvNamedWindow("picture", 1);
	//show the image in the window
	cvShowImage("picture", img);
	//wait for the user to hit a key
	cvWaitKey(0);
	//delete the image and window
	cvReleaseImage(&img);
	cvDestroyWindow("picture");
	//return
	return 0;	
}
如果顺利,会得到你自己的摄像头截图.

接下来的例子有点复杂:先从摄像头截图,再生成一个灰阶图.用模糊来平滑.再用canny算法(canny edge )检测边缘.对2维图像进行边界提取操作,最后用hough变换找到圆形对象.比如眼睛.我找的大多数范例,像hough转化,canny算法啊,总是出问题,要么参数不够,要么参数太多.下面的代码是经过我测试过,保证能用的:

#import "OpenCV/cv.h"
#import "OpenCV/highgui.h"

int main()
{
	//create camera object by using device 0
	CvCapture* capture = cvCaptureFromCAM(0);
	//create image object to be used
	IplImage* img = 0;
	// capture a frame from the camera to see if it is working
	if(!cvGrabFrame(capture))
	{exit(0);}
	//capture the first image from the camera
	img=cvRetrieveFrame(capture,0); 
	//create a gray image by copying the original image
	IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);
	//create storage device for hough circles
	CvMemStorage* storage = cvCreateMemStorage(0);
	//convert the gray image to grayscale
	cvCvtColor( img, gray, CV_BGR2GRAY );
	//smooth the gray image down. it takes in the gray image and outputs the gray image
	cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9, 0, 0 );
	//detect edges of the image by performing the canny operator
	cvCanny(gray, gray, 0, 20, 3);
	//run the hough transform and store the coordinates/radius of the circles in the circles object. it stores
	//it int a 3 column matrix with x, y, and radius as the columns
	CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4,100, 100, 0, 1000);
	
	//grab the total number of circles found
	int total = circles->total;
	//print out the total number of cirlces just to make sure that some where found
	NSLog(@"Total is : %i", total);
	//cycle through the circle matrix and print out circles detected onto the original image
	for(int i = 0; i <total; i++){
		//grab a row of the circle matrix
		float* p = (float*)cvGetSeqElem( circles, i );
		//it prints the circle using the p[0]/x, p[1]/y, and p[2], as the radius
		cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]),CV_RGB(255,255,255), 1, 8, 1 );
	}
	
	//create the windows to display the images
	cvNamedWindow( "circles", 1 ); cvNamedWindow("gray", 1);
	
	//show the images in the correct windows
	cvShowImage("gray", gray);
	cvShowImage( "circles", img );
	//wait for the user to press anything
	cvWaitKey(0);
	//release camera, images, and windows
	cvReleaseCapture(&capture); cvReleaseImage(&gray); cvReleaseImage(&img);cvDestroyWindow("circles"); cvDestroyWindow("gray");
	return 0;
}
结果图片如下(图片被裁过因为上传有限制):


这张图片已经用了hough变形,接着使用canny边缘检测算法.

从这个例子,你可以认为hough算法并不自动.下一周我会试着换一套算法,显示椭圆而不是正圆.我也开始在iPhone上用openCV,但发现有点小难.希望下周我就能弄懂这一点.再发布到博客,我现在做的就是开始.

注意,如果没有摄像头,就用前面的读图片的代码代替.

(完)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好!如果您想在Xcode使用OpenCV,以下是一些简单的步骤: 1. 首先,您需要安装OpenCV库。您可以从OpenCV官方网站(https://opencv.org/)下载适合您系统的版本,并按照安装指南进行安装。 2. 打开Xcode,并创建一个新的项目。 3. 在Xcode菜单中,选择 "File" -> "Add Files to <Your Project>". 然后选择您安装OpenCV时的文件夹,找到包含OpenCV库文件的文件夹(通常是 `/usr/local/include` 和 `/usr/local/lib`),并将它们添加到您的项目中。 4. 在Xcode中,选择您的项目,然后选择 "Build Settings"。在搜索框中输入 "Header Search Paths",然后双击该行并添加OpenCV库的头文件路径(通常是 `/usr/local/include`)。 5. 同样,在 "Build Settings" 中搜索 "Library Search Paths",然后双击该行并添加OpenCV库的库文件路径(通常是 `/usr/local/lib`)。 6. 在 "Build Phases" 选项卡中,展开 "Link Binary With Libraries" 并点击 "+" 按钮。然后从弹出的列表中选择 "libopencv_core.dylib"、"libopencv_highgui.dylib" 和 "libopencv_imgproc.dylib" 等OpenCV库文件。确保这些库文件已经添加到您的项目中。 7. 您可以在代码中引用OpenCV库,并开始使用它们了。例如,您可以添加 `#include <opencv2/opencv.hpp>` 来包含OpenCV库的头文件,并使用OpenCV功能进行图像处理和计算机视觉任务。 希望这些步骤对您有帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值