아래에는 이미지의 픽셀을 반복하고 각 픽셀에 대해 0 또는 1을 출력하는 CImg 라이브러리 (http://cimg.sourceforge.net/)를 사용하는 간단한 프로그램이 있습니다. 계조 값 (밝은 색 또는 어두운 색)을 기반으로합니다. 꽤 이상한 점은 프로그램을 실행할 때마다 (같은 입력으로) 다른 결과를 얻고 있다는 것입니다.C++/CImg 일치하지 않는 결과
내가 예상대로 작동
image.display()
을, 그래서 CIMG 제대로 이미지를 읽는 것 같으면. 그러나 inner for 루프에 AvgVal을 인쇄하려고하면 매번 다른 값을 얻습니다. 차이가 있다면 OSX 10.7.3과 gcc 4.2.1을 사용하고 있습니다.
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "CImg-1.4.9/CImg.h"
using namespace cimg_library;
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cout << "Usage: acepp inputfile outputfile" << std::endl;
}
else {
std::ofstream outputFile(argv[2]);
if (!outputFile.is_open()) throw std::runtime_error("error: cannot open file for writing");
CImg<unsigned char> image(argv[1]);
int RVal, GVal, BVal, AvgVal, outBit;
for (int iHeight = 0; iHeight < image.height(); iHeight++) {
for (int iWidth = 0; iWidth < image.width(); iWidth++) {
RVal = image(iWidth,iHeight,0,0);
GVal = image(iWidth,iHeight,0,1);
BVal = image(iWidth,iHeight,0,2);
AvgVal = (RVal + GVal + BVal)/3;
outBit = 1;
if (AvgVal > 127) outBit = 0; // low is dark, high is light
outputFile << outBit;
}
outputFile << std::endl;
}
outputFile.close();
std::cout << "Done writing to: " << argv[2] << std::endl;
}
return 0;
}
잠시 동안 읽고 있었지만 방금 등록 했으므로 사용중인 예제 이미지를 게시 할 수 없습니다. 저는 그것들을 설명 할 것입니다 - 그들은 Photoshop CS5를 사용하여 작성된 흑백 패턴을 포함하는 10px x 10px png 이미지였습니다.
"inner for 루프에서 AvgVal을 인쇄하려고하면 ArgVal을 어떻게 인쇄합니까?" 기대 출력을 포함한 샘플 출력은 실제 이미지 대신에 좋을 것입니다. – uesp