C++ 将PPM图像转换为灰度C++;
尝试通过索引包含像素数据的指针,将PPM图像转换为灰度:C++ 将PPM图像转换为灰度C++;,c++,pointers,grayscale,ppm,C++,Pointers,Grayscale,Ppm,尝试通过索引包含像素数据的指针,将PPM图像转换为灰度: void PPMObject::greyScale() { const float r = 0.299F; const float g = 0.587F; const float b = 0.114F; int size = this->width * this->height * 3; for (int i = 0; i < size; i++) { t
void PPMObject::greyScale()
{
const float r = 0.299F;
const float g = 0.587F;
const float b = 0.114F;
int size = this->width * this->height * 3;
for (int i = 0; i < size; i++)
{
this->m_Ptr[i] = (this->m_Ptr[i] * r) + (this->m_Ptr[i] * g) + (this->m_Ptr[i] * b);
this->m_Ptr[i+1] = (this->m_Ptr[i+1] * r) + (this->m_Ptr[i+1] * g) + (this->m_Ptr[i+1] * b);
this->m_Ptr[i+2] = (this->m_Ptr[i+2] * r) + (this->m_Ptr[i+2] * g) + (this->m_Ptr[i+2] * b);
}
}
我把数据写如下:
ostream& operator <<(ostream &outputStream, const PPMObject &other)
{
outputStream << other.magicNum << " "
<< other.width << " "
<< other.height << " "
<< other.maxColorValue << " "
;
outputStream.write(other.m_Ptr, other.width * other.height * 3);
return outputStream;
}
ostream&operator在greyScale()函数中,每次遍历循环时都需要将i增加3,因为每个像素占用3个字节,每次处理一个像素:
for (int i = 0; i < size; i+=3)
简化为:
this->m_Ptr[i] = (this->m_Ptr[i] * 1.0F);
正确的公式如下所示(忽略强制转换并假设数据按RGB顺序排列):
for(int i=0;im_Ptr[i]*r)+(this->m_Ptr[i+1]*g)+(this->m_Ptr[i+2]*b);
此->m_Ptr[i]=灰度标度值;
此->m_Ptr[i+1]=灰度标度值;
此->m_Ptr[i+2]=灰度标度值;
}
我今天早些时候编写的以下程序成功地将彩色图像(.ppm)文件转换为灰度图像(.ppm)文件。我添加了很多评论,所以你可以很容易地跟随
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
//File input and output streams
ifstream fin("colorImage.ppm");
ofstream fout("grayscaleImage.ppm");
//Check if input file was successfully opened
if (!fin) {
cout << "Error - Nonexistent Image (.ppm) File" << endl;
system("pause");
return -1; // Error exit
}
//Declare necessary variables
string magic_number;
int pixel_per_row, num_rows, color_depth, red, green, blue;
//Read in values for the following variables from input file
fin >> magic_number >> pixel_per_row >> num_rows >> color_depth;
//Write the following variables to the output file
fout << magic_number << endl << pixel_per_row << " " << num_rows << endl << color_depth << endl;
//Read in input file until file end putting values into appropriate variables
while (fin >> red >> green >> blue) {
red = green = blue = int(0.3 * red + 0.59 * green + 0.11 * blue); //Covert each pixel to grayscale
fout << red << endl << green << endl << blue << endl; //Write converted values to output file
}
//Close files
fin.close();
fout.close();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
//文件输入和输出流
ifstream fin(“colorImage.ppm”);
流fout(“grayscaleImage.ppm”);
//检查输入文件是否已成功打开
如果(!fin){
cout magic_number>>每行像素数>>行数>>颜色深度;
//将以下变量写入输出文件
谢谢!如果我有更多的经验,我应该自己抓住这些错误。
this->m_Ptr[i] = (this->m_Ptr[i] * 1.0F);
for (int i = 0; i < size; i+=3)
{
float greyscaleValue = (this->m_Ptr[i] * r) + (this->m_Ptr[i+1] * g) + (this->m_Ptr[i+2] * b);
this->m_Ptr[i] = greyscaleValue;
this->m_Ptr[i+1] = greyscaleValue;
this->m_Ptr[i+2] = greyscaleValue;
}
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
//File input and output streams
ifstream fin("colorImage.ppm");
ofstream fout("grayscaleImage.ppm");
//Check if input file was successfully opened
if (!fin) {
cout << "Error - Nonexistent Image (.ppm) File" << endl;
system("pause");
return -1; // Error exit
}
//Declare necessary variables
string magic_number;
int pixel_per_row, num_rows, color_depth, red, green, blue;
//Read in values for the following variables from input file
fin >> magic_number >> pixel_per_row >> num_rows >> color_depth;
//Write the following variables to the output file
fout << magic_number << endl << pixel_per_row << " " << num_rows << endl << color_depth << endl;
//Read in input file until file end putting values into appropriate variables
while (fin >> red >> green >> blue) {
red = green = blue = int(0.3 * red + 0.59 * green + 0.11 * blue); //Covert each pixel to grayscale
fout << red << endl << green << endl << blue << endl; //Write converted values to output file
}
//Close files
fin.close();
fout.close();
return 0;
}