C で双一次補間を使用して OpenCV のサイズ変更アルゴリズムのコピーを作成しようとしています。達成したいのは、結果の画像が OpenCV によって生成されたものとまったく同じ (ピクセル値) であることです。拡大ではなく縮小に特に関心があり、単一チャネルのグレースケール画像で使用することに関心があります。ネットで双一次補間アルゴリズムが縮小と拡大で異なると読んだのですが、縮小実装の式が見つからなかったので、私が書いたコードが完全に間違っている可能性があります。私が書いたものは、コンピュータ グラフィックスと OpenGL の大学のコースで得た補間の知識から来ています。私が作成したアルゴリズムの結果は、OpenCV によって生成されたものと視覚的には同じですが、ピクセル値が完全に同一ではない (特にエッジ付近) 画像です。双一次補間を使用した縮小アルゴリズムと可能な実装を教えてもらえますか?
注: 添付されたコードは、最初に水平方向に適用し、次に垂直方向に適用する必要がある 1 次元フィルターです (つまり、転置行列を使用)。
Mat rescale(Mat src, float ratio){
float width = src.cols * ratio; //resized width
int i_width = cvRound(width);
float step = (float)src.cols / (float)i_width; //size of new pixels mapped over old image
float center = step / 2; //V1 - center position of new pixel
//float center = step / src.cols; //V2 - other possible center position of new pixel
//float center = 0.099f; //V3 - Lena 512x512 lower difference possible to OpenCV
Mat dst(src.rows, i_width, CV_8UC1);
//cycle through all rows
for(int j = 0; j < src.rows; j++){
//in each row compute new pixels
for(int i = 0; i < i_width; i++){
float pos = (i*step) + center; //position of (the center of) new pixel in old map coordinates
int pred = floor(pos); //predecessor pixel in the original image
int succ = ceil(pos); //successor pixel in the original image
float d_pred = pos - pred; //pred and succ distances from the center of new pixel
float d_succ = succ - pos;
int val_pred = src.at<uchar>(j, pred); //pred and succ values
int val_succ = src.at<uchar>(j, succ);
float val = (val_pred * d_succ) + (val_succ * d_pred); //inverting d_succ and d_pred, supposing "d_succ = 1 - d_pred"...
int i_val = cvRound(val);
if(i_val == 0) //if pos is a perfect int "x.0000", pred and succ are the same pixel
i_val = val_pred;
dst.at<uchar>(j, i) = i_val;
}
}
return dst;
}