How to use OpenCV to make mirror image effect in C++
This article mainly introduces how to use OpenCV in C++ to make mirror image effect related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this C++ article on how to use OpenCV to make mirror image effect article will have a harvest, let's take a look.
I. Convex lens
Create a convex lens effect (enlarge the image). According to the transformation formula found on the Internet:
Image magnification: convex lens
X = (dx / 2) * (sqrt (pow (dx, 2) + pow (dy, 2)) / r) + cx
Y = (dy / 2) * (sqrt (pow (dx, 2) + pow (dy, 2)) / r) + cy
1. Functional source code
Please check the source code comments
Bool Mirror_Magnify (Mat src) {/ * Image magnification: convex lens x = (dx / 2) * (sqrt (pow (dx, 2) + pow (dy, 2)) / r) + cx; y = (dy / 2) * (sqrt (pow (dx, 2) + pow (dy, 2)) / r) + cy * / Mat canvas = Mat::zeros (src.size (), src.type ()); / / canvas, regenerate image / / image center int cx = src.cols / 2; int cy = src.rows / 2; / / determine mirror size int radius = 200; / / Image pixel modification for (int I = 0; I
< src.rows; i++) { for (int j = 0; j < src.cols; j++) { //任意像素点到图像中心距离 int dx = j - cx; int dy = i - cy; //重新映射像素点位置 int x = (dx / 2)*(sqrt(pow(dx, 2) + pow(dy, 2)) / radius) + cx; int y = (dy / 2)*(sqrt(pow(dx, 2) + pow(dy, 2)) / radius) + cy; for (int c = 0; c < 3; c++) { //防止越界 if ((x >0 & & x
< src.cols) && (y >0 & & y
< src.rows)) { canvas.at(i, j)[c] = src.at(y, x)[c]; } } } } imshow("Mirror_Magnify", canvas); return true;}2.效果显示 二、凹透镜 制作凹透镜效果(将图像缩小)。根据网上查找的变换公式: 图像缩小:凹透镜 x = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * cos(atan2(dy, dx)) + cx; y = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * sin(atan2(dy, dx)) + cy; 1.功能源码 请查看源码注释 bool Mirror_Narrow(Mat src){ /* 图像缩小:凹透镜 x = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * cos(atan2(dy, dx)) + cx; y = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * sin(atan2(dy, dx)) + cy; */ Mat canvas = Mat::zeros(src.size(), src.type());//画布,重新生成哈哈图像 int compress = 12; //压缩强度 //图像中心 int cx = src.cols / 2; int cy = src.rows / 2; //图像像素修改 for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { //任意像素点到图像中心距离 int dx = j - cx; int dy = i - cy; //重新映射像素点位置 int x = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * cos(atan2(dy, dx)) + cx; int y = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * sin(atan2(dy, dx)) + cy; for (int c = 0; c < 3; c++) { //防止越界 if ((x >0 & & x
< src.cols) && (y >0 & & y
< src.rows)) { canvas.at(i, j)[c] = src.at(y, x)[c]; } } } } imshow("Mirror_Narrow", canvas); return true;}2.效果显示
Third, the source code # include#includeusing namespace std;using namespace cv;/* mirror realization principle: let the image pixel distortion, the pixel re-mapping assumption input image width w, high h. Image center point coordinates (cx,cy), distance from any pixel of the image to the center point dx= (x-cx), dy= (y-cy), transform radius r*/bool Mirror_Magnify (Mat src) {/ * Image magnification: convex lens x = (dx / 2) * (sqrt (pow (dx, 2) + pow (dy, 2) / r) + cx Y = (dy / 2) * (sqrt (pow (dx, 2) + pow (dy, 2) / r) + cy; * / Mat canvas = Mat::zeros (src.size (), src.type ()); / / canvas, regenerate image / / image center int cx = src.cols / 2; int cy = src.rows / 2 / / determine the size of the mirror int radius = 200; / / modify the image pixel for (int I = 0; I
< src.rows; i++) { for (int j = 0; j < src.cols; j++) { //任意像素点到图像中心距离 int dx = j - cx; int dy = i - cy; //重新映射像素点位置 int x = (dx / 2)*(sqrt(pow(dx, 2) + pow(dy, 2)) / radius) + cx; int y = (dy / 2)*(sqrt(pow(dx, 2) + pow(dy, 2)) / radius) + cy; for (int c = 0; c < 3; c++) { //防止越界 if ((x >0 & & x
< src.cols) && (y >0 & & y
< src.rows)) { canvas.at(i, j)[c] = src.at(y, x)[c]; } } } } imshow("Mirror_Magnify", canvas); return true;}bool Mirror_Narrow(Mat src){ /* 图像缩小:凹透镜 x = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * cos(atan2(dy, dx)) + cx; y = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * sin(atan2(dy, dx)) + cy; */ Mat canvas = Mat::zeros(src.size(), src.type());//画布,重新生成哈哈图像 int compress = 12; //压缩强度 //图像中心 int cx = src.cols / 2; int cy = src.rows / 2; //图像像素修改 for (int i = 0; i < src.rows; i++) { for (int j = 0; j < src.cols; j++) { //任意像素点到图像中心距离 int dx = j - cx; int dy = i - cy; //重新映射像素点位置 int x = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * cos(atan2(dy, dx)) + cx; int y = sqrt(sqrt(pow(dx, 2) + pow(dy, 2))) * compress * sin(atan2(dy, dx)) + cy; for (int c = 0; c < 3; c++) { //防止越界 if ((x >0 & & x
< src.cols) && (y >0 & & y < src.rows) {canvas.at (I, j) [c] = src.at (y, x) [c];} imshow ("Mirror_Narrow", canvas) Return true;} int main () {Mat src = imread ("test.jpg"); if (src.empty ()) {cout