Hi @rob26r,
Since the background color is easily distinguishable from the color of the quarter, you don’t need a very complex script to identify the needed cropping boundaries.
The following is a Python Mode for Processing script that finds and outputs the x and y values for those boundaries. It was tested using the template quarter image from the link that you provided. The Python code is below. Note the following line, which outputs the coordinates to the console:
print(left, right, top, bottom)
The output for the template quarter was:
(437, 1799, 139, 1533)
Here’s the code, which must be executed in Processing Python Mode:
template = None
scaler = 1
def setup():
global template
size(3200 / scaler, 1800 / scaler)
template = loadImage("Template_Qtr.jpg")
# print(template.width, template.height)
noLoop()
def draw():
background(0)
margin = 40 # for avoiding the border color of the image
red_bound = 160 # for identifying the background color of the image
image(template, 0, 0, template.width / scaler, template.height / scaler)
# find left boundary of coin
done = False
for x in range(margin, width - margin):
for y in range(margin, height - margin, 1):
p = get(x, y)
if red(p) < red_bound:
done = True
if done:
break
if done:
left = x
break
# find right boundary of coin
done = False
for x in range(width - margin, margin, -1):
for y in range(margin, height - margin):
p = get(x, y)
# d = dist(red(p), 230, green(p), 110, blue(p), 2)
if red(p) < red_bound:
done = True
if done:
break
if done:
right = x
break
# find top boundary of coin
done = False
for y in range(margin, height - margin, 1):
for x in range(margin, width - margin, 1):
p = get(x, y)
# d = dist(red(p), 230, green(p), 110, blue(p), 2)
if red(p) < red_bound:
done = True
if done:
break
if done:
top = y
break
# find bottom boundary of coin
done = False
for y in range(height - margin, margin, -1):
for x in range(margin, width - margin, 1):
p = get(x, y)
# d = dist(red(p), 230, green(p), 110, blue(p), 2)
if red(p) < red_bound:
done = True
if done:
break
if done:
bottom = y
break
# display boundary data
print(left, right, top, bottom)
With some additional work, you could automate the cropping of the image, and the saving of the results. If you give it a try and post your code, we can make suggestions.
The following links may be helpful to you:
EDITED on April 12, 2022 to change the value of red_bound
to 160
.