IMAGE
to read an image first we have to import cv2 package which has a function imread that helps us to read an image then you have to place the image
to show the image you have to use the function imshow() this function has two attributes 1-> The first one is the name of the image 2-> One is the image variable
to delay the image time we have to use waitkey() function if we add 0 it i infinitely if we add 1 one millisecond delay
VIDEO
now first we have to import cv2 again then we use function VideoCapture()
then we have to read it frame by frame
cv2.imshow("img",img) cv2.waitKey(0)
img2=cv2.VideoCapture('sample.mp4')
while True: img,suu=img2.read() cv2.imshow('v',suu) if cv2.waitKey(1) & 0xFF==ord('q'): break
A webcam is also similar to a video cam just replace it with video location with the webcam number and we can also re-size it using this
img2.set(3,340)this is 3->width,340->width size img2.set(4,380)this is 4->height,380->height size
we can also change the brightness by using
img2.set(10,100) 10->is id for brightness
Now first thing we will do that to convert it into gray scale
first you have to import cv2
and use a function cvtColor() function .for creating a image we use rgb but in cv2 it is bgr to covert it into gray we use the following code
import cv2
img=cv2.imread('images.jpg')
imgGray=cv2.cvtColor(img,cv2.COLOR_BGRA2GRAY) imgblur=cv2.GaussianBlur(imgGray,(7,7),0)
cv2.imshow('imgGray',imgGray) cv2.imshow('imgblur',imgblur) cv2.waitKey(0)
Next we work on canny edge dettection
imgcanny=cv2.Canny(img,100,100)
to reduse the edges change the threshold values
imgcanny=cv2.Canny(img,150,250) you can seethe diffrence
Dilation -> some times the edges are not connected we can increase the thickness useing dilation
KERNAL IS NOTING BUT A MATRIX VALUE WHICH WE HAV TO EEFINE MATRIX VALUE AND SIZE FOR THIS WE NEED THE MATRIX VALUE AS ONE
For doing this we need numpy which we have to import it
np.uint8 refers to an 8-bit unsigned integer data type in the NumPy library in Python. It stands for "NumPy unsigned integer 8-bit". This data type can represent integers ranging from 0 to 255. It is commonly used for storing image data, especially in grayscale images, where pixel values typically range from 0 (black) to 255 (white).
Now we have to create s Kernal =np.onse((5,5),np.unit8)
image after applying Dialation
The next function is Erode
Erode is the reverse process of Dialation
imgEroded=cv2.erode(imgDialation,kernal,iterations=1)
opencv convention
imgresize=cv2.resize(img(200,300)) first width and then height
in open cv fuction width will come first then height
for croping we wont need cv2 we cann do it useing matrix
imgcrop=img[0:300,50:100]
SHAPES_AND_TEXT
cv2.line(img,(0,0),(300,300),(0,255,0),3) for drawing a line
cv2.rectangle(imgr,(0,0),(200,200),(0,0,255),3) for drawing rectangle
cv2.rectangle(imgr,(0,0),(200,200),(0,0,255),cv2.FILLED)
cv2.circle(imgr,(150,150),8,(255,0,0),cv2.FILLED)
cv2.putText(imgr,"opencv text",(200,100),cv2.FONT_HERSHEY_PLAIN,1,(0,150,0),1)
WARP_PERSPECTIVE
JOINING_IMAGES
import numpy as np import cv2
img=cv2.imread('images.jpg')
imghor=np.hstack((img,img)) imgver=np.vstack((img,img))
cv2.imshow("horizontal",imghor) cv2.imshow("vertical",imgver) cv2.waitKey(0)
#Chapter 7 COLOR_DETECTION
imghsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
We are creating a trackbar
first, we have to create a named window then we have to resize the window then from cv2 use the function createTrackbar first enter the value you want then the window name starting and ending, then a function
CONTOURS/SHAPE DETECTION