将任何彩色PPM图像转换为只有8种颜色的彩色图像

Converting any colored PPM image into a colored image with only 8 colors

本文关键字:8种 颜色 彩色图像 彩色 任何 PPM 图像 转换      更新时间:2023-10-16

我需要一个算法来将ppm格式的任何彩色图像转换为C++中的8种颜色。我尝试了这个代码,但我一直面临>><<运算符的错误:

    #include "stdafx.h"
    #include<iostream>
    #include<fstream>
    using namespace std;
    int main()
    {
ifstream in;
in.open("football.ppm");
ofstream out("football8color.ppm");
string header;
int cols, rows,colors;
int r, g, b;

in >> header >> cols >> rows >> colors;
out << header << endl;
out<< cols<< " " << rows <<endl;
out<< colors<< endl;
for ( int i=0; i<rows; i++)
{
    for (int j=0; j<cols; j++){
        in>> r >> g >> b;
}
out <<endl;
    }
    in.close();
    out.close();
    return 0 ;
    }

我得到这些错误:

    4   IntelliSense: no operator "<<" matches these operands   c:usersatiyehdocumentsvisual studio 2010projectsmm ass 2mm ass 2mm ass 2.cpp    18
    3   IntelliSense: no operator ">>" matches these operands   c:usersatiyehdocumentsvisual studio 2010projectsmm ass 2mm ass 2mm ass 2.cpp    17
Error   2   error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)  c:usersatiyehdocumentsvisual studio 2010projectsmm ass 2mm ass 2mm ass 2.cpp    16
Error   1   error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::ifstream' (or there is no acceptable conversion) c:usersatiyehdocumentsvisual studio 2010projectsmm ass 2mm ass 2mm ass 2.cpp    15

对于算法,您应该查找"颜色量化"。如果你想让它看起来好看,你应该查找抖动技术,比如Floyd-Stienberg抖动。