banner
sanford

sanford

Have you seen the characters flashing in the neural circuit?
github
twitter

01 Comic Explanation - The Division of Image Frames

Find the Best Contour Patterns#

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('data/test02.png',0)
ret,thresh1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
ret,thresh2 = cv.threshold(img,127,255,cv.THRESH_BINARY_INV)
ret,thresh3 = cv.threshold(img,127,255,cv.THRESH_TRUNC)
ret,thresh4 = cv.threshold(img,127,255,cv.THRESH_TOZERO)
ret,thresh5 = cv.threshold(img,127,255,cv.THRESH_TOZERO_INV)
titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]
for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('data/test01.png',0)
img = cv.medianBlur(img,5)
ret,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY,11,2)
titles = ['Original Image', 'Global Thresholding (v = 127)',
            'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding']
images = [img, th1, th2, th3]
for i in range(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()

image 1

Contour Detection#

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Read the image
image = cv2.imread('data/test01.png')

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply binary thresholding
_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)

# Perform morphological operations to remove noise
kernel = np.ones((5, 5), np.uint8)
morph = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)

# Find contours
contours, _ = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Create a list to save segmented text boxes
text_boxes = []

# Iterate through all contours
for contour in contours:
    # Calculate the bounding box of the contour
    x, y, w, h = cv2.boundingRect(contour)
    aspect_ratio = w / float(h)
    print(f'x={x}, y={y}, w={w}, h={h}')

    # Filter out small contours
    if aspect_ratio > 0.1:
        # Extract the text box region
        text_box = image[y:y+h, x:x+w]
        text_boxes.append(text_box)
        
        # Draw the bounding box on the original image
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Save the segmented text boxes
text_boxes.reverse()
for i, box in enumerate(text_boxes):
    # cv2.imwrite(f'text_box_{i}.png', box)
    plt.subplot(len(text_boxes),2,i+1),plt.imshow(box,'gray')
    plt.title(f'text_box_{i}.png')
    plt.xticks([]),plt.yticks([])
plt.show()

![image.png](
image 3
)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.