In search of the center
This commit is contained in:
commit
3a13532849
1 changed files with 111 additions and 0 deletions
111
process_image.py
Normal file
111
process_image.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
import getopt
|
||||
import sys
|
||||
import os
|
||||
from PIL import Image
|
||||
|
||||
min_threshold = 200
|
||||
scan_grid_size = 1
|
||||
|
||||
options = "hi:"
|
||||
long_options = ["help", "image="]
|
||||
|
||||
argumentList = sys.argv[1:]
|
||||
arguments, values = getopt.getopt(argumentList, options, long_options)
|
||||
|
||||
image_name = None
|
||||
|
||||
for currentArgument, currentValue in arguments:
|
||||
|
||||
if currentArgument in ("-h", "--help"):
|
||||
print("reader.py --image=image.jpg")
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
elif currentArgument in ("-i", "--image"):
|
||||
image_name = currentValue
|
||||
|
||||
print('image name: ' + image_name)
|
||||
|
||||
f, e = os.path.splitext(image_name)
|
||||
|
||||
|
||||
def convert_coordinates(coords, grid_size):
|
||||
grid_x_center = round(grid_size[0] / 2)
|
||||
grid_y_center = round(grid_size[1] / 2)
|
||||
|
||||
new_circle_x = coords[0] - grid_x_center
|
||||
new_circle_y = coords[1] - grid_y_center
|
||||
|
||||
return new_circle_x, new_circle_y
|
||||
|
||||
|
||||
def find_circle(image):
|
||||
px = image.load()
|
||||
im_size = image.size
|
||||
print("size: ", im_size)
|
||||
|
||||
pixel_coords = {
|
||||
'max_top': {},
|
||||
'max_bottom': {},
|
||||
'cur_line_max_bottom': {},
|
||||
'prev_line_max_bottom': {}
|
||||
}
|
||||
|
||||
is_prev_line_null = 1
|
||||
|
||||
for y in range(0, im_size[1], scan_grid_size):
|
||||
# print("{:3}: ".format(y), end="")
|
||||
is_current_line_not_null = 0
|
||||
|
||||
for x in range(0, im_size[0], scan_grid_size):
|
||||
px_colors = px[x, y]
|
||||
intens = 0 if px_colors[1] <= min_threshold else px_colors[1]
|
||||
|
||||
is_current_line_not_null = is_current_line_not_null + intens
|
||||
|
||||
if is_prev_line_null:
|
||||
if intens > 0:
|
||||
top_max_intense = 0 if not pixel_coords["max_top"].get("intense") else pixel_coords["max_top"].get(
|
||||
"intense")
|
||||
if intens > top_max_intense:
|
||||
pixel_coords["max_top"] = {"intense": intens, "x": x, "y": y}
|
||||
|
||||
max_bottom_intense = 0 if not pixel_coords["cur_line_max_bottom"].get("intense") else pixel_coords[
|
||||
"cur_line_max_bottom"].get("intense")
|
||||
|
||||
if intens > 0 and intens > max_bottom_intense:
|
||||
pixel_coords["cur_line_max_bottom"] = {"intense": intens, "x": x, "y": y}
|
||||
|
||||
# print('{:4}'.format(intens), end="")
|
||||
|
||||
if is_current_line_not_null > 0:
|
||||
is_prev_line_null = 0
|
||||
pixel_coords["prev_line_max_bottom"] = pixel_coords["cur_line_max_bottom"]
|
||||
pixel_coords["cur_line_max_bottom"] = {}
|
||||
else:
|
||||
if not pixel_coords["max_bottom"]:
|
||||
pixel_coords["max_bottom"] = pixel_coords["prev_line_max_bottom"]
|
||||
|
||||
# print("")
|
||||
|
||||
print("")
|
||||
|
||||
print("coords: ", pixel_coords)
|
||||
x_center = round((pixel_coords["max_bottom"]["x"] + pixel_coords["max_top"]["x"]) / 2)
|
||||
y_center = round((pixel_coords["max_bottom"]["y"] + pixel_coords["max_top"]["y"]) / 2)
|
||||
|
||||
print("center x: {:3} y: {:3}".format(x_center, y_center))
|
||||
|
||||
coords = convert_coordinates((x_center, y_center), im_size)
|
||||
return coords
|
||||
|
||||
|
||||
try:
|
||||
with Image.open(image_name) as img:
|
||||
|
||||
circle_on_image_coords = find_circle(img)
|
||||
|
||||
print("center coords: ", circle_on_image_coords)
|
||||
|
||||
except OSError:
|
||||
print("cannot open", image_name)
|
Loading…
Reference in a new issue