C++ OpenCV制作哈哈鏡圖像效果
前言
本文將使用OpenCV C++ 制作哈哈鏡圖像。其實(shí)原理很簡(jiǎn)單,就是讓圖像像素扭曲,將像素重新進(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.功能源碼
請(qǐng)查看源碼注釋
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)上查找的變換公式:
圖像縮?。喊纪哥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;
1.功能源碼
請(qǐng)查看源碼注釋
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;
/*
哈哈鏡實(shí)現(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)
{
/*
圖像縮小:凹透鏡
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言靜態(tài)動(dòng)態(tài)兩版本通訊錄實(shí)戰(zhàn)源碼
這篇文章主要為大家?guī)?lái)了C語(yǔ)言實(shí)現(xiàn)靜態(tài)動(dòng)態(tài)兩版本的通訊錄實(shí)戰(zhàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
C/C++ 凸多邊形求對(duì)角線交點(diǎn)的示例代碼
這篇文章主要介紹了C/C++ 凸多邊形求對(duì)角線交點(diǎn)的示例代碼,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
C語(yǔ)言如何利用異或進(jìn)行兩個(gè)值的交換詳解
最近在工作中遇到了兩個(gè)值交換的需求,發(fā)現(xiàn)自己對(duì)異或有些忘記,所以索性寫出來(lái),方便以后需要的時(shí)候參考學(xué)習(xí),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言如何利用異或進(jìn)行兩個(gè)值的交換的相關(guān)資料,需要的朋友可以參考下。2017-09-09
C++數(shù)位DP復(fù)雜度統(tǒng)計(jì)數(shù)字問(wèn)題示例詳解
這篇文章主要為大家介紹了利用C++數(shù)位DP的復(fù)雜度來(lái)統(tǒng)計(jì)數(shù)字問(wèn)題的示例實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升值加薪2021-11-11
關(guān)于C++的強(qiáng)制類型轉(zhuǎn)換淺析
C++的強(qiáng)制類型轉(zhuǎn)換是我們?cè)谌粘i_發(fā)中經(jīng)常會(huì)遇到的,下面這篇文章主要給大家介紹了關(guān)于C++強(qiáng)制類型轉(zhuǎn)換的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
C++實(shí)現(xiàn)簡(jiǎn)單職工管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++職工管理系統(tǒng)實(shí)訓(xùn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
Linux下C語(yǔ)言的fork()子進(jìn)程函數(shù)用法及相關(guān)問(wèn)題解析

