0% found this document useful (0 votes)
104 views

Artificial Intelligence Lab 5

This document contains code for three tasks related to image processing: Task 1 converts an image to black and white, Task 2 creates a box around the image content, and Task 3 locates the centroid of the boxed image by calculating the average x and y coordinates of all black pixels. The code takes an input image, performs the black and white conversion and box creation, then on the third task calculates and prints the centroid coordinates.

Uploaded by

Hussain Rizvi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Artificial Intelligence Lab 5

This document contains code for three tasks related to image processing: Task 1 converts an image to black and white, Task 2 creates a box around the image content, and Task 3 locates the centroid of the boxed image by calculating the average x and y coordinates of all black pixels. The code takes an input image, performs the black and white conversion and box creation, then on the third task calculates and prints the centroid coordinates.

Uploaded by

Hussain Rizvi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Artificial Intelligence Lab 5

Syed Muhammad Hussain Rizvi


241736
Muhammad Kumail
242308

Task 1 code : (Missing Code and Updated previous qs code)

def cornersHeuristic(state, problem):

corners = problem.corners # These are the corner coordinates


walls = problem.walls # These are the walls of the maze, as a Grid (game.py)

"*** YOUR CODE HERE ***"

# Find which corners are left to reach GoalState.


visitedCorners = state[1]
cornersLeftToVisit = []
for corner in corners:
if corner not in visitedCorners:
cornersLeftToVisit.append(corner)
# While not all corners are visited find via manhattanDistance
# the most efficient path for each corner
totalCost = 0
coordinate = state[0]
curPoint = coordinate
while cornersLeftToVisit:
heuristic_cost, corner = \
min([(util.manhattanDistance(curPoint, corner), corner) for corner in cornersLeftToVisit])
cornersLeftToVisit.remove(corner)
curPoint = corner
totalCost += heuristic_cost

return totalCost
1

Output:

Autograder.py Output:
2

Task 2:

from PIL import Image, ImageFilter,ImageDraw,ImageFont


import numpy as np
import os

# 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

image = BW_img.crop((left-1, top-1, right+2, bottom+2))


width, height = image.size
border = [(0, 0), (width-1 , height-1)]
img1 = ImageDraw.Draw(image)
img1.rectangle(border, outline ="#D3D3D3")
return image

def main():
BW_img=ConvertImagetoBW("sign.jpeg")
Boxed_img=CreateBox(BW_img)
Boxed_img.save("sign2.jpeg", "JPEG")

Output From Task 2:

Test Images used:

Image 1 Image 2 Image 3

Converted Images:
4

Task 3:

from PIL import Image, ImageFilter,ImageDraw,ImageFont


import numpy as np
import os

# Task 2 Code here also same as above

# 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:

You might also like