C++,图像处理,实现图像平铺,截取

使用指针方式让文件读取变快
让绿色和紫红色

void work_1()
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\4a.jpg");//目标图像
	CImage image;
	image.Load(regionFilePath);
	int width = image.GetWidth();
	int height = image.GetHeight();
	int bpp = image.GetBPP();//图像深度
	int pitch = image.GetPitch();//图像一行字节数
	BYTE* pData = (BYTE*)image.GetBits();//返回指向图像左上角像素点
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);
			BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);
			BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);
			if (g > b&&g>r)//不能用swap交换,否则没有变化
			{
				*(pData + pitch * y + x * bpp / 8 + 0) = g;
				* (pData + pitch * y + x * bpp / 8 + 1) = b;
			}
		}
	}
	image.Save(finalFilePath);
}

将一个图片一分为四,存到不同路径

void work_2()
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath_1 = _T("D:\\41.jpg");//目标图像
	LPCTSTR finalFilePath_2 = _T("D:\\42.jpg");//目标图像
	LPCTSTR finalFilePath_3 = _T("D:\\43.jpg");//目标图像
	LPCTSTR finalFilePath_4 = _T("D:\\44.jpg");//目标图像
	CImage regionImage, finalImage_1, finalImage_2, finalImage_3, finalImage_4;
	regionImage.Load(regionFilePath);
	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度
	finalImage_1.Create(width / 2, height / 2, bpp);
	finalImage_2.Create(width / 2, height / 2, bpp);
	finalImage_3.Create(width / 2, height / 2, bpp);
	finalImage_4.Create(width / 2, height / 2, bpp);
	SetStretchBltMode(finalImage_1.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage_1.ReleaseDC();
	SetStretchBltMode(finalImage_2.GetDC(), COLORONCOLOR);
	finalImage_2.ReleaseDC();
	SetStretchBltMode(finalImage_3.GetDC(), COLORONCOLOR);
	finalImage_3.ReleaseDC();
	SetStretchBltMode(finalImage_4.GetDC(), COLORONCOLOR);//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真
	finalImage_4.ReleaseDC();
	regionImage.Draw(finalImage_1.GetDC(), 0, 0, width / 2, height / 2, 0, 0, width / 2, height / 2);//第一个参数表明目标图像句柄,2-5参数表述左上角xy坐标,目标图像的宽度和高度,6-9时源图像的xy和深度
	finalImage_1.ReleaseDC();
	regionImage.Draw(finalImage_2.GetDC(), 0, 0, width / 2, height / 2, width / 2, 0, width / 2, height / 2);
	finalImage_2.ReleaseDC();
	regionImage.Draw(finalImage_3.GetDC(), 0, 0, width / 2, height / 2, 0, height / 2, width / 2, height / 2);
	finalImage_3.ReleaseDC();
	regionImage.Draw(finalImage_4.GetDC(), 0, 0, width / 2, height / 2, width / 2, height / 2, width / 2, height / 2);
	finalImage_4.ReleaseDC();
	finalImage_1.Save(finalFilePath_1);
	finalImage_2.Save(finalFilePath_2);
	finalImage_3.Save(finalFilePath_3);
	finalImage_4.Save(finalFilePath_4);
}

图像拼接,将原图像拼接到一个图像上,上下左右,目标图像大小不变

void work_3()//图像截取
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\4b.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath);
	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度
	finalImage.Create(width, height, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真,
	regionImage.Draw(finalImage.GetDC(), 0, 0, width / 2, height / 2);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), width / 2, 0, width / 2, height / 2);
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), 0, height/2, width / 2, height / 2);
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), width / 2, height / 2, width / 2, height / 2);
	finalImage.ReleaseDC();
	finalImage.Save(finalFilePath);
}

输入n,m值,控制目标图像大小,实现图像的平铺,同时顺序是一彩一灰

