首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编辑图片的值

问如何编辑图片的值
EN

Stack Overflow用户
提问于 2015-02-08 17:25:45
回答 2查看 130关注 0票数 0

我在处理文件时遇到了一些麻烦。这就是我要实现的目标。我试图通过取蓝色值(每三个值)的倒数来过滤PPM图像。我可以成功地打开和写入文件,但我遇到了一些问题。在我的while (myfile.good())循环中,我认为只有最后3个数字被赋值给变量r、g和b。我试图做的是将第一个(共三个)值赋给变量r,每隔一次赋值给g,每隔三个值赋值给b。但是,我想取255 -当前的b值,并将其设置为新的b值以应用于过滤器。我是否必须创建3个单独的文件(每个变量一个),然后将它们全部打开,然后将它们写入一个4个文件中,作为最终副本?或者有没有办法将它们全部复制并赋值给一个变量?任何帮助都是非常感谢的。我是c++的新手,所以请原谅我。谢谢你的帮助。

我尝试使用的值示例:http://imgur.com/H6EDFIq

代码语言:javascript
复制
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
    char filename[50];
    ifstream myfile;
    cin.getline(filename, 50);
    myfile.open(filename);

    if(!myfile.is_open())
    {
        cout << "File cannot load.";
    }

    char r [50];
    char g [50];
    char b [50];
    myfile >> r >> g >> b;

   while (myfile.good())
    {
        myfile >> r >> g >> b;
    }

myfile.close();

ofstream myfile2;
myfile2.open(filename);


//this is just to test to see what gets written onto the file
//for (int a=0; a<20; a++)
//{
    // ** Is this even allowed??  int r = 255 - r;
    //myfile2 << r << " " << g << " " << b;
//}

myfile2.close();

return 0;
}
EN

回答 2

Stack Overflow用户

发布于 2015-02-08 17:50:47

您需要检查作为.ppm图像第一行的.ppm标头。查看.ppm幻数,无论是P3还是P6,你都需要检查一下。第二行是图像的尺寸,所以你也需要考虑到这一点。

这是我之前做的工作,所以你可以得到一个想法。它可能不会立即生效,所以只需读一读。

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>

int main() {
std::string filename = //whatever your file name is
std::ifstream input(filename.c_str(), std::ios::in | std::ios::binary);
if (input.is_open()) {
    std::string line;
    std::getline(input, line);
    if (line != "P6" || line != "P3" ) { //you're going to want to check if you're using P3 or P6
        //print out errors
    }

    std::stringstream image_dimensions(line);

    try {
        image_dimensions >> w //your width variable if you want to store it here
        image_dimensions >> h //your height variable if you want to store it here
    } catch (std::exception &e) {
        std::cout << "Format error found in header " << e.what() << std::endl;
        return;
    }

    int size = w*h;

std::getline(input, line); 
std::stringstream max_value(line); //max colour value of the image

//you can initialise the vectors here if you want
std::vector<unsigned char> r;
std::vector<unsigned char> g;
std::vector<unsigned char> b;

 //we know it max capacity so reserve that size for the vectors
 r.reserve(size);
 g.reserve(size);
 b.reserve(size);

    char read_rgb;
    for (unsigned int i = 0; i < size; ++i) {
        input.read(&read_rgb, 1);
        r[i] = (unsigned char) read_rgb;
        input.read(&read_rgb, 1);
        g[i] = (unsigned char) read_rgb;
        input.read(&read_rgb, 1);
        b[i] = (unsigned char) read_rgb;
    }
}
input.close();
}

您将希望将r,g,b存储为您选择的数组。一旦你这样做了,你就可以迭代B的数组,编辑它来应用你的过滤器,然后把它写到ppm文件中。

此外,对于错误处理,您始终可以使用记事本或Notepad++打开.ppm文件并读取图像。

票数 0
EN

Stack Overflow用户

发布于 2015-02-09 08:35:22

除非您有特殊需要将数据存储在内存中,否则只需在读取数据时将数据写入到新文件中会更简单。

这是一个读取具有8位颜色条目的P3 ppm的示例,根据您的请求反转蓝色通道,然后使用该数据写入一个新文件。它不会做大量的错误检查,但我不想让它比现在更长。你会想要添加你自己的文件名,提示等等。

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream inFile("lena.ppm");
    if(!inFile)
    {
        std::cerr << "Could not open input file.\n";
        return -1;
    }

    std::string type;
    std::string comment;
    int width = 0, height = 0, colors = 0;

    std::getline(inFile, type);
    if(type != "P3")
    {
        std::cerr << "File is not a P3 format PPM.\n";
        return -1;
    }
    if(inFile.peek() == '#')
    {
        std::getline(inFile, comment);
    }
    inFile >> width >> height >> colors;

    std::ofstream outFile("lena2.ppm");
    if(!outFile)
    {
        std::cerr << "Could not open output file.\n";
        return -1;
    }

    outFile << type << "\n";
    if(!comment.empty())
    {
        outFile << comment << "\n";
    }
    outFile << width << " " << height << "\n" << colors << "\n";
    for(int y = 0; y < height; ++y)
    {
        for(int x = 0; x < width; ++x)
        {
            int r = 0, g = 0, b = 0;
            if(!(inFile >> r >> g >> b))
            {
                std::cerr << "File ended early.\n";
                return -1;
            }
            b = (255 - b);
            if(x != 0 && !(x % 5)) //Keep lines under 70 columns per spec.
            {
                outFile << "\n";
            }
            outFile << r << " " << g << " " << b << " ";
        }
        outFile << "\n";
    }
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28392427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档