Lab 1

You might also like

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

Lab1

February 27, 2023

0.0.1 Topics -
1. Read Images
2. Changing color modes
3. Display Images using Matplotlib
4. Display Images using OpenCV
5. Write Images
6. Accessing color channels of an image
7. Changing pixel values of an image
8. Cropping an image
9. Resizing an image
10. Flipping an image

[1]: import numpy as np


import matplotlib.pyplot as plt
import cv2

[8]: roses01 = cv2.imread('roses.jpg', cv2.IMREAD_COLOR)


roses01_rgb = cv2.cvtColor(roses01, cv2.COLOR_BGRA2RGB)

[9]: type(roses01)

[9]: numpy.ndarray

[10]: roses01.shape

[10]: (125, 174, 3)

[11]: #print(roses01)

[13]: plt.imshow(roses01_rgb)

[13]: <matplotlib.image.AxesImage at 0x1ae3cccc160>

1
[15]: roses_r, roses_g, roses_b = cv2.split(roses01_rgb)

[50]: plt.imshow(roses_r)

[50]: <matplotlib.image.AxesImage at 0x1ae3c96c790>

2
[49]: plt.imshow(roses_g)

[49]: <matplotlib.image.AxesImage at 0x1ae3ccfc2b0>

3
[51]: plt.imshow(roses_b)

[51]: <matplotlib.image.AxesImage at 0x1ae3c1e5cf0>

4
[20]: roses_g = roses_g-50

[22]: roses02_rgb = cv2.merge((roses_r, roses_g, roses_b))

[23]: plt.imshow(roses02_rgb)

[23]: <matplotlib.image.AxesImage at 0x1ae3c2b5a20>

5
[26]: nemo01 = cv2.imread('nemo3.jpg',cv2.IMREAD_COLOR)
nemo01 = cv2.cvtColor(nemo01, cv2.COLOR_BGR2RGB)
plt.imshow(nemo01)

[26]: <matplotlib.image.AxesImage at 0x1ae3c7c64d0>

6
[28]: nemo01[90, 160]

[28]: array([254, 152, 7], dtype=uint8)

[29]: nemo01[90, 160] = (10, 100, 255)

[30]: nemo01[90, 160]

[30]: array([ 10, 100, 255], dtype=uint8)

[31]: plt.imshow(nemo01)

[31]: <matplotlib.image.AxesImage at 0x1ae3c6e2aa0>

7
[32]: nemo_face = nemo01[75:125, 170:220]

[33]: plt.imshow(nemo_face)

[33]: <matplotlib.image.AxesImage at 0x1ae3c3b0e80>

8
[36]: nemo_face = cv2.cvtColor(nemo_face, cv2.COLOR_BGR2RGB)
cv2.imwrite('nemo_face.jpg', nemo_face)

[36]: True

[39]: nemo_face03 = cv2.resize(nemo_face, None, fx = 0.5, fy = 0.5)

[40]: plt.imshow(nemo_face03)

[40]: <matplotlib.image.AxesImage at 0x1ae3c6b6a70>

9
[41]: plt.imshow(nemo01)

[41]: <matplotlib.image.AxesImage at 0x1ae3e37dcc0>

10
[42]: nemo01_vf = cv2.flip(nemo01, 0)

[43]: plt.imshow(nemo01_vf)

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

11
[44]: nemo01_hf = cv2.flip(nemo01, 1)
plt.imshow(nemo01_hf)

[44]: <matplotlib.image.AxesImage at 0x1ae3c8bb8e0>

12
[45]: nemo01_vhf = cv2.flip(nemo01, -1)
plt.imshow(nemo01_vhf)

[45]: <matplotlib.image.AxesImage at 0x1ae3e11df00>

13
[ ]:

14

You might also like