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
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)
To display an image in window, use cv2.imshow()
Window automatically fits to image size.Two Arguments:
Window name (string)
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)
Use the function cv2.imwrite() to save an image. 2 Arguments:
In [24]:
cv2.imwrite('Me1.jpg', img)
Out[24]:
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()
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