<jupyter_start><jupyter_text>**Introduction to OpenCV**  *OpenCV* is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection. 1. Open CV is an Open Source, C++ based API(Application Programming Interface) Library for Computer Vision2. It includes several computer vision algorithms3. It is a library written in C++, but has also its binding in Python. This means that C++ code is executed behind the scene in Python.4. Why in Python? It's a bit easy to implement for beginning5. Explore more of OpenCV [here](https://opencv.org/) - **Computer Vision** : Computer Vision can be defined as a discipline that explains how to reconstruct, interrupt, and understand a 3D scene from its 2D images, in terms of the properties of the structure present in the scene. It deals with modeling and replicating human vision using computer software and hardware.- Computer Vision overlaps significantly with the following fields −  1. **Image Processing** − It focuses on image manipulation.  2. **Pattern Recognition** − It explains various techniques to classify patterns.  3. **Photogrammetry** − It is concerned with obtaining accurate measurements from images.  **Computer Vision Vs Image Processing :** 1. Image processing deals with image-to-image transformation. The input and output of image processing are both images.2. Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures in 3D scene.  **Applications of Computer Vision :**1. **Medicine Application** :   - Classification and detection (e.g. lesion or cells classification and tumor detection)  - 3D human organ reconstruction (MRI or ultrasound)  - Vision-guided robotics surgery_____________________2. **Industrial Automation Application**  - Defect detection  - Assembly  - Barcode and package label reading  - Object sorting_____________________3. **Security Application**  - Biometrics (iris, finger print, face recognition)_____________________4. **Surveillance**:  - Detecting certain suspicious activities or behaviors_____________________5. **Transportation Application**  - Autonomous vehicle  - Safety, e.g., driver vigilance monitoring  Function DescriptionsRefer this from **google** and [OpenCv Docs](https://docs.opencv.org/master/index.html)| Sr. No. | FunctionName                                                                                                             | Parameters                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | Description                                                                                                                                                                                                                                                       ||:------------|:-----------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|| 1           | cv2.imread(filename, [flags])                                                                                                | filename: Name of file to be loaded.flags: Flag that can take values ofcv::ImreadModes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | The function imread loads an image from the specified file and returns it.                                                                                                                                                                                        || 2           | cv2.imshow(window_name, image)                                                                                           | window_name: A string representing the name of the window in which image to be displayed.image: It is the image that is to be displayed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | cv2.imshow() method is used to display an image in a window. The window automatically fits to the image size.It doesn’t return anything.                                                                                                                          || 3           | cv2.cvtColor(src, code[, dst[, dstCn]])                                                                                      | src: It is the image whose color space is to be changed.code: It is the color space conversion code.dst: It is the output image of the same size and depth as src image. It is an optional parameter.dstCn: It is the number of channels in the destination image. If the parameter is 0 then the number of the channels is derived automatically from src and code. It is an optional parameter.                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | cv2.cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV.It returns an image.                                                                            || 4           | cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])                                                               | src: source/input imagedsize: desired size for the output imagefx: scale factor along the horizontal axisfy: scale factor along the vertical axis                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | To resize an image, OpenCV provides cv2.resize() function.                                                                                                                                                                                                        || 5           | cv2.addWeighted(src1, alpha, src2, beta, gamma[,dst[, dtype]])                                                       | src1: first input arrayalpha: weight of the first array elementssrc2: second input array of the same size and channel number as src1beta: weight of the second array elementsdst: output array that has the same size and number of channels as the input arraysgamma: scalar added to each sumdtype: optional depth of the output array                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | This function is used to output the weighted sum of overlapping/blending of two images.                                                                                                                                                                           || 6           | cv2.threshold(source, thresholdValue, maxVal, thresholdingTechnique)                                                     | source: Input Image array (must be in Grayscale). thresholdValue: Value of Threshold below and above which pixel values will change accordingly.maxVal: Maximum value that can be assigned to a pixel.thresholdingTechnique: The type of thresholding to be applied. eg. simple, binary etc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | This function is used for thresholding. The basic Thresholding technique is Binary Thresholding. For every pixel, the same threshold value is applied. If the pixel value is smaller than the threshold, it is set to 0, otherwise, it is set to a maximum value. || 7           | cv2.bitwise_and(src1, src2[, dst[, mask]])                                                                               | src1: first input array or a scalar.src2: second input array or a scalar.dst: output array that has the same size and type as the input arrays.mask: optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | This function computes bitwise conjunction/AND of the two arrays (dst = src1 & src2) Calculates the per-element bit-wise conjunction of two arrays or an array and a scalar.                                                                                      || 8           | cv2.bitwise_not(src[, dst[, mask]])                                                                                      | src: input array.dst: output array that has the same size and type as the input array.mask: optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | This function Inverts every bit of an array .                                                                                                                                                                                                                     || 9           | cv2.bitwise_or(src1, src2[, dst[, mask]])                                                                                | src1: first input array or a scalar.src2: second input array or a scalar.dst: output array that has the same size and type as the input arrays.mask: optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | This function Calculates the per-element bit-wise disjunction/OR of two arrays or an array and a scalar.                                                                                                                                                          || 10          | bitwise_xor(src1, src2[, dst[, mask]])                                                                                   | src1: first input array or a scalar.src2: second input array or a scalar.dst: output array that has the same size and type as the input arrays.mask: optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | This function Calculates the per-element bit-wise "exclusive or" operation on two arrays or an array and a scalar.                                                                                                                                            || 11          | cv2.inRange(src, lowerb, upperb[, dst])                                                                                  | src: first input array.lowerb: inclusive lower boundary array or a scalar.upperb: inclusive upper boundary array or a scalar.dst: output array of the same size as src and CV_8U type.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | This function Checks if array elements lie between the elements of two other arrays.                                                                                                                                                                              || 12          | cv2.Canny(image, threshold1, threshold2[, edges[, apertureSize[, L2gradient]]])                                      | image: 8-bit input image.edges: output edge map; single channels 8-bit image, which has the same size as image .threshold1: first threshold for the hysteresis procedure.threshold2: second threshold for the hysteresis procedure.apertureSize: aperture size for the Sobel operator.L2gradient: a flag, indicating whether a more accurate L2 norm.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | This function Finds edges in an image using the Canny algorithm                                                                                                                                                                                                   || 13          | cv2.findContours(image, mode, method[, contours[, hierarchy[, offset]]])                                             | image: Source, an 8-bit single-channel image. Non-zero pixels are treated as 1's. Zero pixels remain 0's, so the image is treated as binary. contours: Detected contours. Each contour is stored as a vector of pointshierarchy: Optional output vectormode: Contour retrieval modemethod: Contour approximation methodoffset: Optional offset by which every contour point is shifted.                                                                                                                                                                                                                                                                                                                                                                                                                                                                               | This function Finds contours in a binary image. The contours are a useful tool for shape analysis and object detection and recognition.                                                                                                                       || 14          | cv2.drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]]) | image: Destination image.contours: All the input contours. Each contour is stored as a point vector.contourIdx: Parameter indicating a contour to draw. If it is negative, all the contours are drawn.color: Color of the contours.thickness: Thickness of lines the contours are drawn with. If it is negative (for example, thickness=FILLED), the contour interiors are drawn.lineType: Line connectivity. hierarchy: Optional information about hierarchy. It is only needed if you want to draw only some of the contours (see maxLevel ).maxLevel: Maximal level for drawn contours. If it is 0, only the specified contour is drawn. If it is 1, the function draws the contour(s) and all the nested contours. If it is 2, the function draws the contours, all the nested contours, all the nested-to-nested contours, and so on.  . | This function Draws contours outlines or filled contours.The function draws contour outlines in the image if thickness ≥0or fills the area bounded by the contours if thickness<0                                                                         |  Reading and Ploting(Writing) Images using OpenCV- By default OpenCV reads and acquires the image's channels in BGR format order(Blue Green Red).<jupyter_code># Importing the OpenCV library
import cv2

# Reading the image using imread() function
img = cv2.imread('images/DK.jpeg') # Here the path of the image to be read is written

print(type(img))

# Extracting the height and width of an image
# h, w = img.shape[:2]
# or 
h, w, c = img.shape[:]
# c indicates number of channels

# Displaying the height and width of the image (pixels)
print("Height = {},  Width = {}, c = {}".format(h, w, c))
# imshow - arguments : window_name, image_name
cv2.imshow('sample_image', img)

# wait to execute next operation until a key is pressed, keep showing the image windows
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Matplotlib:1. Matplotlib is a cross-platform, data visualization and graphical plotting library for Python.2. Its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB(mathematics library). 3. Developers can also use matplotlib’s APIs (Application Programming Interfaces) to embed plots in GUI applications. i.e can be used in other applications as well.4. OpenCV understands, processes and reads an image by default in BGR channel format (Blue - Green - Red).5. Whereas Matplotlib understands, processes and reads an image by default in RGB channel format (Red - Green - Blue).6. Hence while using OpenCV and matplotlib together, one needs to take care about conversion of image between BGR and RGB format.  Plotting the OpenCV read image with matplotlib<jupyter_code># import matplotlib for using plotting functionalities
import matplotlib.pyplot as plt

# plotting image using matplotlib
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Plotting and reading image using Matplotlib<jupyter_code># Both Reading and Plotting Image using Matplotlib
img2 = plt.imread('images/DK.jpeg')
plt.imshow(img2)<jupyter_output><empty_output><jupyter_text>Conversion from BGR to RGB format using cvtColor() function of OpenCV<jupyter_code># Converting OpenCV read image (BGR format) to RGB format and ploting using matplotlib
# Here cvtColor function accepts the image and the colour conversion code
img3 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img3)
# Any image is made up of arrays and numbers -> each pixel's color value is represented in terms of pixels
img3
# images -> these are NUMPY arrays
print(type(img))
print(type(img2))
print(type(img3))<jupyter_output><class 'numpy.ndarray'>
<class 'numpy.ndarray'>
<class 'numpy.ndarray'><jupyter_text>1. Here each image object i.e. the images are numpy arrays.2. ndarray means a n dimensional array<jupyter_code># Data type of elements in numpy array which represents an image
print(img.dtype)
print(img2.dtype)<jupyter_output>uint8
uint8<jupyter_text>Combining two imageseg. **Image1** = Windows Logo; **Image2** = Linux Logo; **Image3 = Image1 + Image2** Illustrations<jupyter_code>img4 = cv2.imread('images/purple_night.jpg')
img4 = cv2.resize(img4, (img.shape[1],img.shape[0]))
img_res = img + img4
cv2.imshow("purple_night",img4)
cv2.imshow("result",img_res)
cv2.waitKey(0)
cv2.destroyAllWindows()
plt.imshow(img_res)<jupyter_output><empty_output><jupyter_text>Converting Image to Grayscale Illustrations<jupyter_code># Converting image to grayscale (black and white/ monocolor)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Grayscale",gray)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Resizing an Image Illustrations<jupyter_code># Initializing Height and Width
h = 200
w = 900

# Resizing using resize() function 
resizeImg = cv2.resize(img, (w,h))

cv2.imshow("resized", resizeImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Checking the shape
h, w, c = resizeImg.shape
print(h,w,c)<jupyter_output>200 900 3<jupyter_text>Other Properties of Image<jupyter_code># Total number of pixels using size
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

pix = img.size
print("Number of Pixels: {}".format(pix))

# Image Datatype
dType = img.dtype
print("Datatype: {}".format(dType))<jupyter_output>Number of Pixels: 2580480
Datatype: uint8<jupyter_text>Creating a Black Background using zeros() function in Numpy Library<jupyter_code>import cv2
import numpy as np
blImg = np.zeros((800,800,3))
print(blImg.shape)

cv2.imshow("black",blImg)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output>(800, 800, 3)<jupyter_text>Changing value of ROI (Region of Interest)- ROI is a part/subset of the image i.e a set of pixels- Example of ROI Extraction Illustrations<jupyter_code># bgr format
im2 = blImg.copy()

# green 
im2[45:400,35:300] = (0,255,0)
cv2.imshow("im2",im2)

# blue
im2[450:500,350:590] = (255,0,0)
cv2.imshow("im2_1",im2)

# red
im2[700:840,650:] = (0,0,255)
cv2.imshow("im2_2",im2)

# pink = red + blue
im2[500:570,550:600] = (71,0,170)
cv2.imshow("im2_3",im2)

# yellow = green + blue
im2[:170,550:] = (0,255,255)
cv2.imshow("im2_4",im2)


roi = im2[100:700, 340:780]
cv2.imshow("roi", roi)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Image BlendingThis is also image addition, but different weights are given to images in order to give a feeling of blending or transparency. Images are added as per the equation below:                    g(x)=(1−α)f0(x)+αf1(x)By varying α from 0→1, you can perform a cool transition between one image to another.Here I took two images to blend together. The first image is given a weight of 0.7 and the second image is given 0.3. cv.addWeighted() applies the following equation to the image:                     dst=α⋅img1+β⋅img2+γHere γ is taken as zero.**Note** : Image combination is a subset of Image Blending. In image blendign we can specify the amont/percentage of effect that we want form either of the input images.  Illustrations<jupyter_code>import cv2

img1 = cv2.imread('images/dummy1.jpg')
# img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
cv2.imshow("1mg1",img1)
print(img1.shape)

img2 = cv2.imread('images/purple_night.jpg')
# img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
cv2.imshow("img2",img2)
print(img2.shape)

alpha = 0.2
beta = 1 - alpha
# Blending using addWeighted() function which accepts:
# g(x)=(1−α)f0(x)+αf1(x)
# Source of 1st image
# Alpha
# Source of 2nd image
# Beta
# Gamma

dst = cv2.addWeighted(img1,alpha,img2,beta,0.0)

cv2.imshow("weighted",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output>(1080, 1920, 3)
(1080, 1920, 3)<jupyter_text>Bitwise Operations and Masking1. Lower pixel values are close to BLACK2. Higher pixel values are close to WHITE3. These operations are done bitwise i.e. Pixel wise. 4. The logic of operation remains same as that of seen in logical/ bitwise operations Illustrations Input Images  Bitwise AND Bitwise OR Bitwise XOR Bitwise NOT1. Image1 - Bitwise NOT/inversion of Input Image 1  2. Image2 - Bitwise NOT/inversion of Input Image 2   Masking1. Masking is an image processing method in which we define a small 'image piece' and use it to modify a larger image.  2. When talking about editing and processing images the term 'masking' refers to the practice of using a mask to protect a specific area of an image, just as you would use masking tape when painting your house. 3. Masking an area of an image protects that area from being altered by changes made to the rest of the image.4. Masking is the process that is underneath many types of image processing, including edge detection, motion detection, and noise reduction5. Usually binary masks are used (BLACK and WHITE)6. Generally, BLACK portions are those which are undesired and want to be removed/ masked off6. Generally, WHITE portions are those which are desired and want to be retained in the original image<jupyter_code># Load two images
import cv2
img1 = cv2.imread('images/DK.jpeg')
img2 = cv2.imread('images/bcci.png')
# img2 = cv2.imread('images/opencv.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
print(img2.shape)
roi = img1[0:rows, 0:cols]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)

# each pixel having value more than 150(threshold) will be set to white i.e max_value(255) for a mask/ HIGH
# pixel values lower than threshold are set 
ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
# inverting colors
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi, roi, mask = mask)

# # Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)

# # Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst
cv2.imshow('img1',img1)
cv2.imshow('img2',img2)
cv2.imshow('roi',roi)
cv2.imshow('img2gray',img2gray)
cv2.imshow('mask',mask)
cv2.imshow('mask_inv',mask_inv)
cv2.imshow('img1_bg',img1_bg)
cv2.imshow('img2_fg',img2_fg)

cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output>(224, 225, 3)<jupyter_text>Detecting an object based on the range of pixel values in the HSV colorspace using inRange() HSV colorspace1. HSV (hue, saturation, value) colorspace is a model to represent the colorspace similar to the RGB color model. 2. Since the hue channel models the color type, it is very useful in image processing tasks that need to segment objects based on its color. 3. Variation of the saturation goes from unsaturated to represent shades of gray and fully saturated (no white component). 4. Value channel describes the brightness or the intensity of the color.  Object Detection Illustration<jupyter_code>import cv2

# Read image
img = cv2.imread("images/DK.jpeg")
cv2.imshow("img", img)

## convert image to hsv
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
cv2.imshow("hsv", hsv)

# hsv format -> (min hue, min saturation , min value) -> (max hue, max saturation , max value)
# RED mask =>
# mask1 = cv2.inRange(hsv, (0, 100, 0), (10,255 ,255))

# YELLOW mask =>
mask1 = cv2.inRange(hsv, (15,30,150), (36, 255, 255))

cv2.imshow("mask1", mask1)

# BLUE mask =>
mask2 = cv2.inRange(hsv, (33,52,80), (150, 200, 255))
cv2.imshow("mask2", mask2)

## final mask and masked
mask = cv2.bitwise_or(mask1, mask2)
cv2.imshow("mask", mask)

target = cv2.bitwise_and(img,img, mask=mask)

cv2.imshow("target",target)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>CountoursContours can be explained simply as a curve joining all the continuous points (along the boundary), having same color or intensity. The contours are a useful tool for shape analysis and object detection and recognition.Illustration:<jupyter_code>import cv2
image = cv2.imread('images/DK.jpeg')
  
# Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)

# Find Canny edges
edged = cv2.Canny(gray, 170, 250)
cv2.imshow("edged", edged)

# Finding Contours
# Use a copy of the image e.g. edged.copy()
# since findContours alters the image, finds countours
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  
cv2.imshow('Canny Edges After Contouring', edged)
print("Number of Contours found = " + str(len(contours)))
  
# Draw all contours
# -1 signifies drawing all contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09111563541943289, 'fraction_numerical': 0.017408751585966775, 'mean_word_length': 1.051449670116821, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 64, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 571, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '166515', 'license': 'MIT', 'n_tokens_mistral': 6586, 'n_tokens_neox': 6136, 'n_words': 2616, 'path': '3_cv_basics/5_opencv_overview.ipynb', 'repo_name': 'MOLOCH-dev/Pixels_Seminar', 'size': '33891'}