Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def setup ( ) : global source, destination, ti , e , tf size ( 8 0 0 0 , 6 0 0 0 )

def setup():
global source, destination, ti, e, tf
size(8000,6000)
source = loadImage("image.jpeg")
# The destination image is created as a blank image the same size as the source.
destination = createImage(source.width, source.height, RGB)
e =5
ti =0
tf = adpt_th(ti)
while(abs(ti - tf)>e):
temp = adpt_th(tf)
ti = tf
tf = temp
print(tf)
img1= binarize(tf)
img2= crop(img1)
image(source,0,0)
image(img1,0, source.height+10)
image(img2,0,2*source.height+20)
def adpt_th(t):
e =5
ti =120
r =[20,40,250,70,90,120,170,190,220,5]
g1=[]
g2=[]
for i in range(len(r)):
if r[i]<= ti:
g1.append(r[i])
else:
g2.append(r[i])
sum1= sum(g1)
sum2= sum(g2)
l1= len(g1)
l2= len(g2)
a1= sum1/ l1 if l1!=0 else 0
a2= sum2/ l2 if l2!=0 else 0
tf =(a1+ a2)/2 if (l1!=0 and l2!=0) else 0
return g1, g2, sum1, sum2, l1, l2, a1, a2, tf
# Example usage
g1, g2, sum1, sum2, l1, l2, a1, a2, tf = adpt_th(0)
print("g1:", g1)
print("g2:", g2)
print("Sum of elements in g1:", sum1)
print("Sum of elements in g2:", sum2)
print("Length of g1:", l1)
print("Length of g2:", l2)
print("Average of elements in g1:", a1)
print("Average of elements in g2:", a2)
print("Threshold:", tf)
def binarize(t):
source.loadPixels()
destination.loadPixels()
for x in range(source.width):
for y in range(source.height):
loc = x + y*source.width
r = brightness(source.pixels[loc])
#print(r)
# Test the brightness against the threshold
if (r > tf):
destination.pixels[loc]= color(255)
else:
destination.pixels[loc]= color(0)
destination.updatePixels()
return(destination)
def crop(destination):
xl =0
xr =0
yt =0
yb =0
destination.loadPixels()
found = False
for x in range(destination.width):
for y in range(destination.height):
if (found == False):
loc = loc = x + y*source.width
r = brightness(destination.pixels[loc])
#print(r)
if r ==0:
xl = x
found = True
found = False
for x in reverse(range(destination.width)):
for y in range(destination.height):
if (found == False):
loc = loc = x + y*source.width
r = brightness(destination.pixels[loc])
#print(r)
if r ==0:
xr = x
found = True
found = False
for y in range(destination.height):
for x in range(destination.width):
if (found == False):
loc = loc = x + y*source.width
r = brightness(destination.pixels[loc])
#print(r)
if r ==0:
yt = y
found = True
found = False
for y in reverse(range(destination.height)):
for x in range(destination.width):
if (found == False):
loc = loc = x + y*source.width
r = brightness(destination.pixels[loc])
#print(r)
if r ==0:
yb = y
found = True
print(xl, xr, yt, yb)
i = destination.get(xl, yt, xr-xl, yb-yt)
return(i)
Alternate code for this in python

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions

Question

just do it in python , and what do you want more ?

Answered: 1 week ago