1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【画像処理100本ノックに挑戦】Q.28. アフィン変換(平行移動)

Last updated at Posted at 2019-12-23

使用したライブラリ

【画像処理100本ノック】独自の画像入出力クラスを作る
【画像処理100本ノックに挑戦】独自の行列クラスを作る

Q.28. アフィン変換(平行移動)

アフィン変換を利用して画像をx方向に+30、y方向に-30だけ平行移動させよ。

これのために独自の行列クラスを作ったのです。簡単な行列演算をするだけです。

int main()
{
	PPM ppm("imori.pnm");
	int width = ppm.Get_width();
	int height = ppm.Get_height();

	Matrix A(3, 3);
	A.set({
		1,0,30,
		0,1,-30,
		0,0,1
		});
	
	PPM ppm2(width, height);
	for(int i=0; i<width; i++)
		for (int j = 0; j < height; j++)
		{
			ppm2(i, j, 'r') = 0;
			ppm2(i, j, 'g') = 0;
			ppm2(i, j, 'b') = 0;
		}
	for(int i=0; i<width; i++)
		for (int j = 0; j < height; j++)
		{
			Matrix X(3);
			X.set({
				(double)i,
				(double)j,
				1.
				});
			Matrix X2 = A * X;
			int i2 = X2(0, 0);
			int j2 = X2(1, 0);
			if (0 <= i2 && i2 < width && 0 <= j2 && j2 < height)
			{
				ppm2(i2, j2, 'r') = ppm(i, j, 'r');
				ppm2(i2, j2, 'g') = ppm(i, j, 'g');
				ppm2(i2, j2, 'b') = ppm(i, j, 'b');
			}
		}
	
	ppm2.Flush("out.ppm");
	return 0;
}

imori.png out.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?