Getting Started with Images

cv2.imread, cv2.imshow, cv2.imwrite

Reading an image - cv2.imread()

cv2.imread() has two arguments, one address of the image and the other as following arguments: (specifies the way image should be read)

cv2.IMREAD_COLOR : loads a color image. Transparency of image is neglected, it is default flag. [Alternate : 1]

cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode. [Alternate : 0]

cv2.IMREAD_UNCHANGED : Loads image such as including alpha channel. [Alternate : -1]


In [1]:
import numpy as np
import cv2

In [2]:
ls


C++/                                        tkinter12.py
Drawing Functions in OpenCV.ipynb           tkinter_start.py
GeeksFG/                                    ulem.sty
Getting Started with Images - OpenCV.ipynb  UNIX Jupyter Example.ipynb
Getting started with Python.ipynb           UNIX-Jupyter-Notebook-Example.ipynb
ImgProc/                                    Untitled10.ipynb
Introducing Pandas.ipynb                    Untitled1.ipynb
Introduction to Numpy.ipynb                 Untitled2.ipynb
Introduction to Pandas.ipynb                Untitled3.ipynb
Kush.png                                    Untitled4.ipynb
Me1_gray.jpg                                Untitled5.ipynb
Me1.jpg                                     Untitled6.ipynb
Me1.png                                     Untitled7.ipynb
president_heights.csv                       Untitled8.ipynb
PythonDataScienceHandbook-master/           Untitled9.ipynb
python-for-competitive-programming-master/  Untitled.ipynb
Python-Lectures-master/                     Untitled.pdf
SMG.mp4                                     Week-3-Numpy/
startup.py

In [3]:
file_adr = 'Me1.png'

In [4]:
img = cv2.imread(file_adr,cv2.IMREAD_GRAYSCALE) # Alternate- 0 cv2.IMREAD_GRAYSCALE - 0 
cv2.imwrite('Me1_gray.jpg', img) # 
img2 = cv2.imread('Me1_gray.jpg', cv2.IMREAD_COLOR)

Showing an image - cv2.imshow()

To display an image in window, use cv2.imshow()

Window automatically fits to image size.Two Arguments:

  1. Window name (string)

  2. Our image


In [ ]:
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

About cv2.waitKey() function: Keyboard binding function Argument in milliseconds

If zero is passed, it waits indefinitely for a key stroke.

About cv2.destroyAllWindows(): Destroy all windows we created To destroy any specific window, use cv2.destroyWindow(), pass argument with window's name.


In [ ]:
cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Image', img)
cv2.waitKey(5)
cv2.destroyWindow('Image')

cv2.waitKey?

help(cv2.namedWindow)

Writing an Image

Use the function cv2.imwrite() to save an image. 2 Arguments:

  1. First argument is file name
  2. Image you want to save

In [24]:
cv2.imwrite('Me1.jpg', img)


Out[24]:
True

In [25]:
import numpy as np
import cv2

img2 = cv2.imread('Me1.png', -1)
cv2.imshow('IMAGE', img2)
k = cv2.waitKey(0) & 0xFF
if k == 27:
    cv2.destroyAllWindows()
elif k == ord('s'):
    cv2.imwrite('Oh.png', img2)
    cv2.destroyAllWindows()

Using matplotlib


In [ ]:
import numpy as np
import cv2
from matplotlib import pyplot as plt
import seaborn; seaborn.set()
img = cv2.imread('Me1.png',0)
plt.imshow(img, cmap = 'gray', interpolation='bicubic')
# plt.xticks([]), plt.yticks([]) # To hide tick values on x & y -axis
plt.show()
# 255, 255, 0 -  R G B

Color image loaded by OpenCV is in BGR Mode. Matplotlib displays in RGB mode