简体   繁体   English

OpenCV Python 3.3

[英]OpenCV Python 3.3

I have Python 3.3 and 2.7 installed on my computer 我的计算机上安装了Python 3.3和2.7

For Python 3.3, I installed many libraries like numpy, scipy, etc 对于Python 3.3,我安装了许多库,例如numpy,scipy等

Since I also want to use opencv, which only supports python 2.7 so far, I installed opencv under Python 2.7. 由于我还想使用仅支持python 2.7的opencv,因此我在Python 2.7下安装了opencv。

Hey, here comes the problem, what if I want to import numpy as well as cv in the same script? 嘿,问题来了,如果我想在同一脚本中导入numpy和cv怎么办?

You'll have to install all the libraries you want to use together with OpenCV for Python 2.7. 您必须安装要与OpenCV for 2.7一起使用的所有库。 This is not much of a problem, you can do it with pip in one line, or choose one of the many pre-built scientific Python packages. 这不是什么大问题,您可以在一行中使用pip完成此操作,也可以选择许多预先构建的科学Python软件包之一。

"Hey, here comes the problem, what if I want to import numpy as well as cv in the same script?" “嘿,问题来了,如果我想在同一脚本中同时导入numpy和cv怎么办?”

As far as python 2.7 goes, numpy and cv can be imported in the same script. 就python 2.7而言,可以在同一脚本中导入numpy和cv。 In fact opencv accepts numpy arrays. 实际上,opencv接受numpy数组。 Please see the code below 请看下面的代码

import os
import numpy as np
import cv2


def processImage(dirName, imgName):
    imgFilepath = os.path.join(dirName, imgName)
    img = cv2.imread(imgFilepath)
    print imgName, img.size
    #convert img to a numpy array
    numpyImg = np.asarray(img)
    #use as inout to cv2 call
    #this converts a color imageto a grayscale image
    grayscaleImg = cv2.cvtColor(numpyImg, cv2.COLOR_BGR2GRAY)
    (fname, ext) = os.path.splitext(imgName)
    outImgName = fname + '_gray' + ext
    #write grayscalimage
    cv2.imwrite(os.path.join(dirName, outImgName), grayscaleImg)
    pass

def main():
    #aphid1_small.jpg is a rgb image
    imgName = "aphid1_small.jpg"
    dirName = "data"
    processImage(dirName, imgName)

if __name__ == "__main__":
    main()
    pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM