Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include <structureColoring/segcomp/rangeimageio.h>
00037 #include <opencv/cv.h>
00038 #include <opencv/highgui.h>
00039 #include <iostream>
00040 #include <map>
00041
00042
00043 void usage(const char* prg)
00044 {
00045 std::cout << "Usage: " << prg << " infilename outfilename\n";
00046 std::cout << "Convert the image \"infilename\" to a 8-bit grayscale SUN raster image \"outfilename\". ";
00047 std::cout << "The colors are converted as follows:\n";
00048 std::cout << "\tBlack\t-> 0\n"
00049 "\tWhite\t-> 1\n"
00050 "\tall other colors are given consecutive values starting from 10 upwards.\n"
00051 "Therefore more than 248 different colors in the input image are not allowed.\n";
00052 }
00053
00054 uint32_t rgbToInt(const cv::Vec3b& rgb)
00055 {
00056 return ((rgb[0] << 16) | (rgb[1] << 8) | (rgb[2]));
00057 }
00058 cv::Vec3b intToRgb(uint32_t i)
00059 {
00060 cv::Vec3b rgb;
00061 rgb[0] = (i >> 16) & 255;
00062 rgb[1] = (i >> 8) & 255;
00063 rgb[2] = i & 255;
00064 return rgb;
00065 }
00066
00067 std::ostream& operator<< (std::ostream& os, const cv::Vec3b& v)
00068 {
00069 os << (int)v[0] << "," << (int)v[1] << "," << (int)v[2];
00070 return os;
00071 }
00072
00073 int main(int argc, char* argv[])
00074 {
00075 typedef std::map<uint32_t, uint8_t> ColorMap;
00076
00077 if (argc < 3)
00078 {
00079 usage(argv[0]);
00080 }
00081 else
00082 {
00083 cv::Mat input = cv::imread(argv[1], 1);
00084 unsigned int width = input.cols;
00085 unsigned int height = input.rows;
00086 std::cout << argv[1] << ": " << width << " x " << height << "\n";
00087 std::vector<unsigned char> data;
00088 ColorMap colorMap;
00089 uint8_t segCount = 10;
00090 for (size_t j = 0; j < height; ++j)
00091 {
00092 for (size_t i = 0; i < width; ++i)
00093 {
00094
00095 cv::Vec3b rgb = input.at<cv::Vec3b>(j,i);
00096 uint32_t rgbInt = rgbToInt(rgb);
00097 if ((rgb[0] == 255) && (rgb[1] == 255) && (rgb[2] == 255))
00098 colorMap[rgbInt] = 1;
00099 else if (rgbInt == 0)
00100 colorMap[rgbInt] = 0;
00101 ColorMap::iterator it = colorMap.find(rgbInt);
00102 if (it == colorMap.end())
00103 {
00104 colorMap[rgbInt] = segCount;
00105 if (segCount < 255)
00106 ++segCount;
00107 else
00108 {
00109 std::cerr << "ERROR: input image has more than 248 colors!" << std::endl;
00110 exit(1);
00111 }
00112 }
00113 data.push_back(colorMap[rgbInt]);
00114 }
00115 }
00116 for (ColorMap::iterator it = colorMap.begin(); it != colorMap.end(); ++it)
00117 std::cout << intToRgb(it->first) << ": " << (int)it->second << std::endl;
00118 std::cout << "map size: " << colorMap.size() << std::endl;
00119 writeRasterfile(argv[2], width, height, data);
00120 }
00121 return 0;
00122 }
00123