all after vs. before after
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
# Correcting the image paths
image_before_path = './all_before.png'
image_after_path = './all_after.png'
# Check if files exist
if not os.path.exists(image_before_path):
raise FileNotFoundError(f"The file {image_before_path} does not exist.")
if not os.path.exists(image_after_path):
raise FileNotFoundError(f"The file {image_after_path} does not exist.")
# Load the images
image_before = cv2.imread(image_before_path, cv2.IMREAD_GRAYSCALE)
image_after = cv2.imread(image_after_path, cv2.IMREAD_GRAYSCALE)
# Ensure images are the same size
if image_before.shape != image_after.shape:
print("Images are not the same size. Resizing...")
image_after = cv2.resize(image_after, (image_before.shape[1], image_before.shape[0]))
# Compute the absolute difference
abs_diff = cv2.absdiff(image_before, image_after)
# Threshold the difference to highlight erased regions
_, thresholded = cv2.threshold(abs_diff, 30, 255, cv2.THRESH_BINARY)
# Divide the image into 5 horizontal regions
height, width = thresholded.shape
line_height = height // 5
erased_pixels_per_line = []
for i in range(5):
# Extract the region corresponding to the current line
line_region = thresholded[i * line_height:(i + 1) * line_height, :]
# Count the number of erased pixels in the region
erased_pixels = np.sum(line_region > 0)
erased_pixels_per_line.append(erased_pixels)
# Identify the line with the most erased pixels
most_erased_line = np.argmax(erased_pixels_per_line) + 1
# Display results
for i, pixels in enumerate(erased_pixels_per_line):
print(f"Line {i + 1}: {pixels} erased pixels")
print(f"The line with the most erasures is Line {most_erased_line}.")
# Plot the erased regions
plt.figure(figsize=(10, 8))
plt.title("Erased Regions")
plt.imshow(thresholded, cmap='gray')
plt.axis('off')
plt.show()
Run to view results
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
# Correcting the image path
image_path = './after_dust.png'
# Check if the file exists
if not os.path.exists(image_path):
raise FileNotFoundError(f"The file {image_path} does not exist.")
# Load the image
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
raise FileNotFoundError(f"The image at path {image_path} could not be loaded. Please check the format or file corruption.")
# Preprocessing to enhance dust detection
blurred = cv2.GaussianBlur(image, (5, 5), 0)
# Apply adaptive thresholding
thresholded = cv2.adaptiveThreshold(
blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2
)
# Focus on the bottom three lines by dividing the image into 5 regions
height, width = thresholded.shape
line_height = height // 5
dust_count_per_line = []
for i in range(5):
# Extract the region corresponding to the current line
line_region = thresholded[i * line_height:(i + 1) * line_height, :]
# Count the number of dust particles (non-zero pixels)
dust_pixels = np.sum(line_region > 0)
dust_count_per_line.append(dust_pixels)
# Identify the lines with the most dust
most_dust_lines = np.argsort(dust_count_per_line)[-3:] # Bottom 3 lines with the most dust
# Display results
for i, dust in enumerate(dust_count_per_line):
print(f"Line {i + 1}: {dust} dust particles")
print(f"The lines with the most dust are: {', '.join(['Line ' + str(i + 1) for i in most_dust_lines[::-1]])}")
# Plot the original image and the thresholded image
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
plt.title("Original Image")
plt.imshow(image, cmap='gray')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title("Thresholded (Dust Highlighted)")
plt.imshow(thresholded, cmap='gray')
plt.axis('off')
plt.tight_layout()
plt.show()
Run to view results