Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / 在Xcode使用OpenCV

在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 pathIplImage* img = cvLoadImage("/Users/wang/Desktop/me1.jpg", 1);//NSLog(img);//create a window to display the imagecvNamedWindow("picture", 1);//show the image in the windowcvShowImage("picture", img);//wait for the user to hit a keycvWaitKey(0);//delete the image and windowcvReleaseImage(&img);cvDestroyWindow("picture");//returnreturn 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 0CvCapture* capture = cvCaptureFromCAM(0);//if the camera cannot take pictures, exitif(!cvGrabFrame(capture)){exit(0);}//get the image from the directed pathIplImage* img = cvRetrieveFrame(capture,0);//create a window to display the imagecvNamedWindow("picture", 1);//show the image in the windowcvShowImage("picture", img);//wait for the user to hit a keycvWaitKey(0);//delete the image and windowcvReleaseImage(&img);cvDestroyWindow("picture");//returnreturn 0;}如果顺利,会得到你自己的摄像头截图.接下来的例子有点复杂:先从摄像头截图,再生成一个灰阶图.用模糊来平滑.再用canny算法(canny edge )检测边缘.对2维图像进行边界提取操作,最后用hough变换找到圆形对象.比如眼睛.我找的大多数范例,像hough转化,canny算法啊,总是出问题,要么参数不够,要么参数太多.下面的代码是经过我测试过,保证能用的:#import "OpenCV/cv.h"#import "OpenCV/highgui.h"int main(){//create camera object by using device 0CvCapture* capture = cvCaptureFromCAM(0);//create image object to be usedIplImage* img = 0;// capture a frame from the camera to see if it is workingif(!cvGrabFrame(capture)){exit(0);}//capture the first image from the cameraimg=cvRetrieveFrame(capture,0); //create a gray image by copying the original imageIplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);//create storage device for hough circlesCvMemStorage* storage = cvCreateMemStorage(0);//convert the gray image to grayscalecvCvtColor( img, gray, CV_BGR2GRAY );//smooth the gray image down. it takes in the gray image and outputs the gray imagecvSmooth( gray, gray, CV_GAUSSIAN, 9, 9, 0, 0 );//detect edges of the image by performing the canny operatorcvCanny(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 columnsCvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/4,100, 100, 0, 1000);//grab the total number of circles foundint total = circles->total;//print out the total number of cirlces just to make sure that some where foundNSLog(@"Total is : %i", total);//cycle through the circle matrix and print out circles detected onto the original imagefor(int i = 0; i <total; i++){//grab a row of the circle matrixfloat* p = (float*)cvGetSeqElem( circles, i );//it prints the circle using the p[0]/x, p[1]/y, and p[2], as the radiuscvCircle( 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 imagescvNamedWindow( "circles", 1 ); cvNamedWindow("gray", 1);//show the images in the correct windowscvShowImage("gray", gray);cvShowImage( "circles", img );//wait for the user to press anythingcvWaitKey(0);//release camera, images, and windowscvReleaseCapture(&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.htmMac 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.htmUbuntu Linux下安装OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htmUbuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htmCentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htmUbuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htmUbuntu 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.htmOpenCV的详细介绍:请点这里
OpenCV的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/135031.htm