C++ OpenCV制作哈哈鏡圖像效果
前言
本文將使用OpenCV C++ 制作哈哈鏡圖像。其實原理很簡單,就是讓圖像像素扭曲,將像素重新進(jìn)行映射。
一、凸透鏡

制作凸透鏡效果(將圖像放大)。根據(jù)網(wǎng)上查找的變換公式:
圖像放大:凸透鏡
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.功能源碼
請查看源碼注釋
bool Mirror_Magnify(Mat src)
{
/*
圖像放大:凸透鏡
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()); //畫布,重新生成哈哈圖像
//圖像中心
int cx = src.cols / 2;
int cy = src.rows / 2;
//決定哈哈鏡大小
int radius = 200;
//圖像像素修改
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
//任意像素點(diǎn)到圖像中心距離
int dx = j - cx;
int dy = i - cy;
//重新映射像素點(diǎn)位置
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<Vec3b>(i, j)[c] = src.at<Vec3b>(y, x)[c];
}
}
}
}
imshow("Mirror_Magnify", canvas);
return true;
}
2.效果顯示

二、凹透鏡
制作凹透鏡效果(將圖像縮小)。根據(jù)網(wǎng)上查找的變換公式:
圖像縮小:凹透鏡
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)
{
/*
圖像縮?。喊纪哥R
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; //壓縮強(qiáng)度
//圖像中心
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++)
{
//任意像素點(diǎn)到圖像中心距離
int dx = j - cx;
int dy = i - cy;
//重新映射像素點(diǎn)位置
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<Vec3b>(i, j)[c] = src.at<Vec3b>(y, x)[c];
}
}
}
}
imshow("Mirror_Narrow", canvas);
return true;
}
2.效果顯示

三、源碼
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/*
哈哈鏡實現(xiàn)原理:讓圖像像素扭曲,將像素重新進(jìn)行映射
假設(shè)輸入圖像寬w,高h(yuǎn)。圖像中心點(diǎn)坐標(biāo)(cx,cy),圖像任意像素點(diǎn)(x,y)到中心點(diǎn)距離 dx=(x-cx),dy=(y-cy),變換半徑r
*/
bool Mirror_Magnify(Mat src)
{
/*
圖像放大:凸透鏡
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()); //畫布,重新生成哈哈圖像
//圖像中心
int cx = src.cols / 2;
int cy = src.rows / 2;
//決定哈哈鏡大小
int radius = 200;
//圖像像素修改
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
//任意像素點(diǎn)到圖像中心距離
int dx = j - cx;
int dy = i - cy;
//重新映射像素點(diǎn)位置
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<Vec3b>(i, j)[c] = src.at<Vec3b>(y, x)[c];
}
}
}
}
imshow("Mirror_Magnify", canvas);
return true;
}
bool Mirror_Narrow(Mat src)
{
/*
圖像縮?。喊纪哥R
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; //壓縮強(qiáng)度
//圖像中心
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++)
{
//任意像素點(diǎn)到圖像中心距離
int dx = j - cx;
int dy = i - cy;
//重新映射像素點(diǎn)位置
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<Vec3b>(i, j)[c] = src.at<Vec3b>(y, x)[c];
}
}
}
}
imshow("Mirror_Narrow", canvas);
return true;
}
int main()
{
Mat src = imread("test.jpg");
if (src.empty())
{
cout << "No Image!" << endl;
system("pause");
return -1;
}
Mirror_Magnify(src);
Mirror_Narrow(src);
imshow("test", src);
waitKey(0);
system("pause");;
return 0;
}
到此這篇關(guān)于C++ OpenCV制作哈哈鏡圖像效果的文章就介紹到這了,更多相關(guān)C++ OpenCV哈哈鏡圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

