最近需要做一個圖像識別的GUI應用,權衡了Opencv+ 1)QT,2)Python GUI,3).NET後選擇了.NET。。。
本文給出C#+Opencv和Python+Opencv的相應參考,節省大家時間。
(一)C#.NET + Opencv
1)下載並安裝Emgu庫(for opencv on .NET env)
Download @ http://sourceforge.net/projects/emgucv/
2)How to use opencv on C#?
VS上配置Emgu(庫)
3)測試on C#.NET
(二)Python + Opencv
按照官網的Instruction一步步來,可以從source自己編譯,也可以直接用二進制文件。
這裡注意我遇到的一個錯誤,undefined cv2.SIFT():
[plain] view plain copy
- AttributeError: 'module' object has no attribute 'SIFT'
查資料有的地方解釋為高版本把SIFT當做non-free的module需要自己編譯才有,具體參考這個帖子。
但我自己編譯成功後還是沒解決問題。。。於是試著吧opencv 2.3.1換成opencv 2.4.6.0,再按官網的Instruction預編譯二進制文件方法把cv2.pyd拷到python下,成功了。。。。
PS: 喜歡在mac上折騰的看看這個帖子吧。。。Installing OpenCV 2.4.2 on Mac OSX Mountain Lion with Python support
最後貼個python+opencv+numpy測試代碼:
[python] view plain copy
- import cv2
- import numpy as np
- #import pdb
- #pdb.set_trace()#turn on the pdb prompt
-
- #test cv2 and numpy package
- print cv2.__version__
- a = np.arange(10)
- print(a)
-
- #read image
- img = cv2.imread('D:\privacy\picture\little girl.jpg',cv2.IMREAD_COLOR)
- gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- cv2.imshow('tt',img);
-
- #basic accessing
- b,g,r = img[0,0];#blue,green,red
- print b,g,r
-
- rows,columns,channels = img.shape
- print rows,columns,channels
- print img.size
-
- #channels split and merge
- b,g,r = cv2.split(img)
- img = cv2.merge([b,g,r])
-
- #SIFT
- detector = cv2.SIFT()
- keypoints = detector.detect(gray,None)
- img = cv2.drawKeypoints(gray,keypoints)
- #img = cv2.drawKeypoints(gray,keypoints,flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
- cv2.imshow('test',img);
- cv2.waitKey(0)
- cv2.destroyAllWindows()
結果: