Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

OpenCV Introduction

OpenCV Image Read & Write

OpenCV Image Channels

OpenCV Color Conversion

OpenCV Rotation & Crop

OpenCV Text & Shape

OpenCV Video

OpenCV Arithmetics Operation

OpenCV Thresholding

OpenCV Blur & Blending

OpenCV Edge Detection

OpenCV Contours

OpenCV Hough Transformation Lines

OpenCV Moments & Convexhull

OpenCV Morphological Transformation

OpenCV Template Matching

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Opencv arithmetics operation

Arithmetics Operation using OpenCV

Here we perform subtraction, addition, multiplication with image matrices. We know that image is in the form of matrix and inside matrix, each cell has a number between 0-255 and these numbers are for the color. There are multiple channels and by combining these channels you will get a color image. Now if we add, multiply or subtract these numbers with another number then we will get a new different color image. Let's see how to perform these types of operations.

Input
import numpy as np
import cv2

path2=r"E:\picture\prac_deep\dd.jpg"
path3=r"E:\picture\prac_deep\road.jfif"

img=cv2.imread(path2)
img2=cv2.imread(path3)

img=cv2.resize(img,(800,700))
img2=cv2.resize(img2,(800,700))

cv2.imshow("my image 1",img)

M=np.ones(img.shape,dtype="uint8")*150
'''
Here we created a ones matrix and multiply with 150. It means here each cell contains a value of 150. So we can use this M matrix as an image channel because its size and dtype is equal to the main image.
Next, we will add, multiply or subtract this channel with the image.
'''

#Let's perform addition
Add1=cv2.add(img,M)
cv2.imshow("my image 2",Add1)

#Let's perform subtraction
Subtract1=cv2.subtract(img,M)
cv2.imshow("my image 3",Subtract1)

#Let's perform multiplication
Multiply1=cv2.multiply(img,M)
cv2.imshow("my image 4",Multiply1)

#Let's add two different images
Add2=cv2.add(img,img2)
cv2.imshow("my image 5",Add2)

#Let's subtract two different images
Subtract2=cv2.subtract(img,img2)
cv2.imshow("my image 6",Subtract2)

#Let's multiply two different images
Multiply3=cv2.multiply(img,img2)
cv2.imshow("my image 7",Multiply3)

ex4=img-img2
cv2.imshow("my image 8",ex4)

ex5=img+img2
cv2.imshow("my image 9",ex4)

ex6=img*img2
cv2.imshow("my image 10",ex4)

cv2.waitKey(0)
cv2.destroyAllWindows()
Output

Bitwise operation

Input
import numpy as np
import cv2

Square=np.zeros((300,300),np.uint8)
'''
Here we created a square matrix each pixel's value is 0. It means black. It is square because the size is 300,300 and the data type is uint8.
'''
cv2.rectangle(Square,(50,50),(250,250),255,-1)
cv2.imshow("square",Square)
'''
Here we want to create a square image where each pixel value is 0 means black and inside that square black image we want to create a white rectangle inside that black image. Here 50,50 and 250,250 is the size and position of the square, 255 is the for white color and -1 is thickness. The thickness value is -1 because we want to fill the shape.
'''

Ellipse=np.zeros((300,300),np.uint8)
cv2.ellipse(Ellipse,(150,150),(150,150),30,0,180,255,-1)
cv2.imshow("square",Ellipse)

And=cv2.bitwise_and(square,ellipse)
cv2.imshow("And",And)

Or=cv2.bitwise_or(square,ellipse)
cv2.imshow("Or",Or)

Xor=cv2.bitwise_xor(square,ellipse)
cv2.imshow("Xor",Xor)

Not=cv2.bitwise_not(square)
cv2.imshow("Not",Not)

cv2.waitKey(0)
cv2.destroyAllWindows()
Output

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.