//一个使用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;

}

相关推荐
iceslime12 分钟前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
虾球xz24 分钟前
CppCon 2015 学习:3D Face Tracking and Reconstruction using Modern C++
开发语言·c++·学习·3d
small_wh1te_coder1 小时前
c语言超详细知识点总结 1500行手写源码 持续更新中ing 从25年5月到6月5日
c++·c
SteveDraw3 小时前
C++动态链接库封装,供C#/C++ 等编程语言使用——C++动态链接库概述(总)
开发语言·c++·c#·封装·动态链接库
十五年专注C++开发3 小时前
设计模式之单例模式(二): 心得体会
开发语言·c++·单例模式·设计模式
?!7143 小时前
算法打卡第18天
c++·算法
zh_xuan4 小时前
c++ std::pair
开发语言·c++
CodeWithMe4 小时前
【C/C++】EBO空基类优化介绍
开发语言·c++
k要开心4 小时前
从C到C++语法过度1
开发语言·c++
whoarethenext5 小时前
使用 C/C++的OpenCV 实时播放火柴人爱心舞蹈动画
c语言·c++·opencv