Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #Imagine we have an image.
  2. #We’ll represent this image as a simple 2D array where every pixel is a 1 or a 0.
  3. #The image you get is known to have a single rectangle of 0s on a background of 1s.
  4. #Write a function that takes in the image and returns the coordinates of the rectangle
  5. #-- either TOP LEFT and BOTOM RIGHT; or top-left, width, and height.
  6.  
  7.  
  8. #NOTE: The following code calculates for TOP LEFT and BOTTOM RIGHT ONLY
  9. #ASSUMPTIONS: Rectangle will always be the same size
  10.  
  11. image1 = [
  12. [1, 1, 1, 1, 1, 1, 1],
  13. [1, 1, 1, 1, 1, 1, 1],
  14. [1, 1, 1, 0, 0, 0, 1],
  15. [1, 1, 1, 0, 0, 0, 1],
  16. [1, 1, 1, 1, 1, 1, 1]
  17. ];
  18.  
  19. image2 = [
  20. [0, 0, 0, 1, 1, 1, 1],
  21. [0, 0, 0, 1, 1, 1, 1],
  22. [1, 1, 1, 1, 1, 1, 1],
  23. [1, 1, 1, 1, 1, 1, 1],
  24. [1, 1, 1, 1, 1, 1, 1]
  25. ];
  26.  
  27. image3 = [
  28. [1, 1, 1, 1, 1, 1, 1],
  29. [1, 1, 1, 1, 1, 1, 1],
  30. [1, 0, 0, 0, 1, 1, 1],
  31. [1, 0, 0, 0, 1, 1, 1],
  32. [1, 1, 1, 1, 1, 1, 1]
  33. ];
  34.  
  35.  
  36. def return_coordinates(image):
  37. evaluation = [] #stores one list of image object per iteration
  38. coordinates = [] #stores lists of coordiantes - top left, bottom right
  39. top_left = [] #stores top left coordinates
  40. bottom_right = [] #stores bottom right coordinates
  41.  
  42.  
  43. #EVALUATE IMAGE OBJECT
  44. # (1) iterates through list object
  45. # (2) identifies list with image values(0)
  46. # (3) passes determined/calculated values to a nested list object
  47. row = 0
  48. top_left_count = 0
  49.  
  50. for i in image:
  51. evaluation = i
  52. row = row + 1
  53. column = 0
  54.  
  55. #pseudo LOG statements
  56. print( '---' )
  57. print('start eval row#', row)
  58. print(evaluation)
  59. print( '---' )
  60.  
  61. for i in evaluation:
  62. column = column + 1
  63. print('start eval column', column)
  64. #Find TOP LEFT Coordinates
  65. if i == 0:
  66. print("top_left_count", top_left_count)
  67. if top_left_count == 0:
  68. top_left.append(row)
  69. top_left.append(column)
  70. #print('TOP LEFT COORDITANES [ROW, COLUMN]', top_left)
  71. top_left_count = 1
  72. #print("top_left_count", top_left_count)
  73. coordinates.append(top_left)
  74. break
  75. #Calculate BOTTOM RIGHT Coordinates
  76. if top_left_count == 1:
  77. column = column + 2
  78. bottom_right.append(row)
  79. bottom_right.append(column)
  80. #print('BOTTOM RIGHT COORDITANES [ROW, COLUMN]', bottom_right)
  81. coordinates.append(bottom_right)
  82. break
  83. else:
  84. break
  85.  
  86. print('---')
  87. print('TOP LEFT COORDITANES [ROW, COLUMN]', top_left)
  88. print('BOTTOM RIGHT COORDITANES [ROW, COLUMN]', bottom_right)
  89. print('COORDITANES [ROW, COLUMN] for ', coordinates)
  90. print('---')
  91.  
  92. return coordinates
  93.  
  94. #swap out image objects to see different coordinates returned to the console
  95. #print(return_coordinates(image1))
  96. #print(return_coordinates(image2))
  97. print(return_coordinates(image3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement