画像処理入門 > 幾何補正 > 射影変換

射影変換


射影変換
射影変換のプログラムです。
サンプルの射影変換用の行列は、以下の4点の座標変換になっています。

(0, 0)→(0, 0)
(640, 0)→(580, 25)
(0, 480)→(0, 480)
(640, 480)→(600, 400)

バイリニア補間を使用し、端は処理していません。

サンプルプログラム
プログラムはこちら void homography(unsigned char *out, unsigned char *in, int inHeight, int inWidth, int inChannel, double *ht){ int i, j; int index; double tmp, tmpX, tmpY; int floorX, floorY; int inX, inY; int widthStep; widthStep = inWidth * inChannel; if(widthStep % 4 != 0){ widthStep = widthStep + 4 - widthStep % 4; } for(i = 0; i < inHeight; ++i){ for(j = 0; j < inWidth; ++j){ tmp = j * ht[6] + i * ht[7] + 1.0; tmpX = (j * ht[0] + i * ht[1] + ht[2]) / tmp; tmpY = (j * ht[3] + i * ht[4] + ht[5]) / tmp; floorX = (int)tmpX; floorY = (int)tmpY; if(tmpX > 1 && tmpX < (inWidth - 1) && tmpY > 1 && tmpY < (inHeight - 1)){ // 左上 + 右上 + 左下 + 右下 index = i * widthStep + j; out[index] = (floorX + 1.0 - tmpX) * (floorY + 1.0 - tmpY) * (double)in[floorY * widthStep + floorX] + (tmpX - floorX) * (floorY + 1.0 - tmpY) * (double)in[floorY * widthStep + (floorX + 1)] + (floorX + 1.0 - tmpX) * (tmpY - floorY) * (double)in[(floorY + 1) * widthStep + floorX] + (tmpX - floorX) * (tmpY - floorY) * (double)in[(floorY + 1) * widthStep + floorX + 1]; } } } }