Monday, April 10, 2023

image processing using python and opencv

  1. Reading and displaying an image using OpenCV:
python
import cv2 # Load an image img = cv2.imread('image.jpg') # Display the image cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
  1. Resizing an image using OpenCV:
python
import cv2 # Load an image img = cv2.imread('image.jpg') # Resize the image resized = cv2.resize(img, (500, 500)) # Display the resized image cv2.imshow('resized', resized) cv2.waitKey(0) cv2.destroyAllWindows()
  1. Converting an image to grayscale using OpenCV:
python
import cv2 # Load an image img = cv2.imread('image.jpg') # Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Display the grayscale image cv2.imshow('gray', gray) cv2.waitKey(0) cv2.destroyAllWindows()
  1. Applying a Gaussian blur to an image using OpenCV:
python
import cv2 # Load an image img = cv2.imread('image.jpg') # Apply a Gaussian blur to the image blurred = cv2.GaussianBlur(img, (5, 5), 0) # Display the blurred image cv2.imshow('blurred', blurred) cv2.waitKey(0) cv2.destroyAllWindows()
  1. Applying a Sobel edge detection filter to an image using OpenCV:
python
import cv2 import numpy as np # Load an image img = cv2.imread('image.jpg') # Convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply a Sobel filter to the image sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) sobel = np.sqrt(sobelx**2 + sobely**2) # Display the edge-detected image cv2.imshow('edge-detected', sobel) cv2.waitKey(0) cv2.destroyAllWindows()

Note that these are just a few examples of what you can do with image processing in Python. There are many other techniques and libraries available for processing images, depending on your specific needs and goals

No comments: