OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Remove unwanted areas texts from a binary image

  • Thread starter Thread starter kawsar
  • Start date Start date
K

kawsar

Guest
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

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.
<p>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,<a href="https://i.sstatic.net/rUIuG7zk.png" rel="nofollow noreferrer">this is the thing i want(remove all the text inside and outside the big wiring loop</a>.</p>
<p>For this preprocessing, i tried this code</p>
<pre><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()
</code></pre>
<p>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 image<a href="https://i.sstatic.net/OEGJkZ18.png" rel="nofollow noreferrer">Output for my code</a>
But i can't solve the issue with inside the wring loop. Such as <a href="https://i.sstatic.net/oMdn3PA4.png" rel="nofollow noreferrer">i want to remove the 2 ohm text inside the main wring loop</a>. As a beginner with OpenCV how can i achieve this.</p>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top