Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Imagine we have an image.
- #We’ll represent this image as a simple 2D array where every pixel is a 1 or a 0.
- #The image you get is known to have a single rectangle of 0s on a background of 1s.
- #Write a function that takes in the image and returns the coordinates of the rectangle
- #-- either TOP LEFT and BOTOM RIGHT; or top-left, width, and height.
- #NOTE: The following code calculates for TOP LEFT and BOTTOM RIGHT ONLY
- #ASSUMPTIONS: Rectangle will always be the same size
- image1 = [
- [1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 0, 0, 0, 1],
- [1, 1, 1, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1]
- ];
- image2 = [
- [0, 0, 0, 1, 1, 1, 1],
- [0, 0, 0, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1]
- ];
- image3 = [
- [1, 1, 1, 1, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1],
- [1, 0, 0, 0, 1, 1, 1],
- [1, 0, 0, 0, 1, 1, 1],
- [1, 1, 1, 1, 1, 1, 1]
- ];
- def return_coordinates(image):
- evaluation = [] #stores one list of image object per iteration
- coordinates = [] #stores lists of coordiantes - top left, bottom right
- top_left = [] #stores top left coordinates
- bottom_right = [] #stores bottom right coordinates
- #EVALUATE IMAGE OBJECT
- # (1) iterates through list object
- # (2) identifies list with image values(0)
- # (3) passes determined/calculated values to a nested list object
- row = 0
- top_left_count = 0
- for i in image:
- evaluation = i
- row = row + 1
- column = 0
- #pseudo LOG statements
- print( '---' )
- print('start eval row#', row)
- print(evaluation)
- print( '---' )
- for i in evaluation:
- column = column + 1
- print('start eval column', column)
- #Find TOP LEFT Coordinates
- if i == 0:
- print("top_left_count", top_left_count)
- if top_left_count == 0:
- top_left.append(row)
- top_left.append(column)
- #print('TOP LEFT COORDITANES [ROW, COLUMN]', top_left)
- top_left_count = 1
- #print("top_left_count", top_left_count)
- coordinates.append(top_left)
- break
- #Calculate BOTTOM RIGHT Coordinates
- if top_left_count == 1:
- column = column + 2
- bottom_right.append(row)
- bottom_right.append(column)
- #print('BOTTOM RIGHT COORDITANES [ROW, COLUMN]', bottom_right)
- coordinates.append(bottom_right)
- break
- else:
- break
- print('---')
- print('TOP LEFT COORDITANES [ROW, COLUMN]', top_left)
- print('BOTTOM RIGHT COORDITANES [ROW, COLUMN]', bottom_right)
- print('COORDITANES [ROW, COLUMN] for ', coordinates)
- print('---')
- return coordinates
- #swap out image objects to see different coordinates returned to the console
- #print(return_coordinates(image1))
- #print(return_coordinates(image2))
- print(return_coordinates(image3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement