手机版
你好,游客 登录 注册
背景:
阅读新闻

在Xcode使用OpenCV

[日期:2016-09-08] 来源:Linux社区  作者: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,但发现有点小难.希望下周我就能弄懂这一点.再发布到博客,我现在做的就是开始.

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

Mac平台上OpenCV开发环境搭建 http://www.linuxidc.com/Linux/2016-09/135028.htm

Mac OS X安装OpenCV并配置到Xcode和Eclipse上  http://www.linuxidc.com/Linux/2016-09/135029.htm

--------------------------------------分割线 --------------------------------------

OpenCV官方教程中文版(For Python) PDF  http://www.linuxidc.com/Linux/2015-08/121400.htm

Ubuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm

[翻译]Ubuntu 14.04, 13.10 下安装 OpenCV 2.4.9  http://www.linuxidc.com/Linux/2014-12/110045.htm

OpenCV的详细介绍请点这里
OpenCV的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-09/135031.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款