大家好!
几周前,我开始独自学习C ++。我现在希望承担一个小项目;使用我的Ubuntu机器ID来运行一个可以对RGB图像进行阈值处理的代码,然后根据我自己输入的某些值对其进行过滤。
我应该如何进行?尝试时应该记住什么?您认为应该花多长时间?请记住,我之前有0年编码经验。
谢谢!
答案 0 :(得分:2)
对于第一个实验,我建议您仅考虑灰度图像或仅考虑RGB图像的强度通道。 无论如何,如果您有一些图像数据:
libjpeg
等)对于第1点,请尝试从pgm
图片开始,因为它们确实很容易阅读(请参阅Wikipedia)。
对于大多数可管理的pgm
图像,您可能不需要块实现,只需将整个内容加载到一个大的拟合类型数组中即可。
编辑:一些代码,将可运行的C ++代码的具体汇编作为练习;)
std::ifstream file("your-filename.pgm", std::ifstream::binary);
// Read header information:
// Check out wikipedia article and the C++ documentation for ifstream.
char magic[2]; // magic number, P2 or P5
file.read(magic, 2);
if (magic[0] != 'P' || magic[1] < '1' || magic[1] > '5') {
throw std::exception("Invalid magic number");
}
int width, height, maxValue;
file >> width >> height >> maxValue;
if (maxValue <= 0 || maxValue >= (1 << 16)) {
throw std::exception("Max value must be resonable");
}
file.ignore(1);
std::unique_ptr<unsigned char> data(new unsigned char[width * height * sizeof(unsigned char)]);
file.read(reinterpret_cast<char*>(data.get()), width * height * sizeof(unsigned char));
// Now, the smart pointer 'data' contains your image grayscale data.
请谨慎:某些pgm文件使用unsigned short
而不是unsigned char
,但是此信息位于文件头中(您可以通过maxValue
字段进行识别)。
像上面那样加载数据后,您可以遍历该数组,进行处理(阈值处理),然后将处理后的文件再次写入磁盘。