.
model nmos nmos level=1 vto=-1
import numpy as np
# Load image
img = cv2.imread('image.jpg')
# Resize to 128x128
resized = cv2.resize(img, (128, 128))
# Convert to grayscale
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
# Normalize to [0, 1]
normalized = gray / 255.0
print(normalized.shape)
✅ 2. Histogram Equalization
# Histogram equalization (for contrast enhancement)
equalized = cv2.equalizeHist(gray)
✅ 3. Gaussian Blur
# Apply Gaussian blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
✅ 4. Thresholding
# Binary thresholding
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
✅ 5. Edge Detection (Canny)
# Edge detection
edges = cv2.Canny(gray, 100, 200)
✅ 6. Image Augmentation (Flip, Rotate, Zoom)
# Horizontal flip
flipped = cv2.flip(img, 1)
# Rotate 90 degrees
rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
# Zoom (crop center and resize)
h, w = img.shape[:2]
zoom = img[h//4:3*h//4, w//4:3*w//4]
zoom = cv2.resize(zoom, (w, h))
📦 Using keras.preprocessing
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    zoom_range=0.2,
    horizontal_flip=True
)
# Apply to a single image
img = cv2.imread('image.jpg')
img = cv2.resize(img, (128, 128))
img = np.expand_dims(img, axis=0)
aug_iter = datagen.flow(img, batch_size=1)
aug_image = next(aug_iter)[0]
Great! Here's a complete image preprocessing pipeline function in Python using OpenCV and
NumPy. This version is general-purpose and works well for datasets like brain MRI, digits,
CIFAR-10, or any image classification task.
✅ Complete Image Preprocessing Function
import cv2
import numpy as np
def preprocess_image(img_path, target_size=(128, 128), apply_augment=False):
    """
    Preprocess an input image:
    1. Resize
    2. Grayscale
    3. Normalize
    4. Histogram equalization
    5. (Optional) Data augmentation
    Parameters:
    - img_path: str, path to the image
    - target_size: tuple, resize dimensions (width, height)
    - apply_augment: bool, whether to apply augmentation
    Returns:
     - Preprocessed image as NumPy array
     """
     # Load image
     img = cv2.imread(img_path)
     if img is None:
         raise FileNotFoundError(f"Image not found: {img_path}")
     # Resize
     img = cv2.resize(img, target_size)
     # Grayscale
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     # Histogram equalization
     gray = cv2.equalizeHist(gray)
     # Normalize to [0, 1]
     norm = gray / 255.0
     # Augmentation (flip and rotate)
     if apply_augment:
         norm = cv2.flip(norm, 1) # Horizontal flip
         norm = cv2.rotate(norm, cv2.ROTATE_90_CLOCKWISE)         # Rotate 90 degrees
     return norm
🔁 Usage Example
# Process a single image
image_path = "sample.jpg"
processed = preprocess_image(image_path)
# For model input
processed = processed.reshape(1, 128, 128, 1)          # Add batch and channel
dimensions
🧠 For Use in Model Training
If you are working with multiple images in a folder:
import os
def load_dataset(folder):
    data = []
    labels = []
    for label in os.listdir(folder):
        class_folder = os.path.join(folder, label)
        for img_file in os.listdir(class_folder):
            img_path = os.path.join(class_folder, img_file)
            try:
                 img = preprocess_image(img_path)
                   data.append(img)
                   labels.append(label)
               except:
                   continue
       return np.array(data), np.array(labels)
Perfect! Here's a full example of how to build a machine learning image classification
pipeline using:
        The preprocessing function I shared earlier
        Feature extraction by flattening the preprocessed image (simple and effective for ML
         classifiers)
        Training SVM, Random Forest, and XGBoost classifiers
        Evaluating accuracy
📦 Complete ML Image Classification Pipeline (SVM, RF,
XGBoost)
import os
import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score
# Preprocessing function (same as before)
def preprocess_image(img_path, target_size=(64, 64)):
    img = cv2.imread(img_path)
    if img is None:
        raise FileNotFoundError(f"Image not found: {img_path}")
    img = cv2.resize(img, target_size)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)
    norm = gray / 255.0
    return norm
# Load dataset
def load_dataset(folder):
    data = []
    labels = []
    label_map = {} # map string label to integer
    label_idx = 0
       for label in sorted(os.listdir(folder)):
           class_folder = os.path.join(folder, label)
           if not os.path.isdir(class_folder):
               continue
        if label not in label_map:
            label_map[label] = label_idx
            label_idx += 1
        for img_file in os.listdir(class_folder):
            img_path = os.path.join(class_folder, img_file)
            try:
                 img = preprocess_image(img_path)
                 # Flatten image for ML input
                 img_flat = img.flatten()
                 data.append(img_flat)
                 labels.append(label_map[label])
            except Exception as e:
                 print(f"Skipping {img_path}: {e}")
    return np.array(data), np.array(labels), label_map
# Replace with your dataset folder path
dataset_folder = 'path_to_your_image_dataset'
# Load images and labels
X, y, label_map = load_dataset(dataset_folder)
print(f"Loaded {len(X)} samples from {len(label_map)} classes.")
# Split train/test
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42, stratify=y)
# Initialize classifiers
svm = SVC(kernel='linear', probability=True)
rf = RandomForestClassifier(n_estimators=100, random_state=42)
xgb = XGBClassifier(use_label_encoder=False, eval_metric='mlogloss',
random_state=42)
# Train classifiers
print("Training SVM...")
svm.fit(X_train, y_train)
print("Training Random Forest...")
rf.fit(X_train, y_train)
print("Training XGBoost...")
xgb.fit(X_train, y_train)
# Predict and evaluate
def evaluate_model(model, X_test, y_test, name):
    y_pred = model.predict(X_test)
    acc = accuracy_score(y_test, y_pred)
    print(f"{name} Accuracy: {acc:.4f}")
evaluate_model(svm, X_test, y_test, "SVM")
evaluate_model(rf, X_test, y_test, "Random Forest")
evaluate_model(xgb, X_test, y_test, "XGBoost")
📋 How to Use:
   1. Arrange your images in folders like:
dataset_folder/
   ├── class1/
   │    ├── img1.jpg
   │    ├── img2.jpg
   ├── class2/
   │    ├── img1.jpg
   │    ├── img2.jpg
   ...
   2. Change 'path_to_your_image_dataset' in the code to your dataset folder path.
   3. Run the script.
🧠 Notes:
      Images are resized to 64x64 for faster training (adjustable).
      Simple grayscale + histogram equalization + normalization preprocessing.
      Images flattened into 1D vectors for classic ML models.