Random Forest for Image Classification Using OpenCV (original) (raw)

Last Updated : 7 Aug, 2025

Random Forest is a machine learning algorithm that uses the collective decision-making of multiple decision trees to make accurate predictions in both classification and regression tasks. OpenCV is an established open-source library for computer vision and machine learning and it provides tools for extracting and analyzing patterns from visual data. Integration of Random Forest with OpenCV aims to accurately classify images. This approach is helpful for analyzing complex medical images, such as those used for diagnosing diseases, because it makes the evaluation process more consistent and improves the confidence and accuracy of the results.

Step-by-Step Implementation

Used samples can be downloaded from GitHub using the link.

It can be extracted in the environment by the command:

!unzip /content/drawings.zip -d drawing

Let's see the implementation of a Random Forest to classify images using OpenCV,

Step 1: Importing the necessary libraries

from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score import os import matplotlib.pyplot as plt from skimage.feature import hog import random import cv2

`

Step 2: Define a HOG Feature Extractor

def extract_hog_features(image): hog_features = hog( image, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(2, 2), visualize=False ) return hog_features

`

Step 3: Load Images and Extract Features

def load_and_extract_features(directory): X, y = [], [] for label in os.listdir(directory): label_dir = os.path.join(directory, label) for filename in os.listdir(label_dir): image_path = os.path.join(label_dir, filename) img = cv2.imread(image_path) img_resized = cv2.resize(img, (128, 128)) img_gray = cv2.cvtColor(img_resized, cv2.COLOR_BGR2GRAY) hog_features = extract_hog_features(img_gray) X.append(hog_features) y.append(label) return X, y

`

Step 4: Train a Random Forest Classifer

spiral_train_X, spiral_train_y = load_and_extract_features( '/content/drawings/spiral/training') wave_train_X, wave_train_y = load_and_extract_features( '/content/drawings/wave/training')

spiral_rf_classifier = train_random_forest(spiral_train_X, spiral_train_y) wave_rf_classifier = train_random_forest(wave_train_X, wave_train_y)

`

Step 5: Model Testing and Evaluation

spiral_test_X, spiral_test_y = load_and_extract_features( '/content/drawings/spiral/testing') wave_test_X, wave_test_y = load_and_extract_features( '/content/drawings/wave/testing')

spiral_predictions = spiral_rf_classifier.predict(spiral_test_X) wave_predictions = wave_rf_classifier.predict(wave_test_X)

spiral_accuracy = accuracy_score(spiral_test_y, spiral_predictions) wave_accuracy = accuracy_score(wave_test_y, wave_predictions)

print("Spiral Classification Accuracy:", spiral_accuracy) print("Wave Classification Accuracy:", wave_accuracy)

`

**Output:

Spiral Classification Accuracy: 0.8
Wave Classification Accuracy: 0.6333333333333333

Step 6: Visualize the Confusion Matrix

from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix import matplotlib.pyplot as plt

cm = confusion_matrix(spiral_test_y, spiral_predictions) disp = ConfusionMatrixDisplay(confusion_matrix=cm) disp.plot(cmap=plt.cm.Blues) plt.show()

`

**Output:

confusion

Confusion Matrix

Step 7: Visualize the Dataset and Results

Python `

def display_images(directory, num_images=5): fig, axes = plt.subplots(2, num_images, figsize=(15, 5)) fig.suptitle(f"Images from {directory.split('/')[-1]}", fontsize=16)

for i, label in enumerate(os.listdir(directory)):
    label_dir = os.path.join(directory, label)
    image_files = os.listdir(label_dir)
    random.shuffle(image_files)
    for j in range(num_images):
        image_path = os.path.join(label_dir, image_files[j])
        img = cv2.imread(image_path)
        axes[i, j].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        axes[i, j].set_title(f"{label} Image {j+1}")
        axes[i, j].axis('off')
plt.tight_layout()
plt.show()

display_images('/content/drawings/spiral/training') display_images('/content/drawings/wave/training')

display_images('/content/drawings/spiral/testing') display_images('/content/drawings/wave/testing')

`

**Training:

**Testing:

Applications of Random Forest with OpenCV

Advantages of Using Random Forest

Disadvantages of Using Random Forest