PythonLibrary Python NumPy 2019年 1月 18日
MonoImageBlockIterator をベクトル,行列で利用できるようになった. 前回は,ブロックサイズに満たないブロックを無視するように is_small_ok = False の場合のみをテストした. 今回は,is_small_ok = True の例として,画像のブロックごとの平均を使った縮小処理のテストをしてみる.
with such.A('MonoImage class') as it:
@it.should('calc block mean')
def test():
### vector test
org_vec = MonoImage(np.arange(0, 9), 8)
correct_vec = MonoImage.create_with_array([0, 2, 4, 6, 8], np.int, 8)
ans_vec = org_vec.generate_with_mean(1, 2)
it.assertEqual(ans_vec, correct_vec)
### matrix test
org_mat = MonoImage(np.arange(0, 35).reshape(5, 7), 8)
correct_mat = MonoImage.create_with_array([[4, 7, 9], [18, 21, 23], [29, 32, 34]], np.int, 8)
ans_mat = org_mat.generate_with_mean(2, 3)
it.assertEqual(ans_mat, correct_mat)
class MonoImage:
def generate_with_mean(self, br, bc):
nr = int((self.height + br - 1) / br)
nc = int((self.width + bc - 1) / bc)
ans = self.create_with_zeros((nr, nc), self.dtype)
for (pi, bi) in zip(ans.pixel_iterator(), self.block_iterator(br, bc, br, bc, True)):
pi.value = np.mean(bi.value)
return ans
短いが今日はここまで.