├── README.md └── Image processing using open cv.py /README.md: -------------------------------------------------------------------------------- 1 | # image-processing -------------------------------------------------------------------------------- /Image processing using open cv.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[9]: 5 | 6 | 7 | get_ipython().system('pip install opencv-python numpy matplotlib') 8 | 9 | 10 | # In[12]: 11 | 12 | 13 | import cv2 14 | import numpy as np 15 | import matplotlib.pyplot as plt 16 | 17 | 18 | # In[13]: 19 | 20 | 21 | def load_image(image_path): 22 | return cv2.imread(image_path, cv2.IMREAD_COLOR) 23 | 24 | 25 | # In[14]: 26 | 27 | 28 | def convert_to_grayscale(image): 29 | return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 30 | 31 | 32 | # In[15]: 33 | 34 | 35 | def rotate_image(image, angle): 36 | (h, w) = image.shape[:2] 37 | center = (w // 2, h // 2) 38 | matrix = cv2.getRotationMatrix2D(center, angle, 1.0) 39 | return cv2.warpAffine(image, matrix, (w, h)) 40 | 41 | 42 | # In[16]: 43 | 44 | 45 | def crop_border(image, border_thickness): 46 | """ 47 | Crop the border of the image with the specified thickness from all sides. 48 | 49 | Parameters: 50 | image (numpy.ndarray): The input image. 51 | border_thickness (int): The thickness of the border to crop from all sides. 52 | 53 | Returns: 54 | numpy.ndarray: The image with the border removed. 55 | """ 56 | if border_thickness >= min(image.shape[:2]) // 2: 57 | raise ValueError("Border thickness is too large for the given image dimensions.") 58 | 59 | (h, w) = image.shape[:2] 60 | start_row = border_thickness 61 | start_col = border_thickness 62 | end_row = h - border_thickness 63 | end_col = w - border_thickness 64 | return image[start_row:end_row, start_col:end_col] 65 | 66 | 67 | # In[30]: 68 | 69 | 70 | def blur_image(image, kernel_size=(1, 1)): 71 | return cv2.GaussianBlur(image, kernel_size, 0) 72 | 73 | 74 | # In[18]: 75 | 76 | 77 | def get_image_info(image): 78 | dimensions = image.shape 79 | number_of_pixels = image.size 80 | return dimensions, number_of_pixels 81 | 82 | 83 | # In[31]: 84 | 85 | 86 | image_path = "C:/Users/muthu/OneDrive/Desktop/nehru.jpg" 87 | 88 | # Load the image 89 | image = load_image(image_path) 90 | 91 | # Convert to grayscale 92 | gray_image = convert_to_grayscale(image) 93 | 94 | # Rotate the image by 45 degrees 95 | rotated_image = rotate_image(gray_image, 45) 96 | 97 | # Crop the image (region from (50, 50) to (150, 150)) 98 | cropped_image = crop_image(rotated_image, 10, 10, 150, 150) 99 | 100 | # Blur the image 101 | blurred_image = blur_image(cropped_image) 102 | 103 | # Get image dimensions and number of pixels 104 | dimensions, number_of_pixels = get_image_info(blurred_image) 105 | 106 | # Display the original and processed images 107 | plt.figure(figsize=(12, 6)) 108 | 109 | plt.subplot(2, 3, 1) 110 | plt.title('Original Image') 111 | plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) 112 | 113 | plt.subplot(2, 3, 2) 114 | plt.title('Grayscale Image') 115 | plt.imshow(gray_image, cmap='gray') 116 | 117 | plt.subplot(2, 3, 3) 118 | plt.title('Rotated Image') 119 | plt.imshow(rotated_image, cmap='gray') 120 | 121 | plt.subplot(2, 3, 4) 122 | plt.title('Cropped Image') 123 | plt.imshow(cropped_image, cmap='gray') 124 | 125 | plt.subplot(2, 3, 5) 126 | plt.title('Blurred Image') 127 | plt.imshow(blurred_image, cmap='gray') 128 | 129 | plt.show() 130 | 131 | print(f'Dimensions of the image: {dimensions}') 132 | print(f'Number of pixels: {number_of_pixels}') 133 | 134 | --------------------------------------------------------------------------------