Build OpenCV with extra modules for Python

OpenCV & Python

This image comes from Goolge Image.

Recently, I try to utilize the SIFT module in OpenCV. I think that this module should be included by OpenCV because I remembered that Yuanqing had ever used SIFT with OpenCV in his Travi-Navi paper. After I read some materials online, it turns out that some algorithms are not included since version 3.0 just because they are patented. If you want to use those patented algorithms, you can build OpenCV in Microsoft Visual Studio. That is why I write this article to note what I have learned in building OpenCV with extra modules for Python. On the other hand, I cannot get the correct results following the so-called videos and tutorials online. So I write this article to record the correct whole procedures step by step.

Required materials

  1. Microsoft Visual Studio (mine is version 2015, Microsoft Visual Studio Express 2015 for Windows Desktop)
  2. Python (mine is version 2.7.11)
  3. OpenCV: source code
  4. OpenCV_contrib: extra modules source code
  5. CMake with GUI
  6. NumPy and Matplotlib

Please guarantee that all the above dependencies are available. Follow the steps below:

CMake part

  1. Unzip OpenCV and OpenCV_contrib to a known directory and create a new folder.
  2. Open CMake and specify the two empty boxes shown in Figure 1.
  • Where is the source code
  • Where to build the binaries


    Figure 1: CMake settings

    Set the "Where to build the binaries" as the directory of the new folder.

  1. Click the Configure button in CMake GUI and choose the correct Visual Studio version shown in Figure 2. It asks to specify the generator for this project. I choose Visual Studio 14 2015 Win64. There are three options for each version of Visual Studio.
  • Visual Studio 14 2015: 32-bits application in VS
  • Visual Studio 14 2015 Win64: 64-bits application in VS
  • Visual Studio 14 2015 ARM: mobile version of OpenCV
    I choose Win64 because my Python is 64-bits. Then choose on use default native compilers. Click finish.
    FIgure 2: CMake Configure
  1. Some items are supposed to be checked and some are not. The details of mine can be found on the official website. Please note that some fields have changed. I just leave them alone. Remember to add the directory of OPENCV_EXTRA_MUDULES_PATH, which should be .\directory\to\OpenCV_contrib\opencv_contrib-master\modules.

  2. After Configure the source codes without any errors, click Generate button. When you see the Configuring done and Generating done, you can use Visual Studio open OpenCV.sln in "Where to build the binaries" directory.

Microsoft Visual Studio part

  1. Set the build mode as Release.
  2. Since I generated Win64 project, I use x64. You can find in Figure 3.
    Figure 3: Release mode and x64 in Visual Studio
  3. In Solution Explorer(Figure 4) of Visual Studio, find ALL_BUILD in CMake Targets item. Right click it and choose build.
    Figure 4: Solution Explorer of Visual Studio
  4. Do the same procedures of last step 3 for INSTALL file just below ALL_BUILD.

Copy and Paste

  1. Find the cv2.pyd file in where\to\build\binaries\lib\Release and copy it into Python\directory\Lib\site-packages.
  2. In where\to\build\binaries\bin\Release, find all the ".dll" files in Figure 5 and copy them to Python root directory (the same directory of python.exe).
    Figure 5: dll files

Testing

  1. Open Python Shell and input the following codes:
    import cv2
    If failed, you can see the error shown below.
    Traceback (most recent call last):
    File "<pyshell#0>", line 1, in <module>
    import cv2
    ImportError: DLL load failed: %1 is not a valid Win32 application.

  2. If no errors, input the version checking codes:
    cv2.version
    Then you can see the results shown below, which means that you have successfully install OpenCV for you Python.
    In [1]: import cv2

     In [2]: cv2.__version__
     Out[2]: '3.2.0-dev'
    

You can also check the Chinese version of this article here.
您可以點(diǎn)擊這里查看本文的中文版本粥喜。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末凸主,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子额湘,更是在濱河造成了極大的恐慌卿吐,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锋华,死亡現(xiàn)場(chǎng)離奇詭異嗡官,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)毯焕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門衍腥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人纳猫,你說我怎么就攤上這事婆咸。” “怎么了芜辕?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵尚骄,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我侵续,道長(zhǎng)倔丈,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任状蜗,我火速辦了婚禮乃沙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘诗舰。我一直安慰自己警儒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蜀铲,像睡著了一般边琉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上记劝,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天变姨,我揣著相機(jī)與錄音,去河邊找鬼厌丑。 笑死定欧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的怒竿。 我是一名探鬼主播砍鸠,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼耕驰!你這毒婦竟也來了爷辱?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤朦肘,失蹤者是張志新(化名)和其女友劉穎饭弓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體媒抠,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡弟断,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了趴生。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阀趴。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖冲秽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情矩父,我是刑警寧澤锉桑,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布,位于F島的核電站窍株,受9級(jí)特大地震影響民轴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜球订,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一后裸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧冒滩,春花似錦微驶、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽苟耻。三九已至,卻和暖如春扶檐,著一層夾襖步出監(jiān)牢的瞬間凶杖,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工款筑, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留智蝠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓奈梳,卻偏偏與公主長(zhǎng)得像杈湾,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子颈嚼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容