# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/kaustubh-sadekar/Colab-Notebooks/blob/master/OpenCV-Notebooks/IntroductionToOpenCV.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="7tJfdVetzxyr" colab_type="text"
# # **Introduction to OpenCV**
# In this tuorial we will learn basic methods of OpenCV. The topics we will cover are as follows :
#
#
# 1.   Reading an image
# 2.   Showing an image using matplotlib
# 3.   Basics of Numpy that will be useful in handling images
#

# + id="sfpwqfPLzuSY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="4c147905-34d8-4919-8321-86f173111eff"
# Run this block to files needed for this tutorial
# !git clone https://github.com/kaustubh-sadekar/DataFolder.git

# + [markdown] id="Wda-iosAzwWg" colab_type="text"
# ## Part 1 : Reading, displaying and saving an image using OpenCV

# + id="ZB8pmRDb2fHb" colab_type="code" colab={}
# %matplotlib inline
from matplotlib import pyplot as plt
import cv2
import numpy as np



# + id="ctP7s6BV4o47" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 269} outputId="b2b77e24-a301-45b5-c3a5-532f9926ec7e"

# State the path/location of the image you want to read
img_path = '/content/DataFolder/colab-images/minions1.jpg'

# We read the image from a desired path (img_path) and save it an array (image)
image = cv2.imread(img_path)

# Displaying the image uisng matplotlib pyploy
plt.imshow(image)
plt.show()

# + [markdown] id="dUx4EcV43kDF" colab_type="text"
# **Note:** In OpenCV the RGB image is stored in BGR (Blue - Green - Red) format. But matplot lib considers the image in RGB (Reg - Green - Blue) Format. That is why our minions looks so `scary` !!
#
# How do we fix this ? We will see two methods :
# 1. Using OpenCV method cv2.cvtColor
# 2. Using Numpy array
#
# Basically all we need to do is change the order from BGR image to RGB image. Yeah! just exchange the R and B channels

# + id="I_QDMRTV4jhm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 401} outputId="6f8c510d-84c3-402e-b721-d58673487d6c"
img_path = '/content/DataFolder/colab-images/minions1.jpg'
image = cv2.imread(img_path)

# Let's try to know more about the image using numpy
print("Shape of the image :")
print(image.shape)

print("Why is it a 3D matrix ? ")
print("The BGR image consists of 3 channels of 2D images stacked together")

rows,cols,ch = image.shape

print("Thus we have %r rows, %r columns and %r channels in the image"%(rows,cols,ch))

print("Showing all the three channels individually")
print("Note how G and R channels have so many bright pixels since yellow is made of green and red")

f = plt.figure(num=None, figsize=(16, 12), dpi=80)
ax1 = f.add_subplot(1,3, 1)
plt.imshow(image[:,:,0],cmap='gray', vmin=0, vmax=255)
ax2 = f.add_subplot(1,3, 2)
plt.imshow(image[:,:,1],cmap='gray', vmin=0, vmax=255)
ax3 = f.add_subplot(1,3, 3)
plt.imshow(image[:,:,2],cmap='gray', vmin=0, vmax=255)
ax1.title.set_text('Blue channel of image')
ax2.title.set_text('Green channel of image')
ax3.title.set_text('Red channel of image ')
plt.show(block=True)


# + id="RAqirLHfVH2n" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="129906e0-fa88-43a8-a59d-6d4198e1832e"

# Cool ! now let's convert the BGR image to RGB
# First using OpenCV method 
image2 = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

# Second using Numpy
image3 = np.zeros_like(image)  # Trying to show a way create a zero matrix
image3[:,:,0] = image[:,:,2].copy()
image3[:,:,1] = image[:,:,1].copy()
image3[:,:,2] = image[:,:,0].copy()

f = plt.figure(num=None, figsize=(16, 12), dpi=80)
ax1 = f.add_subplot(1,3, 1)
plt.imshow(image)
ax2 = f.add_subplot(1,3, 2)
plt.imshow(image2)
ax3 = f.add_subplot(1,3, 3)
plt.imshow(image3)
ax1.title.set_text('BGR image imported in Opencv')
ax2.title.set_text('BGR to RGB using OpenCV')
ax3.title.set_text('BGR to RGB using numpy ')
plt.show(block=True)

# + id="yMlN0ovwVfyg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="7d8192c5-968c-4280-cf80-ddf4ed200970"
# Saving an image using opencv
path = "saved_image.jpg"
cv2.imwrite(path,image)
print("File saved at %r"%path)
print("Check in the files")

StarcoderdataJupyterScriptsDedupFiltered