def get_rect_ragion(contour, the_image): ''' Contour.get_rect_ragion ''' return Rect.get_ragion(cv2.boundingRect(contour), the_image)
def rect(the_image, rect): rect_image = Rect.get_ragion(rect, the_image) Display.image(rect_image)
with test(Contour.get_rect_ragion): contour = numpy.array([[[ 171, 21]], [[ 18, 26]], [[ 25, 216]], [[ 175, 212]]]) ''' notice the result will be different with different picture. ''' part_image1 = Contour.get_rect_ragion(contour, gray_image) part_image1.shape.must_equal((196, 158)) part_image1[0,0].must_equal(gray_image[21,18]) # Display.image(part_image1) rect = (18, 21, 158, 196) cv2.boundingRect(contour).must_equal(rect) part_image2 = Rect.get_ragion(rect, gray_image) part_image2.shape.must_equal((196, 158)) part_image2[0,0].must_equal(gray_image[21,18]) ''' uncomment the below, you can see the consequence in a picture. ''' # Display.image(part_image1) with test(Contour.check_beyond_borders): contour = numpy. array( [[[ -1, 116]], [[334, 202]], [[318, 457]], [[ 74, 460]]]) height, width = 800, 600 the_image = Image.generate_mask((height, width)) changed_contour = Contour.check_beyond_borders(contour, the_image.shape) changed_contour[0,0,0].must_equal(0)