Issue
i have this page and i am trying to make a cropped image with the top right corner i used this code for the top left corner with black pixel inside but could not edit it to work for top right.it makes a grid .if the square inside has black pixels is number 1 else is 0. it find the top left square with black pixels and crop around it.
input
output
I am trying to auto-crop this page number area
I think the script search from like the blue arrow and i want to work like the red
import cv2
import numpy as np
# Load the cropped image in grayscale
image = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE)
# Get the height and width of the image
h, w = image.shape
# Define the number of rows and columns of the grid
rows = 30
cols = 40
# Calculate the size of each square in pixels
square_h = h // rows
square_w = w // cols
# Create an empty array to store the grid values
grid = np.zeros((rows, cols), dtype=int)
# Loop over the rows and columns of the grid
for i in range(rows):
for j in range(cols):
# Get the coordinates of the top left corner of the square
x = j * square_w
y = i * square_h
# Get the sub-image corresponding to the square
square = image[y:y+square_h, x:x+square_w]
# Check if the square contains any black pixels
if np.any(square < 255):
# Set the grid value to 1
grid[i, j] = 1
# Find the top left corner with a 1 square
# Use np.argwhere to get the indices of the nonzero elements
# Use np.min to get the minimum row and column index
top_left = np.min(np.argwhere(grid == 1), axis=0)
# Get the row and column index of the top left corner
row, col = top_left
# Define the number of squares to crop around the corner
crop_size = 3
# Get the coordinates of the top left corner of the cropped region
x = col * square_w
y = row * square_h
# Get the sub-image corresponding to the cropped region
cropped_image = image[y:y+crop_size*square_h, x:x+crop_size*square_w]
# Save the result
cv2.imwrite('output.jpg', cropped_image)
could not work for top right corner
Solution
ok i figure out what i wanted i think i find how to detect page number from a page
this is the black n white page on the top right is the pattern i want to crop ,this match with page number from multiple even pages ,the one i uploaded input.jpg is an example image
output.txt
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
0000#0#######################0#####################00##00000
000##0##############################################0##00000
000#################################################00000000
000#################################################00000000
000#################################################00000000
000#################################################00000000
000#################################################00000000
000#################################################00000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000##################################################0000000
000####################################################00000
000####################################################00000
000####################################################00000
000####################################################00000
000####################################################00000
000####################################################00000
000####################################################00000
000###0#######################0########################00000
000###0#######################0########################00000
000###0#######################0########################00000
000###0#######################0#####################0##00000
000###0#######################0#####################0##00000
000###0#######################0#####################0##00000
000###0#######################00########0###########0##00000
000000000000000000000000000000000000000000000000000000000000
####0000000000000000000000000000#00####00#0###000#00########
############################################################
00#0##0##0###000000000#0##000000#########00000##0000000#0000
00#0#0#0#0#000#00#000#00##00000##########000#00##0##00#00000
0###0#00###0##00#000####000#0#00###################00#000000
0#00##00####0#000000##00###0000#######0########0######000000
this is the pattern i will try to crop here is on top right
0000
0##0
0##0
0000
0000
this is the code
import cv2
import numpy as np
# Load the cropped image in grayscale
image = cv2.imread('input.jpg', cv2.IMREAD_GRAYSCALE)
# Get the height and width of the image
h, w = image.shape
# Define the number of rows and columns of the grid
rows = 45
cols = 60
# Calculate the size of each square in pixels
square_h = h // rows
square_w = w // cols
# Create an empty array to store the grid values
grid = np.empty((rows, cols), dtype=str)
# Loop over the rows and columns of the grid
for i in range(rows):
for j in range(cols):
# Get the coordinates of the top left corner of the square
x = j * square_w
y = i * square_h
# Get the sub-image corresponding to the square
square = image[y:y+square_h, x:x+square_w]
# Check if the square contains any black pixel
if np.any(square < 255):
# Set the grid value to '#'
grid[i, j] = '#'
else:
# Set the grid value to '0'
grid[i, j] = '0'
# Write the result to output.txt
with open('output.txt', 'w') as file:
for i in range(rows):
for j in range(cols):
file.write(grid[i, j])
file.write('\n')
# Pattern matching
pattern = [
'0000',
'0##0',
'0##0',
'0000',
'0000'
]
with open("output.txt", "r") as file:
content = file.read()
lines = content.split('\n')
for i in range(len(lines) - len(pattern) + 1):
for j in range(len(lines[i]) - len(pattern[0]) + 1):
match = True
for k in range(len(pattern)):
if lines[i + k][j:j+len(pattern[0])] != pattern[k]:
match = False
break
if match:
print(f"Pattern found at line {i+1}, position {j+1}")
# Crop the region where the pattern is found
x_start = j * square_w
y_start = i * square_h
x_end = (j + len(pattern[0])) * square_w
y_end = (i + len(pattern)) * square_h
cropped_image = image[y_start:y_end, x_start:x_end]
# Save the cropped image
cv2.imwrite('cropped_page_number.jpg', cropped_image)
thanks for your comments
Answered By - konos93
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.