//一个使用CImage类将彩色jpeg图像转换成灰度图像的小程序

//本小程序使用VS2022编程环境,在C++空项目中使用CImage类将彩色jpeg图像转换成灰度图像

//相关代码及注释如下:

#include<iostream>

#include<atlimage.h> //处理图像类对象必须包含的头文件

#include<string.h>

using namespace std;

void ColorToGray(LPCTSTR srcFilePath, LPCTSTR destFilePath)

{

CImage srcImage; //声明图像类对象

srcImage.Load(srcFilePath); //将源图像载入

int width = srcImage.GetWidth(); //获得源图像像素宽度值

int height = srcImage.GetHeight();//获得源图像像素高度值

int bpp = srcImage.GetBPP();

int pitch = srcImage.GetPitch(); //每行字节数

BYTE* pImage = (BYTE*)srcImage.GetBits();//获取图像起始位置指针,GetBits返回的是未定类型,强转一下

cout << "源图像相关信息" << endl;

cout << "图像宽度:" << width << "像素" << endl;

cout << "图像高度:" << height << "像素" << endl;

cout << "分 辨 率:" << width * height / 10000 << "万像素" << endl;

cout << "位 深 度:" << bpp << endl;

system("pause");

for (int x = 0; x < width; x++) //双重循环遍历各像素点

{

for (int y = 0; y < height; y++)

{

BYTE r = *(pImage + pitch * y + x * bpp / 8 + 0);

BYTE g = *(pImage + pitch * y + x * bpp / 8 + 1);

BYTE b = *(pImage + pitch * y + x * bpp / 8 + 2);

//这里指针移动不是通过pImage++自增移动,而是始终以首地址为基准,通过加每行字节(GetPitch)乘行数,

//再加指定位置像素所在列位置(通过GetBPP/8+0/1/2得到,GetBPP得到的是位数值通过除8的到字节数),还

//得加上r、g、b位置。因为像素每行末尾可能存在空字节,所以这种方法比使用pImage++的方法简单。

BYTE gray = round(r * 0.299 + g * 0.587 + b * 0.114);

//彩色图像转换成灰度图像的转换公式

*(pImage + pitch * y + x * bpp / 8 + 0) = gray;

*(pImage + pitch * y + x * bpp / 8 + 1) = gray;

*(pImage + pitch * y + x * bpp / 8 + 2) = gray;

//这里实际是伪转换,图像的大小没有改变,只是r、g、b三个字节都使用相同颜色值。

}

}

srcImage.Save(destFilePath);

//保存转换成灰度的图形

}

int main()

{

LPCTSTR srcFilePath = _T("C:\\Users\\Administrator\\Desktop\\1111.jpg"); //源文件

LPCTSTR destFilePath = _T("C:\\Users\\Administrator\\Desktop\\2222.jpg"); //目标文件

ColorToGray(srcFilePath, destFilePath);

return 0;

}

相关推荐
端平入洛21 小时前
auto有时不auto
c++
哇哈哈20212 天前
信号量和信号
linux·c++
多恩Stone2 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马2 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝2 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc2 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼2 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛
yyjtx2 天前
DHU上机打卡D31
开发语言·c++·算法
czxyvX2 天前
020-C++之unordered容器
数据结构·c++
会编程的土豆2 天前
2.25 做题
数据结构·c++·算法