我正在尝试在C++和OpenCV中实现图像变形。我的代码如下:
Mat input = imread("Lena.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat out;
double xo, yo;
input.convertTo(input, CV_32FC1);
copyMakeBorder(input, input, 3, 3, 3, 3, 0);
int height = input.rows;
int width = input.cols;
out = Mat(height, width, input.type());
for(int j = 0; j < height; j++){
for(int i =0; i < width; i++){
xo = (8.0 * sin(2.0 * PI * j / 128.0));
yo = (8.0 * sin(2.0 * PI * i / 128.0));
out.at<float>(j,i) = (float)input.at<float>(((int)(j+yo+height)%height),((int)(i+xo+width)%width));
}
}
normalize(out, out,0,255,NORM_MINMAX,CV_8UC1);
imshow("output", out);
这将产生以下图像:
清晰可见,边界附近的值不为零。谁能告诉我如何获得下图所示的黑色边框,而不是我从代码中得到的伪像?
仅应考虑该图像的黑色边框,即图像应为波浪形(正弦),但无伪像。
谢谢...
最佳答案
这里:
xo = (8.0 * sin(2.0 * PI * j / 128.0));
yo = (8.0 * sin(2.0 * PI * i / 128.0));
out.at<float>(j,i) = (float)input.at<float>(((int)(j+yo+height)%height),((int)(i+xo+width)%width));
您可以计算源像素的位置,但是要使用具有宽度/高度的mod来确保其在图像中。这导致像素在边缘缠绕。相反,您需要将图像外部的任何像素设置为黑色(或者,如果源图像具有黑色边框,则将其钳位到边缘)。
由于已经有边框,因此可以钳位坐标,如下所示:
int ix = min(width-1, max(0, (int) (i + xo)));
int iy = min(height-1, max(0, (int) (j + yo)));
out.at<float>(j,i) = (float)input.at<float>(iy,ix);