def part_3a(): """Complete the remaining parts of this section as instructed in the instructions document.""" feature1 = ps6.HaarFeature((2, 1), (25, 30), (50, 100)) feature1.preview((200, 200), filename="ps6-3-a-1.png") # TODO: Generate and save all required images feature2 = ps6.HaarFeature((1, 2), (10, 25), (50, 150)) feature2.preview((200, 200), filename="ps6-3-a-2.png") feature3 = ps6.HaarFeature((3, 1), (50, 50), (100, 50)) feature3.preview((200, 200), filename="ps6-3-a-3.png") feature4 = ps6.HaarFeature((1, 3), (50, 125), (100, 50)) feature4.preview((200, 200), filename="ps6-3-a-4.png") feature5 = ps6.HaarFeature((2, 2), (50, 25), (100, 150)) feature5.preview((200, 200), filename="ps6-3-a-5.png")
def test_preview(self): feat_types = 2 * [(1, 2)] + 2 * [(2, 1)] + 2 * [(1, 3)] \ + 2 * [(3, 1)] + 2 * [(2, 2)] positions = 5 * [(6, 9), (8, 5)] sizes = 5 * [(14, 18), (11, 15)] samples = 5 * [0, 1] for j in range(10): feat_type = feat_types[j] pos = positions[j] size = sizes[j] s = samples[j] file_name = "haar_preview_ft{}_pos{}_sz{}_{}.npy".format( feat_type, pos, size, s) ref_img = np.load(os.path.join(self.input_dir, file_name)) #Uncomment if you want to see the reference image #cv2.imshow("ref_img", ref_img.astype("uint8")) #cv2.waitKey(0) hf = ps6.HaarFeature(feat_type, pos, size) hf_img = hf.preview((50, 50)) correct = np.allclose(hf_img, ref_img) message = "Output image does not match the reference solution." self.assertTrue(correct, message)
def part_3a(): """Complete the remaining parts of this section as instructed in the instructions document.""" # feature1 = ps6.HaarFeature((2, 1), (25, 30), (50, 100)) # feature1.preview((200, 200), filename="ps6-3-a-1.png") feat1 = ps6.HaarFeature((2, 1), (25, 30), (50, 100)) feat1.preview((200, 200), filename='ps6-3-a-1') feat2 = ps6.HaarFeature((1, 2), (10, 25), (50, 150)) feat2.preview((200, 200), filename='ps6-3-a-2') feat3 = ps6.HaarFeature((3, 1), (50, 50), (100, 50)) feat3.preview((200, 200), filename='ps6-3-a-3') feat4 = ps6.HaarFeature((1, 3), (50, 125), (100, 50)) feat4.preview((200, 200), filename='ps6-3-a-4') feat4 = ps6.HaarFeature((2, 2), (50, 25), (50, 150)) feat4.preview((200, 200), filename='ps6-3-a-5')
def part_3a(): """Complete the remaining parts of this section as instructed in the instructions document.""" feature1 = ps6.HaarFeature((2, 1), (25, 30), (50, 100)) feature1.preview((200, 200), filename="ps6-3-a-1") feature2 = ps6.HaarFeature((1, 2), (10, 25), (50, 150)) feature2.preview((200, 200), filename="ps6-3-a-2") feature3 = ps6.HaarFeature((3, 1), (50, 50), (100, 50)) feature3.preview((200, 200), filename="ps6-3-a-3") feature4 = ps6.HaarFeature((1, 3), (50, 125), (100, 50)) feature4.preview((200, 200), filename="ps6-3-a-4") feature5 = ps6.HaarFeature((2, 2), (50, 25), (100, 150)) feature5.preview((200, 200), filename="ps6-3-a-5")
def part_3a(): """Complete the remaining parts of this section as instructed in the instructions document.""" posn = {1 : [(2, 1), (25, 30), (50, 100)], 2 : [(1, 2), (10, 25), (50, 150)], 3 : [(3, 1), (50, 50), (100, 50)], 4 : [(1, 3), (50, 125), (100, 50)], 5 : [(2, 2), (50, 25), (100, 150)]} for k, p in posn.items(): feature = ps6.HaarFeature(*p) feature.preview((200, 200), filename="ps6-3-a-%s"%(k))
def test_HaarFeature_evaluate(self): ti_path = os.path.join(self.input_dir, "test_image_ii.npy") ii_path = os.path.join(self.input_dir, "integral_image_ii.npy") test_image = np.load(ti_path) integral_image = np.load(ii_path) feat_type = (1, 2) # Change feature type pos = (5, 5) size = (30, 30) if feat_type == (2, 1): A = np.sum(test_image[pos[0]:pos[0] + size[0] / 2, pos[1]:pos[1] + size[1]]) B = np.sum(test_image[pos[0] + size[0] / 2:pos[0] + size[0], pos[1]:pos[1] + size[1]]) ref = A - B if feat_type == (1, 2): A = np.sum(test_image[pos[0]:pos[0] + size[0], pos[1]:pos[1] + size[1] / 2]) B = np.sum(test_image[pos[0]:pos[0] + size[0], pos[1] + size[1] / 2:pos[1] + size[1]]) ref = A - B if feat_type == (3, 1): A = np.sum(test_image[pos[0]:pos[0] + size[0] / 3, pos[1]:pos[1] + size[1]]) B = np.sum(test_image[pos[0] + size[0] / 3:pos[0] + 2 * size[0] / 3, pos[1]:pos[1] + size[1]]) C = np.sum(test_image[pos[0] + 2 * size[0] / 3:pos[0] + size[0], pos[1]:pos[1] + size[1]]) ref = A - B + C if feat_type == (1, 3): A = np.sum(test_image[pos[0]:pos[0] + size[0], pos[1]:pos[1] + size[1] / 3]) B = np.sum(test_image[pos[0]:pos[0] + size[0], pos[1] + size[1] / 3:pos[1] + 2 * size[1] / 3]) C = np.sum(test_image[pos[0]:pos[0] + size[0], pos[1] + 2 * size[1] / 3:pos[1] + size[1]]) ref = A - B + C if feat_type == (2, 2): A = np.sum(test_image[pos[0]:pos[0] + size[0] / 2, pos[1]:pos[1] + size[1] / 2]) B = np.sum(test_image[pos[0]:pos[0] + size[0] / 2, pos[1] + size[1] / 2:pos[1] + size[1]]) C = np.sum(test_image[pos[0] + size[0] / 2:pos[0] + size[0], pos[1]:pos[1] + size[1] / 2]) D = np.sum(test_image[pos[0] + size[0] / 2:pos[0] + size[0], pos[1] + size[1] / 2:pos[1] + size[1]]) ref = -A + B + C - D hf = ps6.HaarFeature(feat_type, pos, size) score = hf.evaluate(integral_image) showImage(integral_image) correct = np.allclose(score, ref) message = "Wrong score returned." self.assertTrue(correct, message)