您好,登錄后才能下訂單哦!
這篇文章主要介紹了C++中怎么使用OpenCV制作哈哈鏡圖像效果的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇C++中怎么使用OpenCV制作哈哈鏡圖像效果文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
制作凸透鏡效果(將圖像放大)。根據(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;
請(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()); //畫(huà)布,重新生成哈哈圖像 //圖像中心 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; }
制作凹透鏡效果(將圖像縮?。8鶕?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;
請(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());//畫(huà)布,重新生成哈哈圖像 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; }
#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()); //畫(huà)布,重新生成哈哈圖像 //圖像中心 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());//畫(huà)布,重新生成哈哈圖像 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制作哈哈鏡圖像效果”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“C++中怎么使用OpenCV制作哈哈鏡圖像效果”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。