I want to remove the texts and other unwanted small regions(+ – inside the circle) of a binary image for further processing, as shown in the first image,this is the thing i want(remove all the text inside and outside the big wiring loop.
For this preprocessing, i tried this code
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
from google.colab import drive
# Mount Google Drive
drive.mount('/content/drive')
def preprocess_image(image_path, target_size=(720, 1080)):
# Load the image
image = cv2.imread(image_path)
# Resize image
resized_image = cv2.resize(image, target_size)
# Apply Gaussian Blur to remove noise
blurred_image = cv2.GaussianBlur(resized_image, (9, 9), 0)
# Convert to grayscale
gray_image = cv2.cvtColor(blurred_image, cv2.COLOR_BGR2GRAY)
# Thresholding and contour detection to remove small regions
_, thresh = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
area = cv2.contourArea(contour)
if area < 100000: # Adjust this value to remove smaller regions
cv2.drawContours(thresh, [contour], -1, 0, -1)
# Convert to binary image
binary_image = cv2.threshold(thresh, 0, 255, cv2.THRESH_BINARY)[1]
return resized_image, binary_image
def main():
# Directory containing images
folder_path = '/content/drive/My Drive/project/dataset'
# Get all image paths in the directory
image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if f.endswith('.jpg')]
# Iterate through each image
for image_path in image_paths:
# Preprocess the image
original_image, processed_image = preprocess_image(image_path)
# Display original and processed images side by side
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].imshow(cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB))
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(processed_image, cmap='gray')
axes[1].set_title('Processed Image')
axes[1].axis('off')
plt.tight_layout()
plt.show()
if __name__ == "__main__":
main()
this code is for 50 images inside my drive. Its work very well for the outside texts of the wiring loop as shown in the below imageOutput for my code
But i can’t solve the issue with inside the wring loop. Such as i want to remove the 2 ohm text inside the main wring loop. As a beginner with OpenCV how can i achieve this.
kawsar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1