C++ 读取ppm文件并转换为灰度或反转
我似乎无法读取文件,我希望用户输入文件名,比如“wallpaper”,然后将其转换为文件名“wallpaper.ppm”。我想获取这个输入,将数字转换为向量,然后根据用户输入将文件转换为灰度或反转。谢谢C++ 读取ppm文件并转换为灰度或反转,c++,vector,fstream,ppm,C++,Vector,Fstream,Ppm,我似乎无法读取文件,我希望用户输入文件名,比如“wallpaper”,然后将其转换为文件名“wallpaper.ppm”。我想获取这个输入,将数字转换为向量,然后根据用户输入将文件转换为灰度或反转。谢谢 // A5.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <cmath> #include <iostream> #in
// A5.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <array>
using namespace std;
int main()
{
struct Pixel {
int Red;
int Green;
int Blue;
};
vector <vector< Pixel> > image;
string fileType;
int height{}, width{}, maxColor{};
string p3;
cout << "Enter the name of the file:";
cin >> fileType;
string fileTypeActual = fileType + ".ppm";
ifstream myImage(fileTypeActual);
if (myImage.fail()) {
cerr << "Fail";
return 1;
}
while (!myImage.eof()) {
string p3;
myImage >> p3 >> width >> height >> maxColor;
int i, j;
for (i = 0; i < height; i++) {
vector<Pixel>rowofPixels;
for (j = 0; j < width; j++) {
Pixel p;
myImage >> p.Red;
myImage >> p.Green;
myImage >> p.Blue;
rowofPixels.push_back(p);
}
image.push_back(rowofPixels);
}
};
myImage.close();
int userChoice;
cout << "Would you like to invert the colors or convert to greyscale? Type 1 for greyscale and 2 for color inversion";
cin >> userChoice;
if (userChoice == 1) {
//begin greyscale conversion
string fileGray;
fileGray = fileType + "_grayscale.ppm";
ofstream imageOut(fileGray);
if (imageOut.fail()) {
cerr << "Fail";
return 1;
}
struct greyPixel {
double redGrey;
double blueGrey;
double greenGrey;
};
vector <vector< greyPixel > > greyScale;
//gray = 0.2989 * Red + 0.5870 * Green + 0.1140 * Blue
for (int i = 0; i < height; i++) {
vector<greyPixel>rowofGrey;
for (int j = 0; j < width; j++) {
int pixelRed = ((image.at(i)).at(j)).Red;
int pixelBlue = ((image.at(i)).at(j)).Blue;
int pixelGreen = ((image.at(i)).at(j)).Green;
greyPixel g;
g.redGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
g.blueGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
g.greenGrey = 0.2989 * pixelRed + 0.5870 * pixelGreen + 0.1140 * pixelBlue;
rowofGrey.push_back(g);
}
greyScale.push_back(rowofGrey);
}
imageOut.close();
}
else if (userChoice == 2) {
//begin color inverson
string fileInvert;
fileInvert = fileType + "_inverted.ppm";
ofstream imageInv(fileInvert);
if (imageInv.fail()) {
cerr << "Fail";
return 1;
}
struct invertPixel {
int redInvert;
int blueInvert;
int greenInvert;
};
vector <vector< invertPixel > > invert;
for (int i = 0; i < height; i++) {
vector<invertPixel>rowofInvert;
for (int j = 0; j < width; j++) {
int pixelRed = ((image.at(i)).at(j)).Red;
int pixelBlue = ((image.at(i)).at(j)).Blue;
int pixelGreen = ((image.at(i)).at(j)).Green;
invertPixel inv;
inv.blueInvert = maxColor - pixelBlue;
inv.greenInvert = maxColor - pixelGreen;
inv.redInvert = maxColor - pixelRed;
rowofInvert.push_back(inv);
}
invert.push_back(rowofInvert);
}
imageInv.close();
}
return 0;
}
//A5.cpp:此文件包含“main”函数。程序执行从那里开始并结束。
//
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
结构像素{
红色;
绿色;
蓝色;
};
矢量>图像;
字符串文件类型;
整数高度{},宽度{},最大颜色{};
字符串p3;
cout>文件类型;
字符串fileTypeActual=fileType+“.ppm”;
ifstream myImage(fileTypeActual);
if(myImage.fail()){
cerr>p3>>宽度>>高度>>最大颜色;
int i,j;
对于(i=0;i>红色;
我的图像>>绿色;
我的图像>>蓝色;
像素行。推回(p);
}
图像。向后推(像素行);
}
};
myImage.close();
int用户选择;
用户选择;
if(userChoice==1){
//开始灰度转换
线灰色;
fileGray=fileType+“_grayscale.ppm”;
流图像输出(文件灰色);
if(imageOut.fail()){
cerr灰度;
//灰色=0.2989*红色+0.5870*绿色+0.1140*蓝色
对于(int i=0;i
您能将代码示例简化为实际出现错误的部分吗?它被称为a,这将使我们更容易帮助您。您是说打开文件会出现错误,还是在下游发生错误?嘿,simon,谢谢您的回答。我无法打开文件,代码运行时没有错误,并返回1,表示错误告诉我它运行到完成。我期望的问题是我没有打开文件并正确读取它输入文件在哪里?当你说“代码运行没有错误,并返回1”时,发布的代码有两个if(/*..*/.fail()){cerr Bob_uuuuuu所说的。但是:在ifstream myImage行上放置一个断点(fileTypeActual)
,逐步检查代码并尝试找出实际发生的情况。实际遇到的是哪个返回1
语句?