def _drawDebugBoundingBoxes(self, renderer, camera): ''' Internal function to visualize the bounding boxes. ''' from look import OutlineLook from models import Rectangle from views import DefaultRectangleRenderer # set look outline = OutlineLook('black') outline.Apply(renderer) # local bbox model = Rectangle(self.localBoundingBox.Size) view = DefaultRectangleRenderer(renderer, model) view.transform = self.worldTransform view.Render(camera)
def getTestSymbols(color_image): ''' Segments the image into lines For each of these lines, finds individual symbol and gets a list of such symbols in a list of Rectangle objects. :param color_image: input image :return: list of Rectangle objects, each object shows outline of a symbol ''' input_lines,stanzalinecount = part1.getLines(color_image) rectList = [] for tuple in input_lines: rectList.append(Rectangle.Rectangle(tuple[0], tuple[1])) return part2.segmentImage(color_image, rectList)
def segmentImage(image, rectList): gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) binary_image = cv2.threshold(gray_image, 200, 255, cv2.THRESH_BINARY)[1] rows, cols = binary_image.shape eroded_image = cv2.erode(binary_image, np.ones((3, 3), np.uint8), iterations=1) output = [] count = 0 # meta.displayImage(eroded_image,"erode") for rect in rectList: start_point = rect.top_left end_point = rect.bottom_right (x1, y1) = start_point (x2, y2) = end_point vertical_histogram = np.zeros(y2 + 1) for i in range(x1, x2 + 1): for j in range(y1, y2 + 1): if (eroded_image[i][j] == 0): count += 1 vertical_histogram[j] = vertical_histogram[j] + 1 # calculates vertical_histogram i = y1 while (i <= y2): while (i <= y2 and vertical_histogram[i] == 0): i = i + 1 start = i while (i <= y2 and vertical_histogram[i] > 0): i = i + 1 end = i if (end - start >= 7): # Identified a symbol output.append(Rectangle.Rectangle((start, x1), (end, x2))) cv2.rectangle(binary_image, (start, x1), (end, x2), 0, 1) meta.displayImage(binary_image, 'binary') return output
def main(): width = 900 height = 700 pygame.init() screen = pygame.display.set_mode((width, height)) color = (0.40, 0.40, 0 / 40) box1 = Box(100, 10, 200, 80, 90, 2) box2 = Box(-100, 10, 200, 85, 90, 2) box3 = Box(100, 10, 300, 100, 100, 2) box4 = Box(-100, 10, 300, 100, 130, 2) rect = Rectangle(0, 100, 80, 300, 3) roll = 0 pitch = 0 yaw = 0 x_offset = 0 y_offset = 0 z_offset = 0 fov = 80 epsilon = 0.005 keys = {} vp = viewport(0, 0, 0.1, 100, width, height) while True: screen.fill((255, 255, 255)) # controls event = pygame.event.poll() if event.type == KEYDOWN: keys[event.key] = True elif event.type == KEYUP: keys[event.key] = False if keys.get(K_a, False): x_offset -= STEP if keys.get(K_d, False): x_offset += STEP if keys.get(K_u, False): y_offset += STEP if keys.get(K_j, False): y_offset -= STEP if keys.get(K_s, False): z_offset -= STEP if keys.get(K_w, False): z_offset += STEP if keys.get(K_LEFT, False): yaw += STEP if keys.get(K_RIGHT, False): yaw -= STEP if keys.get(K_z, False): roll -= STEP if keys.get(K_x, False): roll += STEP if keys.get(K_UP, False) and keys.get(K_LCTRL, False): fov -= STEP elif keys.get(K_UP, False): pitch += STEP if keys.get(K_DOWN, False) and keys.get(K_LCTRL, False): fov += STEP elif keys.get(K_DOWN, False): pitch -= STEP text = set_up_text(fov, pitch, yaw, roll, x_offset, y_offset, z_offset) projection = perspective_projection(fov, 0.1, 100, width, height) t = translation(-x_offset, -y_offset, -z_offset) mat = t.dot(rotation(-pitch, -yaw, -roll)) mat = mat.dot(projection) mat = mat.dot(vp) transformed_triangles = [] for box in (box1, box2, box3, box4, rect): transformed_points = [point.dot(mat) for point in box.POINTS] for triangle in box.TRIANGLES: p0 = transformed_points[triangle[0]] p1 = transformed_points[triangle[1]] p2 = transformed_points[triangle[2]] if p0[3] < epsilon or p1[3] < epsilon or p2[3] < epsilon: continue p0 = p0 / p0[3] p1 = p1 / p1[3] p2 = p2 / p2[3] transformed_triangles.append((p0, p1, p2)) for p0, p1, p2 in sorted(transformed_triangles, key=get_z, reverse=True): # drawing pygame.draw.polygon(screen, 0xabcaf4, [p0[:2], p1[:2], p2[:2]]) pygame.draw.aalines(screen, (0, 0, 0), True, [p0[:2], p1[:2], p2[:2]], 1) # screen.blit(text, (420 - text.get_width() // 2, 10 - text.get_height() // 2)) # exit if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): pygame.quit() sys.exit() pygame.display.flip()
def get_rectangle(): return Rectangle(4, 6)
# calculates vertical_histogram i = y1 while (i <= y2): while (i <= y2 and vertical_histogram[i] == 0): i = i + 1 start = i while (i <= y2 and vertical_histogram[i] > 0): i = i + 1 end = i if (end - start >= 7): # Identified a symbol output.append(Rectangle.Rectangle((start, x1), (end, x2))) cv2.rectangle(binary_image, (start, x1), (end, x2), 0, 1) meta.displayImage(binary_image, 'binary') return output if __name__ == "__main__": color_image = cv2.imread(meta.getRootDir() + '/images/vasant_cont.jpeg', cv2.IMREAD_COLOR) input_lines = [((0, 0), (439, 30)), ((0, 90), (439, 120)), ((0, 213), (439, 243))] rectList = [] for tuple in input_lines: rectList.append(Rectangle.Rectangle(tuple[0], tuple[1])) o, binary_image = segmentImage(color_image, rectList) cv2.imshow('binary_image', binary_image) cv2.waitKey(0) cv2.destroyAllWindows()
def test_invalid_rectangle(side_a, side_b): with pytest.raises(AttributeError): Rectangle(side_a, side_b)
def test_add_areas(): figure1 = Rectangle(10, 12) figure2 = Rectangle(8, 5) assert figure1.add_area(figure2) == figure1.area + figure2.area