C++,实现图像的翻转和变色

C++,实现图像的翻转和变色,实践周纪念一下
主要上下左右,180度翻转,和其他变色
需要用到头文件 atlimage.h,类CImage,COLORREF
编译器:VS,安装ATL for 142生成工具
字符集设置为宽字符,或者利用LPCTSTR直接定义,不过 dev-C++可以不用考虑定义LPCTSTR,也无需设置,直接使用,不过就是不能用atlimage.h这个头文件是了,基本的文件输入输出没问题
修改的图文件都是jpg格式

左右颠倒

void reverseRGB(COLORREF& c1, COLORREF& c2)//左右颠倒上下颠倒
{
	c1 = RGB(GetRValue(c2), GetGValue(c2), GetBValue(c2));
	c2 = RGB(GetRValue(c1) , GetGValue(c1) , GetBValue(c1));
}
void Work_3()//实现左右颠倒
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2d.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width/2; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(width-x-1, y);
			reverseRGB(c1, c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(width - x - 1,y, c2);
		}
	}
	regionmage.Save(finalFilePath);
}

上下颠倒

void reverseRGB(COLORREF& c1, COLORREF& c2)//左右颠倒上下颠倒
{
	c1 = RGB(GetRValue(c2), GetGValue(c2), GetBValue(c2));
	c2 = RGB(GetRValue(c1) , GetGValue(c1) , GetBValue(c1));
}
void Work_4()//实现上下颠倒
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2e.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height/2; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(x, height-y-1);
			reverseRGB(c1,c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(x, height - y - 1, c2);
		}
	}
	regionmage.Save(finalFilePath);
}

180度顺势针翻转(请指正,有瑕疵)

void reverseRGB(COLORREF& c1, COLORREF& c2)//左右颠倒上下颠倒
{
	c1 = RGB(GetRValue(c2), GetGValue(c2), GetBValue(c2));
	c2 = RGB(GetRValue(c1) , GetGValue(c1) , GetBValue(c1));
}
void Work_5()//实现180度翻转
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2f.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height ; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(width-1-x, height-1-y);
			reverseRGB(c1, c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(width - 1 - x, height - 1 - y,c2);
		}
	}
	regionmage.Save(finalFilePath);
}

只将黄色变为红色(原图涉及人脸的颜色,所以要避免人脸的肤色变换)

void yellowRGB(COLORREF& c)//黄色气球变红色
{
	int r = GetRValue(c);
	int g = GetGValue(c);
	int b = GetBValue(c);
	if (r>150&&g>150&&r>=b+20&g>=b+20)
	{
		g = b;
	}
	c = RGB(r, g, b);
}
void Work_6()//黄色变红色
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2g.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c = regionmage.GetPixel(x, y);
			yellowRGB(c);
			regionmage.SetPixel(x, y, c);
		}
	}
	regionmage.Save(finalFilePath);
}

全部代码

#include
#include
#include
#include
#include
#include
using namespace std;
void swapRGB(COLORREF& c)//交换RG,GB,BR
{
	int r = GetRValue(c);
	int g = GetGValue(c);
	int b = GetBValue(c);
	int t = r;
	r = g;
	g = b;
	b = t;
	c = RGB(r, g, b);
}
void tranRGB(COLORREF& c)//灰度反色
{
	int r = GetRValue(c);
	int g = GetGValue(c);
	int b = GetBValue(c);
	int Grey = 255 - (r * 0.299 + g * 0.587 + b * 0.114);
	c = RGB(Grey, Grey, Grey);
}
void reverseRGB(COLORREF& c1, COLORREF& c2)//左右颠倒上下颠倒
{
	c1 = RGB(GetRValue(c2), GetGValue(c2), GetBValue(c2));
	c2 = RGB(GetRValue(c1) , GetGValue(c1) , GetBValue(c1));
}
void yellowRGB(COLORREF& c)//黄色气球变红色
{
	int r = GetRValue(c);
	int g = GetGValue(c);
	int b = GetBValue(c);
	if (r>150&&g>150&&r>=b+20&g>=b+20)
	{
		g = b;
	}
	c = RGB(r, g, b);
}
void purpleredRGB(COLORREF& c)//绿色气球变紫红色
{
	int r = GetRValue(c);
	int g = GetGValue(c);
	int b = GetBValue(c);
	if (g>100&&g>r+10&&g>b+10)
	{
		swap(r, g);
	}
	c = RGB(r, g, b);
}
void Work_1()//实现源图像到目标图像转换,RGB互换
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2b.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c = regionmage.GetPixel(x, y);
			swapRGB(c);
			regionmage.SetPixel(x, y, c);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_2()//实现灰度反色图像
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2c.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c = regionmage.GetPixel(x, y);
			tranRGB(c);
			regionmage.SetPixel(x, y, c);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_3()//实现左右颠倒
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2d.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width/2; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(width-x-1, y);
			reverseRGB(c1, c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(width - x - 1,y, c2);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_4()//实现上下颠倒
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2e.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height/2; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(x, height-y-1);
			reverseRGB(c1,c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(x, height - y - 1, c2);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_5()//实现180度翻转
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2f.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height ; y++)
		{
			COLORREF c1 = regionmage.GetPixel(x, y);
			COLORREF c2 = regionmage.GetPixel(width-1-x, height-1-y);
			reverseRGB(c1, c2);
			regionmage.SetPixel(x, y, c1);
			regionmage.SetPixel(width - 1 - x, height - 1 - y,c2);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_6()//黄色变红色
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2g.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c = regionmage.GetPixel(x, y);
			yellowRGB(c);
			regionmage.SetPixel(x, y, c);
		}
	}
	regionmage.Save(finalFilePath);
}
void Work_7()//绿色变紫红色
{
	LPCTSTR regionFilePath = _T("2a.jpg");//源文件路径
	LPCTSTR finalFilePath = _T("2h.jpg");//目标图像路径
	CImage regionmage;
	regionmage.Load(regionFilePath);
	int width = regionmage.GetWidth();
	int height = regionmage.GetHeight();
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			COLORREF c = regionmage.GetPixel(x, y);
			purpleredRGB(c);
			regionmage.SetPixel(x, y, c);
		}
	}
	regionmage.Save(finalFilePath);
}
void Solve()//所有题目的集合,1到7
{
	Work_1();
	Work_2();
	Work_3();
	Work_4();
	Work_5();
	Work_6();
	Work_7();
}
int main()
{
	Solve();//显示结果
	return 0;
}

有问题请指正丫

你可能感兴趣的:(笔记,其他)