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

时间:2013-12-14 11:07:04

标签: c++

我需要一种算法,将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:\users\atiyeh\documents\visual studio 2010\projects\mm ass 2\mm ass 2\mm ass 2.cpp    18
    3   IntelliSense: no operator ">>" matches these operands   c:\users\atiyeh\documents\visual studio 2010\projects\mm ass 2\mm ass 2\mm 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:\users\atiyeh\documents\visual studio 2010\projects\mm ass 2\mm ass 2\mm 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:\users\atiyeh\documents\visual studio 2010\projects\mm ass 2\mm ass 2\mm ass 2.cpp    15

1 个答案:

答案 0 :(得分:0)

对于算法,您应该查找Color Quantization。如果你想让它看起来不错,你应该查看dithering技术,如Floyd-Stienberg抖动。