0% found this document useful (0 votes)
59 views

Python Code

This document demonstrates various image processing techniques using Matplotlib and OpenCV in Python. It shows how to load an image, extract color channels, modify pixel values, resize and flip images. It also demonstrates drawing shapes like circles, rectangles, polygons and text on images using OpenCV functions.

Uploaded by

Rathan N
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Python Code

This document demonstrates various image processing techniques using Matplotlib and OpenCV in Python. It shows how to load an image, extract color channels, modify pixel values, resize and flip images. It also demonstrates drawing shapes like circles, rectangles, polygons and text on images using OpenCV functions.

Uploaded by

Rathan N
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

jupyterlab

Matplotlib

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

from PIL import Image


img = Image.open('images/rock.jpg')
img

img.rotate(-90)

type(img)

img_array = np.asarray(img)
type(img_array)
numpy.ndarray

img_array.shape

plt.imshow(img_array)

img_test = img_array.copy()

plt.imshow(img_test[:,:,0}) //red

plt.imshow(img_test[:,:,0}, cmap='gray')

plt.imshow(img_test[:,:,1}) //green

plt.imshow(img_test[:,:,2}) // blue

Remove red colour

img_test[:,:,0] = 0

plt.imshow(img_test)

Difference between matplotlib and opencv

import cv2

img = cv2.imread('images/rock.jpg')
type(img)
img.shape

plt.imshow(img)

colour pattern BGR

img_fix = cv2.cvtcolor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_fix)

img_gray = cv2.imread('images/rock.jpg', cv2.IMREAD_GRAYSCALE)


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

Resize Image

img_new =cv2.resize(img_fix,(1000, 400)) //width, height


plt.imshow(img_new)

width_ratio = 0.5
height_ration = 0.5

img2 = cv2.resize(img_fix,(0,0),img_fix,width_ratio,height_ratio)
plt.imshow(img2)
img2.shape

flip on horizontal axis

img_3 = cv2.flip(img_fix,0)

plt.imshow(img_3)

flip on vertical axis

img_3 = cv2.flip(img_fix,1) //flip on horizontal on vertical axis use -1


plt.imshow(img_3)

chnage the size of out canva

last_img = plt.figure(figsize=(10,7))
ilp = last_img.add_subplot(111)
ilp.imshow(img_fix)

draw shapes

black_img = np.zeros(shape=(512,512,3),dtype=np.int16)
black_img.shape
plt.imshow(black_img

draw a circle

cv2.circle(img=black_img,center=(400,100),radius=50,color=(255,0,0),thickness=8)
plt.imshow(black_img)
cv2.circle(img=black_img,center=(400,200),radius=50,color=(0,255,0),thickness=-1)
plt.imshow(black_img)

cv2.rectangle(black_img,pt1=(200,200),pt2=(300,300),color(0,255,0),thickness=5)
plt.imshow(black_img)

vertices = np.array([[10,450,[110,350],[180,450]],np.int32)
pts = vertices.reshape(-1,1,2)
cv2.polylines(black_img,[pts],isClosed=True,color=(0,0,255),thickness=3)
plt.imshow(black_img)

cv2.rectangle(black_img,pt1=(200,50),pt2=(300,150),color(137,79,213),thickness=-1)
plt.imshow(black_img)

vertices = np.array([[10,250,[110,150],[180,250]],np.int32)
pts = vertices.reshape(-1,1,2)
cv2.fillpoly(black_img,[pts],color=(255,167,201))
plt.imshow(black_img)

cv2.line(black_img,pt1=(512,0),pt2=(0,512),color=(255,0,255),thickness=3)
plt.imshow(black_img)

font = cv2.font_hershey_simplex

cv2.putText(black_img,text='Rhyme',org(210,
500),fontFace=font,fontscale=3,color=(255,255,0),thickness=3,lineType=cv2.LINE_AA)
plt.imshow(black_img)

You might also like