-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_image_rgb.py
More file actions
121 lines (97 loc) · 4.25 KB
/
example_image_rgb.py
File metadata and controls
121 lines (97 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Demonstration script for Local Binary Patterns (LBP).
This script loads an image and computes its LBP texture features. LBP is a simple
yet powerful texture descriptor that works by comparing each pixel with its
surrounding neighbors. The result highlights local texture patterns.
The script performs two main operations:
1. Calculates the LBP on a grayscale version of the image.
2. Efficiently calculates the LBP for all three channels (R, G, B) of the
color image in a single operation.
"""
from lib.lbplib import LbpPytorch
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
def process_image_lbp(image_path):
"""
Loads an image, computes its LBP for both grayscale and RGB versions,
and displays the results using the LbpPytorch module.
"""
try:
# Read the image using PIL
img_pil = Image.open(image_path)
except FileNotFoundError:
print(f"Error: Image not found at {image_path}")
return
# Create an instance of LbpPytorch module with NCHW format
# Use CPU for image processing (can change to CUDA if needed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
lbp_module = LbpPytorch(input_format='NCHW').to(device)
lbp_module.eval() # Set to evaluation mode
# --- 1. Grayscale LBP Processing ---
# Convert to grayscale and then to a NumPy array
img_gray_np = np.array(img_pil.convert('L'))
# Convert to PyTorch tensor with proper dimensions
# Shape: (H, W) -> (1, 1, H, W) [batch, channel, height, width]
img_gray_torch = torch.from_numpy(img_gray_np).unsqueeze(0).unsqueeze(0).to(device)
# Process with LbpPytorch module
with torch.no_grad():
lbp_gray_tensor = lbp_module(img_gray_torch)
# Convert back to NumPy for display
# Shape: (1, 1, H, W) -> (H, W)
lbp_gray = lbp_gray_tensor.squeeze().cpu().numpy()
# Convert from normalized [0, 1] to [0, 255] for display
lbp_gray = (lbp_gray * 255).astype(np.uint8)
# --- 2. Efficient RGB LBP Processing ---
# Convert the original RGB image to a NumPy array
img_rgb_np = np.array(img_pil)
# Prepare the tensor for PyTorch
# NumPy format is (H, W, C), but PyTorch expects (B, C, H, W)
# a. Transpose from (H, W, C) to (C, H, W)
img_chw = img_rgb_np.transpose((2, 0, 1))
# b. Add a batch dimension to get (1, C, H, W)
img_batch = np.expand_dims(img_chw, axis=0)
# Convert to a PyTorch tensor
img_rgb_torch = torch.from_numpy(img_batch.astype(np.uint8)).to(device)
# Calculate LBP on all 3 channels in a single, efficient pass
with torch.no_grad():
lbp_rgb_torch = lbp_module(img_rgb_torch)
# Convert the result back for display
# a. Move to CPU and NumPy, remove the batch dimension -> (C, H, W)
lbp_rgb_np = lbp_rgb_torch.cpu().numpy().squeeze()
# b. Transpose back to (H, W, C) for plotting
lbp_rgb = lbp_rgb_np.transpose((1, 2, 0))
# Convert from normalized [0, 1] to [0, 255] for display
lbp_rgb = (lbp_rgb * 255).astype(np.uint8)
# --- 3. Show Input and Output Images ---
# Use subplots for a clean, side-by-side comparison
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
axes[0].imshow(img_rgb_np)
axes[0].set_title('Original RGB Image')
axes[0].axis('off')
axes[1].imshow(lbp_gray, cmap='gray')
axes[1].set_title('Grayscale LBP')
axes[1].axis('off')
axes[2].imshow(lbp_rgb)
axes[2].set_title('RGB LBP (Channel-wise)')
axes[2].axis('off')
plt.tight_layout()
plt.show()
# --- 4. Print Additional Information ---
print("-" * 50)
print("Image Processing Information:")
print(f"Device used: {device}")
print(f"Original image shape: {img_rgb_np.shape}")
print(f"Grayscale LBP shape: {lbp_gray.shape}")
print(f"RGB LBP shape: {lbp_rgb.shape}")
print(f"Grayscale LBP value range: [{lbp_gray.min()}, {lbp_gray.max()}]")
print(f"RGB LBP value range: [{lbp_rgb.min()}, {lbp_rgb.max()}]")
print("-" * 50)
if __name__ == '__main__':
# Define the path to your test image
IMAGE_PATH = 'img/ILSVRC2012_val_00000328.JPEG'
process_image_lbp(IMAGE_PATH)