Python - Encontre o centro do objeto em uma imagem
Eu tenho um arquivo de imagem que possui um fundo branco com um objeto não branco. Eu quero encontrar o centro do objeto usando python (Pillow).
Encontrei uma pergunta semelhante em c ++, mas nenhuma resposta aceitável -Como posso encontrar o centro do objeto?
Pergunta semelhante, mas com links quebrados na resposta -Qual é a maneira mais rápida de encontrar o centro de um polígono de formato irregular? (links quebrados em resposta)
Também li esta página, mas ela não me fornece uma receita útil -https://en.wikipedia.org/wiki/Smallest-circle_problem
Aqui está um exemplo de imagem:
Edit: A solução atual que estou usando é esta:
def find_center(image_file):
img = Image.open(image_file)
img_mtx = img.load()
top = bottom = 0
first_row = True
# First we find the top and bottom border of the object
for row in range(img.size[0]):
for col in range(img.size[1]):
if img_mtx[row, col][0:3] != (255, 255, 255):
bottom = row
if first_row:
top = row
first_row = False
middle_row = (top + bottom) / 2 # Calculate the middle row of the object
left = right = 0
first_col = True
# Scan through the middle row and find the left and right border
for col in range(img.size[1]):
if img_mtx[middle_row, col][0:3] != (255, 255, 255):
left = col
if first_col:
right = col
first_col = False
middle_col = (left + right) / 2 # Calculate the middle col of the object
return (middle_row, middle_col)