Artificial Intelligence Lab 5
Artificial Intelligence Lab 5
return totalCost
1
Output:
Autograder.py Output:
2
Task 2:
# Task 2
def ConvertImagetoBW(ImageLink):
img=Image.open(ImageLink).convert("L")
img_array=np.array(img)
for i in range(img_array.shape[0]):
for j in range(img_array.shape[1]):
if(img_array[i,j]<110):
img_array[i,j]=0
else:
img_array[i,j]=255
BW_img=Image.fromarray(img_array)
return BW_img
# Task 2
def CreateBox(BW_img):
width, height = BW_img.size
left=width
right = 0
top=height
bottom=0
for x in range(width):
for y in range(height):
coordinates=x,y
color=BW_img.getpixel(coordinates)
if(color == 0):
if(x > right):
right=x
if(x < left):
left=x
if(y>bottom):
bottom=y
if(y<top):
3
top=y
def main():
BW_img=ConvertImagetoBW("sign.jpeg")
Boxed_img=CreateBox(BW_img)
Boxed_img.save("sign2.jpeg", "JPEG")
Converted Images:
4
Task 3:
# Task 3
def locateCentroid(image):
width, height = image.size
XX, YY, count = 0, 0, 0
for x in range(0, width, 1):
for y in range(0, height, 1):
if image.getpixel((x, y)) == 0:
XX += x
YY += y
count += 1
print(f"Coordinates of Centroid are : ({int(XX/count)},{int(YY/count)})")
return XX/count, YY/count
def main():
# Task 2 functions
BW_img=ConvertImagetoBW("sign.jpeg")
Boxed_img=CreateBox(BW_img)
Boxed_img.save("sign2.jpeg", "JPEG")
# Task 2 functions used till here
locateCentroid(Boxed_img)
if __name__ == "__main__":
main()
5
Output Task 3:
For Image 1:
For Image 2: