convertToRas.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Fraunhofer FKIE
00003  *
00004  * Authors: Jochen Welle
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  * * Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * * Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * * Neither the name of the Fraunhofer FKIE nor the names of its
00015  *   contributors may be used to endorse or promote products derived from
00016  *   this software without specific prior written permission.
00017  *
00018  * This file is part of the StructureColoring ROS package.
00019  *
00020  * The StructureColoring ROS package is free software:
00021  * you can redistribute it and/or modify it under the terms of the
00022  * GNU Lesser General Public License as published by the Free
00023  * Software Foundation, either version 3 of the License, or
00024  * (at your option) any later version.
00025  *
00026  * The StructureColoring ROS package is distributed in the hope that it will be useful,
00027  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00028  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029  * GNU Lesser General Public License for more details.
00030  *
00031  * You should have received a copy of the GNU Lesser General Public License
00032  * along with The StructureColoring ROS package.
00033  * If not, see <http://www.gnu.org/licenses/>.
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                 //uint8_t c = input.at<uint8_t>(j,i);
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()) // not seen yet
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 


structure_coloring_fkie
Author(s): Bastian Gaspers
autogenerated on Sun Jan 5 2014 11:38:09