void tranGrey()//生成灰色图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5grey.jpg");//目标图像
	CImage image;
	image.Load(regionFilePath);
	int width = image.GetWidth();
	int height = image.GetHeight();
	int bpp = image.GetBPP();//图像深度
	int pitch = image.GetPitch();//图像一行字节数
	BYTE* pData = (BYTE*)image.GetBits();//返回指向图像左上角像素点
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);
			BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);
			BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

			BYTE grey = (BYTE)round(r * 0.299 + g * 0.587 + b * 0.114);
			*(pData + pitch * y + x * bpp / 8 + 0) = grey;
			*(pData + pitch * y + x * bpp / 8 + 1) = grey;
			*(pData + pitch * y + x * bpp / 8 + 2) = grey; 
		}
	}
	image.Save(finalFilePath);
}
void work_4()
{
	int m, n;
	int cnt = 1;
	cin >> n >> m;
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5a.jpg");//目标图像
	LPCTSTR greyFilePath = _T("D:\\5grey.jpg");//源图像
	CImage regionImage, finalImage, regionGreyImage;
	tranGrey();//生成灰色图片
	regionImage.Load(regionFilePath); regionGreyImage.Load(greyFilePath);

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(width * n, height * m, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真
	
	for (int j = 0; j < height * m; j += height)
	{
		for (int i = 0; i < width * n; i += width)
		{
			if (cnt%2==1)//彩色
			{
				regionImage.Draw(finalImage.GetDC(), i, j, width, height, 0, 0, width, height);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
				finalImage.ReleaseDC();
				cnt++;
			}
			else//灰色
			{
				regionGreyImage.Draw(finalImage.GetDC(), i, j, width, height, 0, 0, width, height);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
				finalImage.ReleaseDC();
				cnt++;
			}
		}
	}
	finalImage.Save(finalFilePath);
}

将图像拉伸成5000*5000的大小

void work_5()//作业5,拉成5000*5000分辨率的图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5b.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath);

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(5000, 5000, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真

	regionImage.Draw(finalImage.GetDC(), 0, 0, 5000, 5000);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
	finalImage.ReleaseDC();
	finalImage.Save(finalFilePath);
}

将原图平铺成5000*5000的大小

void work_5()//作业5,拉成5000*5000分辨率的图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5b.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath);

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(5000, 5000, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真

	regionImage.Draw(finalImage.GetDC(), 0, 0, 5000, 5000);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
	finalImage.ReleaseDC();
	finalImage.Save(finalFilePath);
}

全部代码

#include<iostream>
#include<string.h>
#include<fstream>
#include<windows.h>
#include<direct.h>
#include<atlimage.h>
using namespace std;
void tranGrey()//生成灰色图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5grey.jpg");//目标图像
	CImage image;
	image.Load(regionFilePath);
	int width = image.GetWidth();
	int height = image.GetHeight();
	int bpp = image.GetBPP();//图像深度
	int pitch = image.GetPitch();//图像一行字节数
	BYTE* pData = (BYTE*)image.GetBits();//返回指向图像左上角像素点
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);
			BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);
			BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);

			BYTE grey = (BYTE)round(r * 0.299 + g * 0.587 + b * 0.114);
			*(pData + pitch * y + x * bpp / 8 + 0) = grey;
			*(pData + pitch * y + x * bpp / 8 + 1) = grey;
			*(pData + pitch * y + x * bpp / 8 + 2) = grey; 
		}
	}
	image.Save(finalFilePath);
}
void work_1()
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\4a.jpg");//目标图像
	CImage image;
	image.Load(regionFilePath);
	int width = image.GetWidth();
	int height = image.GetHeight();
	int bpp = image.GetBPP();//图像深度
	int pitch = image.GetPitch();//图像一行字节数
	BYTE* pData = (BYTE*)image.GetBits();//返回指向图像左上角像素点
	for (int x = 0; x < width; x++)
	{
		for (int y = 0; y < height; y++)
		{
			BYTE b = *(pData + pitch * y + x * bpp / 8 + 0);
			BYTE g = *(pData + pitch * y + x * bpp / 8 + 1);
			BYTE r = *(pData + pitch * y + x * bpp / 8 + 2);
			if (g > b&&g>r)
			{
				*(pData + pitch * y + x * bpp / 8 + 0) = g;
				* (pData + pitch * y + x * bpp / 8 + 1) = b;
			}
		}
	}
	image.Save(finalFilePath);
}
void work_2()
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath_1 = _T("D:\\41.jpg");//目标图像
	LPCTSTR finalFilePath_2 = _T("D:\\42.jpg");//目标图像
	LPCTSTR finalFilePath_3 = _T("D:\\43.jpg");//目标图像
	LPCTSTR finalFilePath_4 = _T("D:\\44.jpg");//目标图像
	CImage regionImage, finalImage_1, finalImage_2, finalImage_3, finalImage_4;
	regionImage.Load(regionFilePath);
	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度
	finalImage_1.Create(width / 2, height / 2, bpp);
	finalImage_2.Create(width / 2, height / 2, bpp);
	finalImage_3.Create(width / 2, height / 2, bpp);
	finalImage_4.Create(width / 2, height / 2, bpp);
	SetStretchBltMode(finalImage_1.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage_1.ReleaseDC();
	SetStretchBltMode(finalImage_2.GetDC(), COLORONCOLOR);
	finalImage_2.ReleaseDC();
	SetStretchBltMode(finalImage_3.GetDC(), COLORONCOLOR);
	finalImage_3.ReleaseDC();
	SetStretchBltMode(finalImage_4.GetDC(), COLORONCOLOR);//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真
	finalImage_4.ReleaseDC();
	regionImage.Draw(finalImage_1.GetDC(), 0, 0, width / 2, height / 2, 0, 0, width / 2, height / 2);//第一个参数表明目标图像句柄,2-5参数表述左上角xy坐标,目标图像的宽度和高度,6-9时源图像的xy和深度
	finalImage_1.ReleaseDC();
	regionImage.Draw(finalImage_2.GetDC(), 0, 0, width / 2, height / 2, width / 2, 0, width / 2, height / 2);
	finalImage_2.ReleaseDC();
	regionImage.Draw(finalImage_3.GetDC(), 0, 0, width / 2, height / 2, 0, height / 2, width / 2, height / 2);
	finalImage_3.ReleaseDC();
	regionImage.Draw(finalImage_4.GetDC(), 0, 0, width / 2, height / 2, width / 2, height / 2, width / 2, height / 2);
	finalImage_4.ReleaseDC();
	finalImage_1.Save(finalFilePath_1);
	finalImage_2.Save(finalFilePath_2);
	finalImage_3.Save(finalFilePath_3);
	finalImage_4.Save(finalFilePath_4);
}
void work_3()//图像截取
{
	LPCTSTR regionFilePath = _T("D:\\4.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\4b.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath);
	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度
	finalImage.Create(width, height, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真,
	regionImage.Draw(finalImage.GetDC(), 0, 0, width / 2, height / 2);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), width / 2, 0, width / 2, height / 2);
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), 0, height/2, width / 2, height / 2);
	finalImage.ReleaseDC();
	regionImage.Draw(finalImage.GetDC(), width / 2, height / 2, width / 2, height / 2);
	finalImage.ReleaseDC();
	finalImage.Save(finalFilePath);
}
void work_4()
{
	int m, n;
	int cnt = 1;
	cin >> n >> m;
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5a.jpg");//目标图像
	LPCTSTR greyFilePath = _T("D:\\5grey.jpg");//源图像
	CImage regionImage, finalImage, regionGreyImage;
	tranGrey();//生成灰色图片
	regionImage.Load(regionFilePath); regionGreyImage.Load(greyFilePath);

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(width * n, height * m, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真
	
	for (int j = 0; j < height * m; j += height)
	{
		for (int i = 0; i < width * n; i += width)
		{
			if (cnt%2==1)//彩色
			{
				regionImage.Draw(finalImage.GetDC(), i, j, width, height, 0, 0, width, height);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
				finalImage.ReleaseDC();
				cnt++;
			}
			else//灰色
			{
				regionGreyImage.Draw(finalImage.GetDC(), i, j, width, height, 0, 0, width, height);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
				finalImage.ReleaseDC();
				cnt++;
			}
		}
	}
	finalImage.Save(finalFilePath);
}
void work_5()//作业5,拉成5000*5000分辨率的图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5b.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath);

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(5000, 5000, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真

	regionImage.Draw(finalImage.GetDC(), 0, 0, 5000, 5000);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
	finalImage.ReleaseDC();
	finalImage.Save(finalFilePath);
}
void work_6()//作业6,平铺成5000*50000分辨率的图像
{
	LPCTSTR regionFilePath = _T("D:\\5.jpg");//源图像
	LPCTSTR finalFilePath = _T("D:\\5c.jpg");//目标图像
	CImage regionImage, finalImage;
	regionImage.Load(regionFilePath); 

	int width = regionImage.GetWidth();
	int height = regionImage.GetHeight();
	int bpp = regionImage.GetBPP();//图像深度

	finalImage.Create(5000, 5000, bpp);
	SetStretchBltMode(finalImage.GetDC(), COLORONCOLOR);//设置图像的清晰模式
	finalImage.ReleaseDC();//注意每次GetDc都要RealeaseDC(),Create需要指定长宽和深度,COLORONCOLOR模式保证图像缩放时不会产生失真

	for (int j = 0; j < 5000; j+=height)
	{
		for (int i = 0; i < 5000; i+=width)
		{
			regionImage.Draw(finalImage.GetDC(), i, j, width, height, 0, 0, width, height);//第一个参数表明目标图像句柄,2-5参数表述源图像内容到目标图像位置,6-9就是源图像某块到目标图像
			finalImage.ReleaseDC();
		}
	}
	finalImage.Save(finalFilePath);
}
int main()
{
	work_1();//把草地,绿叶改成紫红色
	work_2();//图像截取,截取四个位置,把图片分成四块
	work_3();//把图片放到不同位置,合成原图一样的大小
	work_4();//输入n.m平铺成彩灰交替的图片
	work_5();//把图片拉成5000*5000的图片
	work_6();//用一张图片平铺5000*5000的图片
	return 0;
}
//LPCTSTR
//紫红色 R=255;G=0;B=255;黑 0 白 255 Grey 0-255 黑——白 R=B=G灰度 Gray=R*0.299+G*0.587+B*0.114
//COLORREF-unsign long  int GetRValue(COLORREF &)   COLORREF RGB(int R,int G,int B)
//BYTE 0~255整数,unsigned char

//另外C++的图像处理博客也太少了叭~

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值