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

Jupyter Notebook3

Uploaded by

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

Jupyter Notebook3

Uploaded by

RamRakh Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

# Image Processing

In [43]: import cv2

In [7]: #Load Image File into Memory


img=cv2.imread('Mahesh.jpg')
plt.imshow(img)
plt.axis('Off') #Display Image on
plt.title('Mahesh Yadav') #Hide X an Y axis

Out[7]: Text(0.5, 1.0, 'Mahesh Yadav')

OpenCV represents the images in BGR as opposed to the RGB we expect. Since it
is inthe reverse order, you tend to see the blue color in images. We will use
the following codefor converting from BGR to RGB
In [9]: img1=cv2.imread('Mahesh.jpg')
plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB))
plt.title("Mahesh Yadav")

Out[9]: Text(0.5, 1.0, 'Mahesh Yadav')


In [12]:

img3 = cv2.imread('Mahesh.jpg')
gray = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
plt.imshow(gray, cmap='gray')
plt.show()
In [13]: f=plt.figure(figsize=(15,15))
f.add_subplot(1,2,1)
plt.imshow(gray,cmap='gray')
plt.title('Gray Scale Image')

Out[13]: Text(0.5, 1.0, 'Gray Scale Image')


In [14]: f.add_subplot (1,2,2)
plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.title("Coloured Image")

Out[14]: Text(0.5, 1.0, 'Coloured Image')

In [16]: # Check the size of Image


print(img4.shape)

(1600, 1200, 3)
In [21]: # Resizing an Image
resized=cv2.resize(img, (360,640))
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
print(resized.shape)

(640, 360, 3)

In [29]: # Resizing an Image

resized = cv2.resize(img, (int(img.shape[1] * 2), int(img.shape[0] / 2)))


plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))

Out[29]: <matplotlib.image.AxesImage at 0x1e19426dd90>


In [37]: imgcropped=img[700:850,100:800]
plt.imshow(imgcropped)

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

In [38]: # Flip an Image


img5=cv2.flip(img,0)
plt.imshow(img3)

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


In [40]: #Edge Detection
img9=cv2.imread('Mahesh.jpg')
edge=cv2.Canny(img, 200,200)
plt.imshow(edge)

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

In [41]: #Saving an image to your system


cv2.imwrite('Gray.jpg', img9)

Out[41]: True

You might also like