| Loading and Saving Images:
                        
                                    
                        | img = cv2.imread('image.jpg', cv2.IMREAD_COLOR) #Load image
cv2.imshow('image',img) #Show image
cv2.imwrite('output.jpg',img) #Save image
cv2.waitKey(0) #Wait for a keyboard event
cv2.destroyAllWindows() #Close all windows
 |  Image Properties:
                        
                                    
                        | img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
print('Image Shape:', img.shape) #Prints (height, width, channels)
print('Image Size:', img.size) #Prints the total number of pixels
print('Image Datatype:', img.dtype) #Prints the datatype of the pixels
 |  Accessing a Camera/ Video Capture:
                        
                                    
                        | cap = cv2.VideoCapture(0) #Open default camera
while(True):
    ret, frame = cap.read() #Read frame from camera
    cv2.imshow('frame',frame) #Show frame
    if cv2.waitKey(1) & 0xFF == ord('q'): #Quit when 'q' is pressed
        break
 |  Accessing a Video File:
                        
                                    
                        | cap = cv2.VideoCapture('video.mp4') #Open video file
while(cap.isOpened()):
    ret, frame = cap.read() #Read frame from video
    cv2.imshow('frame',frame) #Show frame
    if cv2.waitKey(25) & 0xFF == ord('q'): #Quit when 'q' is pressed
        break
 |  Video Writer:
                        
                                    
                        | cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID') #Codec
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) #Create video writer
while(cap.isOpened()):
    ret, frame = cap.read() #Capture frame-by-frame
    if ret==True:
        out.write(frame) #Write frame to output video
        cv2.imshow('frame',frame) #Display frame
        if cv2.waitKey(1) & 0xFF == ord('q'): #Exit loop on 'q' key press
            break
    else:
        break
cap.release() #Release camera
out.release() #Release video writer
cv2.destroyAllWindows()
 |  Colorspace Conversion:
                        
                                    
                        | img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #Convert to grayscale
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) #Convert to HSV colorspace
 |  Blurring:
                        
                                    
                        | # reduces image noise and sharpness by averaging pixel values in a neighborhood.
gaussian_blur = cv2.GaussianBlur(img, (5, 5), 0) 
median_blur = cv2.medianBlur(img, 5)
bilateral_filter = cv2.bilateralFilter(img, 9, 75, 75)
 |  Image Filtering:
                        
                                    
                        | # modifies pixel values to improve image quality. Common methods include 2D convolution, 
#blurring, and edge detection.
img = cv2.imread('image.jpg')
kernel = np.ones((5,5),np.float32)/25
convolution = cv2.filter2D(img,-1,kernel)  #2D Convolution
blur = cv2.blur(img,(5,5))  # Image Blurring (averaging)
gaussian_blur = cv2.GaussianBlur(img, (5,5),0)   # Gaussian Blurring
median_blur = cv2.medianBlur(img, 5)   # Median Blurring
bilateral_filter = cv2.bilateralFilter(img, 9, 75, 75)  #Bilateral Filtering
 |  Edge Detection:
                        
                                    
                        | img = cv2.imread('image.jpg', 0)  # Read image in grayscale
# Canny edge detection
edges_canny = cv2.Canny(img, 100, 200)
# Sobel operator
edges_sobel = cv2.Sobel(img, cv2.CV_64F, 1, 0) + cv2.Sobel(img, cv2.CV_64F, 0, 1)
# Laplacian of Gaussian (LoG)
edges_log = cv2.Laplacian(cv2.GaussianBlur(img, (3, 3), 0), cv2.CV_64F)
cv2.imshow('Original', img)
cv2.imshow('Canny Edges', edges_canny)
cv2.imshow('Sobel Edges', edges_sobel)
cv2.imshow('LoG Edges', edges_log)
cv2.waitKey(0)
cv2.destroyAllWindows()
 |  Morphological Operations:
                        
                                    
                        | kernel = np.ones((5,5),np.uint8) #5x5 kernel
erosion = cv2.erode(img,kernel,iterations = 1) #Erosion
dilation = cv2.dilate(img,kernel,iterations = 1) #Dilation
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) #Opening
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel) #Closing
 |  Histogram Equalization:
                        
                                    
                        | # improves contrast by redistributing pixel values in an image. 
# It's useful for image preprocessing and analysis
eq_img = cv2.equalizeHist(img) #Apply histogram equalization
 |  Hough Transform:
                        
                                    
                        | # detect simple shapes such as lines, circles, and ellipses in an image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img,50,150,apertureSize = 3) #Canny edge detection
lines = cv2.HoughLines(edges,1,np.pi/180,200) #Hough transform
for rho,theta in lines[0]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int
 |  Thresholding:
                        
                                    
                        | # Thresholding separates object from background by setting pixel values above or below a 
# threshold. It creates a binary image. Helps with object detection, segmentation, and feature 
# extraction. Methods: global, adaptive, and Otsu's.
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) #Binary thresholding
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) #Inverse binary thresholding
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC) #Truncated thresholding
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO) #Threshold to zero
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV) #Inverse threshold to zero
adaptive_thresh = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) #Adaptive thresholding
 |  Image Pyramids:
                        
                                    
                        | img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
lower_reso = cv2.pyrDown(img) #Reduce resolution
higher_reso = cv2.pyrUp(img) #Increase resolution
 |  | 
            
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment