degate  0.1.2
ImageStatistics.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020 */
00021 
00022 #ifndef __IMAGESTATISTICS_H__
00023 #define __IMAGESTATISTICS_H__
00024 
00025 #include <BoundingBox.h>
00026 #include <ImageManipulation.h>
00027 
00028 namespace degate {
00029 
00030   // We need a forward decleration here in order to use img->get_pixel_as<>().
00031   template<typename PixelTypeDst, typename ImageTypeSrc>
00032   inline PixelTypeDst get_pixel_as(typename std::shared_ptr<ImageTypeSrc> img,
00033                                    unsigned int x, unsigned int y);
00034 
00035   /**
00036    * Get the minimum pixel value of a single channel image.
00037    */
00038   template<typename ImageType>
00039   typename ImageType::pixel_type get_minimum(std::shared_ptr<ImageType> img) {
00040 
00041     assert_is_single_channel_image<ImageType>();
00042     typename ImageType::pixel_type minimum = img->get_pixel(0, 0);
00043 
00044     for(unsigned int y = 0; y < img->get_height(); y++)
00045       for(unsigned int x = 0; x < img->get_width(); x++) {
00046         typename ImageType::pixel_type p = img->get_pixel(x, y);
00047         if(p < minimum) minimum = p;
00048       }
00049     return minimum;
00050   }
00051 
00052   /**
00053    * Get the maximum pixel value of a single channel image.
00054    */
00055   template<typename ImageType>
00056   typename ImageType::pixel_type get_maximum(std::shared_ptr<ImageType> img) {
00057 
00058     assert_is_single_channel_image<ImageType>();
00059     typename ImageType::pixel_type maximum = img->get_pixel(0, 0);
00060 
00061     for(unsigned int y = 0; y < img->get_height(); y++)
00062       for(unsigned int x = 0; x < img->get_width(); x++) {
00063         typename ImageType::pixel_type p = img->get_pixel(x, y);
00064         if(p > maximum) maximum = p;
00065       }
00066     return maximum;
00067   }
00068 
00069 
00070   /**
00071    * Calculate the average pixel value of a single channel image.
00072    * If the input image is a multi-channel image, data will be converted on-the-fly.
00073    */
00074   template<typename ImageType>
00075   double average(std::shared_ptr<ImageType> img) {
00076     return average(img, 0, 0, img->get_width(), img->get_height());
00077   }
00078 
00079   /**
00080    * Calculate the average pixel value of a single channel image.
00081    * If the input image is a multi-channel image, data will be converted on-the-fly.
00082    * @exception DegateRuntimeException This exception is thrown, if the image area is 0. 
00083    */
00084   template<typename ImageType>
00085   double average(std::shared_ptr<ImageType> img, 
00086                  unsigned int start_x, unsigned int start_y, 
00087                  unsigned int width, unsigned int height) {
00088 
00089     double sum = 0;
00090 
00091     if(height == 0 || width == 0) throw DegateRuntimeException("Can't calculate average for an image.");
00092 
00093     for(unsigned int y = start_y; y < height; y++)
00094       for(unsigned int x = start_x; x < width; x++) {
00095         sum += img->template get_pixel_as<double>(x, y);
00096       }
00097 
00098     return sum / (double)(height * width);
00099   }
00100 
00101   /**
00102    * Calculate the average pixel value of a single channel image.
00103    * If the input image is a multi-channel image, data will be converted on-the-fly.
00104    * @exception DegateRuntimeException This exception is thrown, if the image area is 0. 
00105    */
00106   template<typename ImageType>
00107   void average_and_stddev(std::shared_ptr<ImageType> img, 
00108                           unsigned int start_x, unsigned int start_y, 
00109                           unsigned int width, unsigned int height,
00110                           double * avg, double * stddev) {
00111 
00112     double sum = 0;
00113     
00114     if(height == 0 || width == 0)
00115       throw DegateRuntimeException("Can't calculate average for an image.");
00116     
00117     for(unsigned int y = start_y; y < start_y + height; y++)
00118       for(unsigned int x = start_x; x < start_x + width; x++) {
00119         sum += img->template get_pixel_as<gs_double_pixel_t>(x, y);
00120       }
00121     
00122     *avg = sum / (double)(height * width);
00123     
00124     sum = 0;
00125     
00126     for(unsigned int y = start_y; y < start_y + height; y++)
00127       for(unsigned int x = start_x; x < start_x + width; x++) {
00128         sum += pow(*avg - img->template get_pixel_as<gs_double_pixel_t>(x, y), 2);
00129       }
00130     
00131     *stddev = sqrt(sum/(double)(height * width));
00132   }
00133 
00134 
00135 }
00136 
00137 #endif