import cv2 import numpy as np import matplotlib.pyplot as plt # Internet ki zaroorat nahi, hum khud aik dummy image create kar letay hain test def create_test_image(): # Aik color pattern create karein (Chessboard jaisa) img = np zeros((100, 100, 3), dtype = np uint8) img[::20, :] = [255, 0, 0] # Blue lines img[:, ::20] = [0, 255, 0] # Green lines cv2 circle(img, (50, 50), 30, (0, 0, 255), - 1) # Red circle return img def resize_image(image, target_size): print(f"Original image shape: {image shape}") resized_nn = cv2 resize(image, target_size, interpolation = cv2 INTER_NEAREST) resized_bilinear = cv2 resize(image, target_size, interpolation = cv2 INTER_LI resized_bicubic = cv2 resize(image, target_size, interpolation = cv2 INTER_CUB return resized_nn, resized_bilinear, resized_bicubic # Dummy image generate karein image = create_test_image() if image is None : print("Image create nahi ho saki.") else : print("Test image successfully created!") # Image ko bara (upscale) kar k dekhte hain taake interpolation ka farq naza target_size = (800, 600) nn, bilinear, bicubic = resize_image(image, target_size) # Display the results plt figure(figsize = (15, 5)) plt subplot(131); plt imshow(cv2 cvtColor(nn, cv2 COLOR_BGR2RGB)) plt title('Nearest Neighbor (Pixelated)'); plt axis('off') plt subplot(132); plt imshow(cv2 cvtColor(bilinear, cv2 COLOR_BGR2RGB)) plt title('Bilinear (Smooth)'); plt axis('off') plt subplot(133); plt imshow(cv2 cvtColor(bicubic, cv2 COLOR_BGR2RGB)) plt title('Bicubic (Sharper)'); plt axis('off') plt tight_layout() plt show() print("Script completed successfully!") Test image successfully created! Original image shape: (100, 100, 3) In [4]: Script completed successfully! import cv2 import numpy as np import matplotlib.pyplot as plt def create_low_contrast_image(): # Aik dark aur low-contrast image banatay hain taake normalization nazar aay img = np zeros((300, 300, 3), dtype = np uint8) # Background ko thora gray rakhte hain img[:, :] = [50, 55, 60] # Aik circle aur rectangle add karte hain light colors mein cv2 circle(img, (150, 150), 80, (100, 110, 120), - 1) cv2 rectangle(img, (50, 50), (100, 100), (80, 90, 100), - 1) # Thora noise add karte hain noise = np random randint(0, 20, (300, 300, 3), dtype = 'uint8') img = cv2 add(img, noise) return img def normalize_image(image): print("Performing image normalization...") # 1. Min-Max Scaling (Pure image stretch) min_max = cv2 normalize(image, None , 0, 255, cv2 NORM_MINMAX) # 2. Z-score Normalization (Standardization) z_score = np zeros_like(image, dtype = np float32) for i in range(3): # for each channel channel = image[:,:,i] astype(np float32) mean = np mean(channel) std = np std(channel) z_score[:,:,i] = (channel - mean) / (std + 1e-8) # Visualization ke liye Z-score ko wapas 0-255 range mein lana parta hai z_score_viz = cv2 normalize(z_score, None , 0, 255, cv2 NORM_MINMAX) astype(n # 3. Histogram Equalization (Grayscale par hota hai contrast barhane ke liye gray = cv2 cvtColor(image, cv2 COLOR_BGR2GRAY) hist_eq = cv2 equalizeHist(gray) print("Normalization complete.") return min_max, z_score_viz, hist_eq # Image create karein (Kaggle path ki zaroorat nahi) image = create_low_contrast_image() print("Successfully generated test image!") min_max, z_score, hist_eq = normalize_image(image) # Display the results In [5]: plt figure(figsize = (20, 6)) plt subplot(141) plt imshow(cv2 cvtColor(image, cv2 COLOR_BGR2RGB)) plt title('Original (Dark/Low Contrast)') plt axis('off') plt subplot(142) plt imshow(cv2 cvtColor(min_max, cv2 COLOR_BGR2RGB)) plt title('Min-Max Scaling') plt axis('off') plt subplot(143) plt imshow(cv2 cvtColor(z_score, cv2 COLOR_BGR2RGB)) plt title('Z-score Normalization') plt axis('off') plt subplot(144) plt imshow(hist_eq, cmap = 'gray') plt title('Histogram Equalization') plt axis('off') plt tight_layout() plt show() print("Script completed successfully!") Successfully generated test image! Performing image normalization... Normalization complete. Script completed successfully! import cv2 import numpy as np import matplotlib.pyplot as plt def create_shape_image(): # Aik white background wali image banatay hain jis par shapes hoon img = np full((300, 300, 3), 255, dtype = np uint8) # Blue Rectangle cv2 rectangle(img, (50, 50), (150, 150), (255, 0, 0), - 1) # Red Circle (is se rotation aur flip ka pata chale ga) cv2 circle(img, (200, 200), 40, (0, 0, 255), - 1) # Green Triangle (direction check karne ke liye) points = np array([[250, 50], [200, 100], [300, 100]]) cv2 fillPoly(img, [points], (0, 255, 0)) return img def augment_image(image): print("Performing image augmentation...") In [7]: # 1. Rotation (45 degrees) rows, cols = image shape[:2] M = cv2 getRotationMatrix2D((cols / 2, rows / 2), 45, 1) rotated = cv2 warpAffine(image, M, (cols, rows), borderValue = (255,255,255)) # 2. Flipping (Horizontal) flipped = cv2 flip(image, 1) # 3. Brightness Adjustment (Alpha 1.5 barha raha hai brightness) bright = cv2 convertScaleAbs(image, alpha = 1.2, beta = 30) # 4. Add Noise (Gaussian Noise) noise = np random normal(0, 25, image shape) astype(np int16) # int16 use ki noisy = np clip(image astype(np int16) + noise, 0, 255) astype(np uint8) print("Augmentation complete.") return rotated, flipped, bright, noisy # Image generate karein image = create_shape_image() print("Successfully generated test image!") rotated, flipped, bright, noisy = augment_image(image) # Display the results plt figure(figsize = (22, 5)) plt subplot(151) plt imshow(cv2 cvtColor(image, cv2 COLOR_BGR2RGB)) plt title('Original') plt axis('off') plt subplot(152) plt imshow(cv2 cvtColor(rotated, cv2 COLOR_BGR2RGB)) plt title('Rotated (45°)') plt axis('off') plt subplot(153) plt imshow(cv2 cvtColor(flipped, cv2 COLOR_BGR2RGB)) plt title('Flipped (Horizontal)') plt axis('off') plt subplot(154) plt imshow(cv2 cvtColor(bright, cv2 COLOR_BGR2RGB)) plt title('Brightness Adjusted') plt axis('off') plt subplot(155) plt imshow(cv2 cvtColor(noisy, cv2 COLOR_BGR2RGB)) plt title('Noisy (Gaussian)') plt axis('off') plt tight_layout() plt show() print("Script completed successfully!") Successfully generated test image! Performing image augmentation... Augmentation complete. Script completed successfully! import cv2 import numpy as np import matplotlib.pyplot as plt def create_noisy_test_image(): # Aik clean image banatay hain pehle img = np zeros((300, 300, 3), dtype = np uint8) cv2 rectangle(img, (50, 50), (250, 250), (200, 200, 0), - 1) # Yellow Box cv2 putText(img, "Denoise Test", (60, 160), cv2 FONT_HERSHEY_SIMPLEX, 1, (25 # 1. "Salt and Pepper" noise add karna (White/Black dots) probs = 0.02 rnd = np random rand(300, 300) img[rnd < (probs / 2)] = 0 # Pepper img[rnd > 1 - (probs / 2)] = 255 # Salt # 2. Thora sa Gaussian noise bhi add kar letay hain noise = np random normal(0, 15, (300, 300, 3)) astype(np int16) img = np clip(img astype(np int16) + noise, 0, 255) astype(np uint8) return img def denoise_image(image): print("Performing image denoising...") # Gaussian: General smoothing ke liye gaussian = cv2 GaussianBlur(image, (5, 5), 0) # Median: Salt & Pepper noise ke liye best hai median = cv2 medianBlur(image, 5) # Bilateral: Noise hatata hai magar edges ko sharp rakhta hai bilateral = cv2 bilateralFilter(image, 9, 75, 75) print("Denoising complete.") return gaussian, median, bilateral # Image generate karein image = create_noisy_test_image() print("Successfully generated noisy test image!") gaussian, median, bilateral = denoise_image(image) # Results display karein plt figure(figsize = (20, 6)) plt subplot(141) plt imshow(cv2 cvtColor(image, cv2 COLOR_BGR2RGB)) plt title('Original (With Noise)') plt axis('off') In [8]: plt subplot(142) plt imshow(cv2 cvtColor(gaussian, cv2 COLOR_BGR2RGB)) plt title('Gaussian Blur (Smoothed)') plt axis('off') plt subplot(143) plt imshow(cv2 cvtColor(median, cv2 COLOR_BGR2RGB)) plt title('Median Blur (Salt & Pepper gone)') plt axis('off') plt subplot(144) plt imshow(cv2 cvtColor(bilateral, cv2 COLOR_BGR2RGB)) plt title('Bilateral Filter (Edges preserved)') plt axis('off') plt tight_layout() plt show() print("Script completed successfully!") Successfully generated noisy test image! Performing image denoising... Denoising complete. Script completed successfully! import cv2 import numpy as np import matplotlib.pyplot as plt def create_edge_test_image(): # White background wali image img = np full((300, 300, 3), 255, dtype = np uint8) # Aik Circle aur Rectangle banatay hain taake edges milain cv2 circle(img, (150, 150), 70, (0, 0, 0), 3) # Black circle cv2 rectangle(img, (50, 50), (250, 250), (0, 0, 0), 2) # Black square # Thori diagonal lines cv2 line(img, (0, 0), (300, 300), (0, 0, 0), 2) return img def detect_edges(image): print("Performing edge detection...") gray = cv2 cvtColor(image, cv2 COLOR_BGR2GRAY) # 1. Sobel (Gradient based - thori "thick" edges deta hai) 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) # Normalization taake values 0-255 mein rahein sobel = np uint8(sobel / (sobel max() + 1e-8) * 255) In [9]: # 2. Canny (Clean aur thin edges ke liye best hai) canny = cv2 Canny(gray, 100, 200) # 3. Laplacian of Gaussian (LoG) blur = cv2 GaussianBlur(gray, (3, 3), 0) log = cv2 Laplacian(blur, cv2 CV_64F) log = np uint8(np absolute(log)) print("Edge detection complete.") return sobel, canny, log # Image generate karein image = create_edge_test_image() print("Successfully generated edge test image!") sobel, canny, log = detect_edges(image) # Display the results plt figure(figsize = (20, 6)) plt subplot(141) plt imshow(cv2 cvtColor(image, cv2 COLOR_BGR2RGB)) plt title('Original (Test Image)') plt axis('off') plt subplot(142) plt imshow(sobel, cmap = 'gray') plt title('Sobel (Gradients)') plt axis('off') plt subplot(143) plt imshow(canny, cmap = 'gray') plt title('Canny (Sharp Edges)') plt axis('off') plt subplot(144) plt imshow(log, cmap = 'gray') plt title('Laplacian of Gaussian') plt axis('off') plt tight_layout() plt show() print("Script completed successfully!") Successfully generated edge test image! Performing edge detection... Edge detection complete. Script completed successfully! import cv2 import numpy as np import matplotlib.pyplot as plt def create_gradient_test_image(): # 1. Aik plain white image aur us par text/shapes img = np full((300, 300), 255, dtype = np uint8) cv2 putText(img, "TEST BINARY", (30, 150), cv2 FONT_HERSHEY_SIMPLEX, 1.2, (0 cv2 circle(img, (240, 60), 30, (50, 50, 50), - 1) # 2. Aik artificial 'shadow' (gradient) add karte hain # Is se image aik taraf se dark aur aik taraf se light ho jaye gi gradient = np tile(np linspace(0, 255, 300, dtype = np uint8), (300, 1)) img = cv2 addWeighted(img, 0.5, gradient, 0.5, 0) # Convert to 3-channel BGR to match your script's structure return cv2 cvtColor(img, cv2 COLOR_GRAY2BGR) def binarize_image(image): print("Performing image binarization...") gray = cv2 cvtColor(image, cv2 COLOR_BGR2GRAY) # Global thresholding (Otsu's method) # Ye puri image ke liye aik hi threshold value nikalta hai _, global_thresh = cv2 threshold(gray, 0, 255, cv2 THRESH_BINARY + cv2 THRES # Adaptive thresholding # Ye image ke chote chote hisson (blocks) ke liye alag threshold nikalta hai adaptive_thresh = cv2 adaptiveThreshold(gray, 255, cv2 ADAPTIVE_THRESH_GAUSS cv2 THRESH_BINARY, 11, 2) print("Binarization complete.") return global_thresh, adaptive_thresh # Image generate karein image = create_gradient_test_image() print("Successfully generated test image with uneven lighting!") global_thresh, adaptive_thresh = binarize_image(image) # Display the results plt figure(figsize = (15, 6)) plt subplot(131) plt imshow(cv2 cvtColor(image, cv2 COLOR_BGR2RGB)) plt title('Original (Uneven Lighting)') plt axis('off') plt subplot(132) plt imshow(global_thresh, cmap = 'gray') plt title('Global (Otsu)') plt axis('off') plt subplot(133) plt imshow(adaptive_thresh, cmap = 'gray') plt title('Adaptive Thresholding') plt axis('off') In [10]: plt tight_layout() plt show() print("Script completed successfully!") Successfully generated test image with uneven lighting! Performing image binarization... Binarization complete. Script completed successfully! In [ ]: