0% found this document useful (0 votes)
30 views6 pages

CV Notebook

Uploaded by

hunarkhurana08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

CV Notebook

Uploaded by

hunarkhurana08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

29/04/2023, 10:38 2023- CV notebook.

ipynb - Colaboratory

import cv2 # import OpenCV


from matplotlib import pyplot as plt # import matplotlib
import numpy as np # import numpy

img = cv2.imread('original_man.jpg') #Load the image file into memory


plt.imshow(img)
plt.title('man')
plt.axis('on')
plt.show()

img = cv2.imread('original_man.jpg') #Load the image file into memory


plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('on')
plt.show()

img1 = cv2.imread('tulips.jpeg',0) #Load the image file into memory


#cl=cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
#gr=cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
plt.imshow(img1,cmap='gray')
plt.title('flowers')
plt.axis('off')
plt.show()

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 1/6
29/04/2023, 10:38 2023- CV notebook.ipynb - Colaboratory

img = cv2.imread('original_man.jpg')
print(img.shape)

(3967, 2977, 3)

img = cv2.imread('original_man.jpg',0)
print(img.shape)

(3967, 2977)

img = cv2.imread('original_man.jpg')
print (img.min())
print (img.max())

0
255

img = cv2.imread('original_man.jpg')

# Extract a square that focuses on the man's face


# Remember, your current image size is 3967 x 2977
roi = img[1500:2500,1000:2000] #img[range of y, range of x]
plt.imshow(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('on')
plt.show()

img = cv2.imread('original_man.jpg')
img[1500:3000,1000:2000] = [35,100,200]
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('on')
plt.show()

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 2/6
29/04/2023, 10:38 2023- CV notebook.ipynb - Colaboratory

# Extract a part of the image that focuses on the word 'JACK'


# Remember, your current image size is 3967 x 2977
img = cv2.imread('original_man.jpg')
jack = img[3500:3900, 1000:1500]
plt.imshow(cv2.cvtColor(jack, cv2.COLOR_BGR2RGB))
plt.title('jack')
plt.axis('on')
plt.show()

img = cv2.imread('original_man.jpg')

jack = img[3500:3900, 1000:1500]


img[0:400,0:500]=jack
img[400:800,500:1000]=jack
img[800:1200,1000:1500]=jack

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('more jacks')
plt.axis('on')
cv2.imwrite('copied.jpg',img)
cv2.imwrite('new.jpg',img)
plt.show()

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 3/6
29/04/2023, 10:38 2023- CV notebook.ipynb - Colaboratory
# resize the image to 200x200px, ignoring aspect ratio
img = cv2.imread('original_man.jpg')
resized = cv2.resize(img, (200, 200))
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('off')
plt.show()
print(resized.shape)

(200, 200, 3)

# fixed resizing and distort aspect ratio so let's resize the width
# to be 300px but compute the new height based on the aspect ratio
# Remember, your current image size is 3967 x 2977
img = cv2.imread('original_man.jpg')
resized=cv2.resize(img,(int(img.shape[1]/4),int(img.shape[0]/4)))
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title('man')
plt.axis('off')
plt.show()
print(resized.shape)

(991, 744, 3)

#rotating an image
img_rotate=cv2.rotate(img, cv2.ROTATE_180) #other values can be cv2.ROTATE_90_CLOCKWISE ; cv2.ROTATE_90_COUNTERCLOCKWISE
plt.imshow(img_rotate)

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 4/6
29/04/2023, 10:38 2023- CV notebook.ipynb - Colaboratory

<matplotlib.image.AxesImage at 0x7f6f80b36f80>

#flipping an image vertically , around x axis


fl=cv2.flip(img,0)
plt.imshow(fl)

<matplotlib.image.AxesImage at 0x7f6f71d0f460>

#flipping an image horizontally , around y axis


fl1=cv2.flip(img,1)
plt.imshow(fl1)

<matplotlib.image.AxesImage at 0x7f6f71e2c9d0>

cv2.imwrite('original_man.jpg',img)
cv2.imwrite('resized_man.jpg',resized)

True

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 5/6
29/04/2023, 10:38 2023- CV notebook.ipynb - Colaboratory

check 3s completed at 10:34

https://colab.research.google.com/drive/1Vm0qVpLn1dAIkRbVyGwQGtPABFpIm8kE#scrollTo=OO4liNjrNb2s&printMode=true 6/6

You might also like