OpenCV Python

Computer Vision is a science of acquiring, processing and analyzing digital image(s) to extract useful information.

First of all, Lets check what language , interface  and OS support available in OpenCV.

OpenCV (Open Source Computer Vision Library)

Implementation Language :  C/C++

Language Interfaces : C++ ,Python and Java

OS Support : Windows, Linux, Mac OS, iOS and Android

OpenCV-Python is a Python wrapper for the original OpenCV C++ implementation. So it gives flexibility of writing easy python modules while maintaining performance with highly optimized C++ library.

There are two releases for Python interface known as cv and cv2 . cv2 is the latest one.

The Python Dynamically module (.pyd) is of DLL format. So when you use import cv2  , This cv2,pyd will be loaded.

Secondly, We need to check whether we have correct system environment for Python version.

Python Version

Python can be part of many applications installed in your system. It may be due to installation of Python,  Cygwin, Microsoft Visual Studio 2017, Conda etc

One can use  where cmdname  to find all Python executables in the system

C:\WINDOWS\system32>where python
C:\ProgramData\Anaconda3\python.exe
C:\Users\shreyash\AppData\Local\Programs\Python\Python36\python.exe
C:\Users\shreyash\AppData\Local\Programs\Python\Python36\Scripts\python.exe
c:\cygwin64\bin\python
C:\Users\shreyash\Anaconda3\python.exe

Version information can be found by executing  python –version with full path

C:\ProgramData\Anaconda3\Lib\site-packages>C:\Users\shreyash\Anaconda3\python –version
Python 3.6.7 :: Anaconda, Inc.

shreyash@DESKTOP-ORB7A8Q /cygdrive/c/cygwin64/bin
$ python –version
Python 2.7.14

Which Python version will be executed based on system environment settings ? 

One can find this using  which python command

C:\Users\shreyash\Desktop\Learn\Language\Python\opencv>which python
/cygdrive/c/ProgramData/Anaconda3/python

Alternatively you can use  print(sys.executable) in Python, iPython or Jupyter Notebook

C:\>python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import sys
>>> print(sys.executable)
C:\ProgramData\Anaconda3\python.exe

Most importantly , One must do a quick sanity check of OpenCV working

OpenCV Sanity Check

I copied cv2.pyd  from  C:\Users\shreyash\Anaconda3\Lib\site-packages    to  C:\ProgramData\Anaconda3\Lib\site-packages

Here is the basic check for OpenCV  with Python 

C:\Users\shreyash\Desktop\Learn\Language\Python\opencv>python
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import cv2
>>> cv2.__version__
‘4.0.0’

How to  load and display an existing image using OpenCV-Python  ? 

Loading of an image can be done using imread() of OpenCV

retval=cv.imread(filename[, flags])

flags specify the color type of loaded image.

  1. <0 Return the loaded image as it is
  2. =0 Return a grayscale image.
  3. >0 Return a 3-channel color image

Here is the complete set of codes

enum cv::ImreadModes {
cv::IMREAD_UNCHANGED = -1,
cv::IMREAD_GRAYSCALE = 0,
cv::IMREAD_COLOR = 1,
cv::IMREAD_ANYDEPTH = 2,
cv::IMREAD_ANYCOLOR = 4,
cv::IMREAD_LOAD_GDAL = 8,
cv::IMREAD_REDUCED_GRAYSCALE_2 = 16,
cv::IMREAD_REDUCED_COLOR_2 = 17,
cv::IMREAD_REDUCED_GRAYSCALE_4 = 32,
cv::IMREAD_REDUCED_COLOR_4 = 33,
cv::IMREAD_REDUCED_GRAYSCALE_8 = 64,
cv::IMREAD_REDUCED_COLOR_8 = 65,
cv::IMREAD_IGNORE_ORIENTATION = 128
}

Finally  try following code with any picture. Make sure you use correct path and image file name.

Tested Example

import numpy as np
import cv2
myimg = cv2.imread('c:/Users/shreyash/Desktop/test.jpeg',0)
cv2.imshow('image',myimg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Note :  Image will be displayed until any key is hit because of waitkey(0) , waitkey() is must because it is doing some event processing also.

Here is the giff file

opencv-python

OpenCV Reference

Official Website: OpenCV Homepage

License : BSD at  OpenCV License 

Github : OpenCV Github 

Documentation : OpenCV Documentation

Thanks for reading till end. I am trying to improve usability of my  site. Did you find this discussion helpful ? If so,  Please subscribe to YouTube channel Embedkarias well for additional embedded related stuff.

Embedkari  provides  100 mins videos on step-by-step Python uses with Cloud and Machine Learning. These videos are under low cost training and kept at Rs500 only.. Please check the detail  of Low Cost Training Option1.

[list-pages]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.