分享

第五章: pcDuino上面安装和测试SimpleCV以及OpenCV

 xiaofenglib 2013-08-29
pcDuino有计算能力来进行计算机视觉,这就让很多在Arduino 上不可能的项目可以在pcDuino上实现。 我们可以把计算机视觉这一非常强大的技术用在我们的项目中。 在这章,我们来看怎么安装和使用SimpleCV和OpenCV。


SimpleCV的安装和例子

在我们开始动手之前,我们要确保手头有一个 UVC 兼容的USB摄像头。
下面为安装SimpleCV的步骤:
  1. $sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
  2. $sudo pip install https://github.com/ingenuitas/SimpleCV/zipball/master
  3. $sudo apt-get install python-pygame
  4. $sudo apt-get install python-imaging
复制代码
在终端里面输入 “$simpleCV" 就可以启动simpleCV. simpleCV是一个交互的命令解释器。 我们可以输入命令来执行:
  1. ubuntu@ubuntu:~$ simplecv

  2. +-----------------------------------------------------------+
  3. SimpleCV 1.3.0 [interactive shell] - http://
  4. +-----------------------------------------------------------+

  5. Commands:
  6. "exit()" or press "Ctrl+ D" to exit the shell
  7. "clear" to clear the shell screen
  8. "tutorial" to begin the SimpleCV interactive tutorial
  9. "example" gives a list of examples you can run
  10. "forums" will launch a web browser for the help forums
  11. "walkthrough" will launch a web browser with a walkthrough

  12. Usage:
  13. dot complete works to show library
  14. for example: Image().save("/tmp/test.jpg") will dot complete
  15. just by touching TAB after typing Image().

  16. Documentation:
  17. help(Image), ?Image, Image?, or Image()? all do the same
  18. "docs" will launch webbrowser showing documentation

  19. SimpleCV:1> cam=Camera()
  20. VIDIOC_QUERYMENU: Invalid argument
  21. VIDIOC_QUERYMENU: Invalid argument
  22. VIDIOC_QUERYMENU: Invalid argument
  23. VIDIOC_QUERYMENU: Invalid argument
  24. VIDIOC_QUERYMENU: Invalid argument
  25. VIDIOC_QUERYMENU: Invalid argument
  26. VIDIOC_QUERYMENU: Invalid argument

  27. SimpleCV:2> img=cam.getImage()

  28. SimpleCV:3> img.show()
  29. SimpleCV:5:
复制代码
以下为命令执行的结果:




OpenCV的安装和例子

OpenCV 是个开源的计算机视觉软件包。在这里我们详细的介绍如何安装OpenCV Python版本到pcDuino上面。 并且给出了两个例子,一个例子用来通过USB 摄像头来获取图形,另外一个例子通过OpenCV来做人脸识别。

安装步骤:
  1. $ sudo apt-get -y install build-essential cmake cmake-qt-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools

  2. $sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev libdc1394-22-dev libdc1394-22 libdc1394-utils swig libv4l-0 libv4l-dev python-numpy libpython2.6 python2.6-dev libgtk2.0-dev pkg-config

  3. $sudo apt-get install libopencv-dev python-opencv

  4. $sudo apt-get install python-dev

  5. $sudo ln -s /usr/lib/arm-linux-gnueabihf/libjpeg.so /usr/lib
  6. $sudo ln -s /usr/lib/arm-linux-gnueabihf/libfreetype.so /usr/lib
  7. $sudo ln -s /usr/lib/arm-linux-gnueabihf/libz.so /usr/lib

  8. $sudo easy_install PIL
  9. $sudo pip install -v PIL
复制代码
例子1: 用OpenCV通过USB摄像头来抓图像:
  1. #!/Users/brent/.virtualenvs/lumber/bin/python

  2. import cv

  3. cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
  4. camera_index = 0
  5. capture = cv.CaptureFromCAM(camera_index)

  6. gx = gy = 1
  7. grayscale = blur = canny = False

  8. def repeat():
  9.     global capture #declare as globals since we are assigning to them now
  10.     global camera_index
  11.     global gx, gy, grayscale, canny, blur
  12.     frame = cv.QueryFrame(capture)
  13.     # import pdb; pdb.set_trace()

  14.     if grayscale:
  15.         gray = cv.CreateImage(cv.GetSize(frame), frame.depth, 1)
  16.         cv.CvtColor(frame, gray, cv.CV_RGB2GRAY)
  17.         frame = gray

  18.     if blur:
  19.         g = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.channels)
  20.         cv.Smooth(frame, g, cv.CV_GAUSSIAN, gx, gy)
  21.         frame = g

  22.     if grayscale and canny:
  23.         c = cv.CreateImage(cv.GetSize(frame), frame.depth, frame.channels)
  24.         cv.Canny(frame, c, 10, 100, 3)
  25.         frame = c
  26.     cv.ShowImage("w1", frame)

  27.     c = cv.WaitKey(10)
  28.     if c==ord('='): #in "n" key is pressed while the popup window is in focus
  29.         gx += 2
  30.         gy += 2
  31.     elif c == ord('-'):
  32.         gx = max(1, gx-2)
  33.         gy = max(1, gy-2)
  34.     elif c == ord('x'):
  35.         gx += 2
  36.     elif c == ord('X'):
  37.         gx = max(1, gx-2)
  38.     elif c == ord('q'):
  39.         exit(0)

  40.     elif c == ord('b'):
  41.         blur = not blur
  42.     elif c == ord('g'):
  43.         grayscale = not grayscale
  44.     elif c == ord('c'):
  45.         canny = not canny

  46. while True:
  47. repeat()
复制代码
例子二: 人脸识别

输入图像:


下载上面的图像,保存为 ”opencv_in.jpg"。

输出图像:

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. import os
  4. from PIL import Image, ImageDraw
  5. import cv

  6. def detect_object(image):
  7.     grayscale = cv.CreateImage((image.width, image.height), 8, 1)
  8.     cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)

  9.     cascade = cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml")
  10.     rect = cv.HaarDetectObjects(grayscale, cascade, cv.CreateMemStorage(), 1.1, 2,
  11.         cv.CV_HAAR_DO_CANNY_PRUNING, (20,20))

  12.     result = []
  13.     for r in rect:
  14.         result.append((r[0][0], r[0][1], r[0][0]+r[0][2], r[0][1]+r[0][3]))

  15.     return result

  16. def process(infile):
  17.     image = cv.LoadImage(infile);
  18.     if image:
  19.         faces = detect_object(image)

  20.     im = Image.open(infile)
  21.     path = os.path.abspath(infile)
  22.     save_path = os.path.splitext(path)[0]+"_face"
  23.     try:
  24.         os.mkdir(save_path)
  25.     except:
  26.         pass
  27.     if faces:
  28.         draw = ImageDraw.Draw(im)
  29.         count = 0
  30.         for f in faces:
  31.             count += 1
  32.             draw.rectangle(f, outline=(255, 0, 0))
  33.             a = im.crop(f)
  34.             file_name = os.path.join(save_path,str(count)+".jpg")
  35.      #       print file_name
  36.             a.save(file_name)

  37.         drow_save_path = os.path.join(save_path,"out.jpg")
  38.         im.save(drow_save_path, "JPEG", quality=80)
  39.     else:
  40.         print "Error: cannot detect faces on %s" % infile

  41. if __name__ == "__main__":
  42. process("./opencv_in.jpg")
复制代码
保存上面的代码到文件名为" test_face.py"的文件中。

运行“$ python test_face.py" 来运行。

chapter5_2.png (340.02 KB, 下载次数: 5)

chapter5_2.png

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多