亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++ OpenCV制作哈哈鏡圖像效果

 更新時(shí)間:2022年01月10日 12:04:46   作者:Zero___Chen  
這篇文章主要介紹了使用OpenCV C++ 制作哈哈鏡圖像特效。其原理就是讓圖像像素扭曲,將像素重新進(jìn)行映射。感興趣的可以跟隨小編一起試一試

前言

本文將使用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()); //畫(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;
}

2.效果顯示

二、凹透鏡

制作凹透鏡效果(將圖像縮?。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;

1.功能源碼

請(qǐng)查看源碼注釋

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;
}

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()); //畫(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)
{
	/*
	圖像縮?。喊纪哥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;
}


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)文章

  • Linux下C語(yǔ)言的fork()子進(jìn)程函數(shù)用法及相關(guān)問(wèn)題解析

    Linux下C語(yǔ)言的fork()子進(jìn)程函數(shù)用法及相關(guān)問(wèn)題解析

    fork()函數(shù)在Linux下可以用于產(chǎn)生一個(gè)子進(jìn)程,這里我們挑選了兩個(gè)fork相關(guān)的面試題,來(lái)看一下Linux下C語(yǔ)言的fork()子進(jìn)程函數(shù)用法及相關(guān)問(wèn)題解析
    2016-06-06
  • 最新評(píng)論