Import As Import As Import As

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

7/27/2021 FDP-Day1

In [1]: import numpy as np

import matplotlib.pyplot as plt

import matplotlib.image as mpimage

In [31]: img=mpimage.imread('C:/Users/admin/Desktop/PVT/test_images/peppers_color.jpg')

In [32]: plt.figure(1)

plt.imshow(img)

Out[32]: <matplotlib.image.AxesImage at 0x2ad48483a0>

In [20]: np.shape(img)

Out[20]: (512, 512, 3)

In [33]: img.dtype

Out[33]: dtype('uint8')

In [17]: img_R=img[:,:,0]

plt.imshow(img_R,cmap='gray')

Out[17]: <matplotlib.image.AxesImage at 0x2ad409d8e0>

In [24]: img[100,100,1]

Out[24]: 22

localhost:8888/nbconvert/html/Desktop/PVT/FDP-Day1.ipynb?download=false 1/5
7/27/2021 FDP-Day1

In [10]: np.shape(img_R)

Out[10]: (256, 256)

In [18]: img_G=img[:,:,1]

plt.imshow(img_G,cmap='gray')

Out[18]: <matplotlib.image.AxesImage at 0x2ad40edf70>

In [19]: img_B=img[:,:,2]

plt.imshow(img_B,cmap='gray')

Out[19]: <matplotlib.image.AxesImage at 0x2ad0796340>

In [25]: newimg=np.zeros(np.shape(img))

In [26]: np.shape(newimg)

Out[26]: (512, 512, 3)

In [28]: newimg[:,:,0]=img_R

plt.figure(2)

plt.imshow(newimg)

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats o
r [0..255] for integers).

Out[28]: <matplotlib.image.AxesImage at 0x2ad4067af0>

localhost:8888/nbconvert/html/Desktop/PVT/FDP-Day1.ipynb?download=false 2/5
7/27/2021 FDP-Day1

In [34]: newimg.dtype

Out[34]: dtype('float64')

In [35]: newimg=newimg.astype('uint8')

In [36]: newimg.dtype

Out[36]: dtype('uint8')

In [37]: plt.figure(2)

plt.imshow(newimg)

Out[37]: <matplotlib.image.AxesImage at 0x2ad4899a00>

In [38]: newimg[:,:,1]=img_G

plt.figure(3)

plt.imshow(newimg)

Out[38]: <matplotlib.image.AxesImage at 0x2ad41e6340>

localhost:8888/nbconvert/html/Desktop/PVT/FDP-Day1.ipynb?download=false 3/5
7/27/2021 FDP-Day1

In [43]: def rgb2gray(inp):

r,g,b=inp[:,:,0],inp[:,:,1],inp[:,:,2]

gray=0.2989*r+0.5870*g+0.1140*b

return gray

img_gray=rgb2gray(img)

plt.figure(4)

plt.imshow(img_gray,cmap='gray')

Out[43]: <matplotlib.image.AxesImage at 0x2ad428fbe0>

In [45]: plt.hist(img_gray)

plt.xlabel('graylevels')

plt.ylabel('frequency of graylevels')

Out[45]: Text(0, 0.5, 'frequency of graylevels')

localhost:8888/nbconvert/html/Desktop/PVT/FDP-Day1.ipynb?download=false 4/5
7/27/2021 FDP-Day1

In [48]: img_gray=img_gray+60;

plt.figure(5)

plt.imshow(img_gray,cmap='gray')

plt.figure(6)

plt.hist(img_gray)

plt.xlabel('graylevels')

plt.ylabel('frequency of graylevels')

plt.xlim([0,255])

Out[48]: (0.0, 255.0)

In [ ]:

localhost:8888/nbconvert/html/Desktop/PVT/FDP-Day1.ipynb?download=false 5/5

You might also like