|
degate 0.1.1
|
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 #include <EdgeDetection.h> 00023 #include <IPMedianFilter.h> 00024 00025 using namespace degate; 00026 00027 EdgeDetection::EdgeDetection(unsigned int _min_x, unsigned int _max_x, 00028 unsigned int _min_y, unsigned int _max_y, 00029 unsigned int _median_filter_width, 00030 unsigned int _blur_kernel_size, 00031 double _sigma) : 00032 min_x(_min_x), 00033 max_x(_max_x), 00034 min_y(_min_y), 00035 max_y(_max_y), 00036 median_filter_width(_median_filter_width), 00037 blur_kernel_size(_blur_kernel_size), 00038 border(_blur_kernel_size >> 1), 00039 sigma(_sigma), 00040 has_path(false) { 00041 00042 setup_pipe(); 00043 } 00044 00045 00046 EdgeDetection::~EdgeDetection() { 00047 } 00048 00049 unsigned int EdgeDetection::get_border() const { 00050 return border; 00051 } 00052 00053 00054 void EdgeDetection::setup_pipe() { 00055 00056 debug(TM, "will extract background image (%d, %d) (%d, %d)", min_x, min_y, max_x, max_y); 00057 std::tr1::shared_ptr<IPCopy<TileImage_RGBA, TileImage_GS_DOUBLE> > copy_rgba_to_gs 00058 (new IPCopy<TileImage_RGBA, TileImage_GS_DOUBLE>(min_x, max_x, min_y, max_y) ); 00059 00060 pipe.add(copy_rgba_to_gs); 00061 00062 if(median_filter_width > 0) { 00063 std::tr1::shared_ptr<IPMedianFilter<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE> > median_filter 00064 (new IPMedianFilter<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE>(median_filter_width)); 00065 00066 pipe.add(median_filter); 00067 } 00068 00069 00070 std::tr1::shared_ptr<IPNormalize<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE> > normalizer 00071 (new IPNormalize<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE>(0, 1) ); 00072 pipe.add(normalizer); 00073 00074 00075 if(blur_kernel_size > 0) { 00076 std::tr1::shared_ptr<GaussianBlur> 00077 GaussianB(new GaussianBlur(blur_kernel_size, blur_kernel_size, sigma)); 00078 00079 GaussianB->print(); 00080 std::tr1::shared_ptr<IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE> > gaussian_blur 00081 (new IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE>(GaussianB) ); 00082 00083 pipe.add(gaussian_blur); 00084 } 00085 00086 } 00087 00088 00089 unsigned int EdgeDetection::get_width() const { 00090 return max_x - min_x; 00091 } 00092 00093 unsigned int EdgeDetection::get_height() const { 00094 return max_y - min_y; 00095 } 00096 00097 void EdgeDetection::set_directory(std::string const& path) { 00098 directory = path; 00099 has_path = true; 00100 } 00101 00102 std::string EdgeDetection::get_directory() const { 00103 return directory; 00104 } 00105 00106 bool EdgeDetection::has_directory() const { 00107 return has_path; 00108 } 00109 00110 void EdgeDetection::run_edge_detection(ImageBase_shptr in) { 00111 00112 ImageBase_shptr out = pipe.run(in); 00113 assert(out != NULL); 00114 00115 std::tr1::shared_ptr<SobelYOperator> SobelY(new SobelYOperator()); 00116 std::tr1::shared_ptr<IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE> > edge_filter_x 00117 (new IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE>(SobelY) ); 00118 00119 std::tr1::shared_ptr<SobelXOperator> SobelX(new SobelXOperator()); 00120 std::tr1::shared_ptr<IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE> > edge_filter_y 00121 (new IPConvolve<TileImage_GS_DOUBLE, TileImage_GS_DOUBLE>(SobelX) ); 00122 00123 i1 = std::tr1::dynamic_pointer_cast<TileImage_GS_DOUBLE>(edge_filter_x->run(out)); 00124 i2 = std::tr1::dynamic_pointer_cast<TileImage_GS_DOUBLE>(edge_filter_y->run(out)); 00125 assert(i1 != NULL && i2 != NULL); 00126 00127 if(has_path) save_normalized_image<TileImage_GS_DOUBLE>(join_pathes(directory, "01_sobelx.tif"), i1); 00128 if(has_path) save_normalized_image<TileImage_GS_DOUBLE>(join_pathes(directory, "02_sobely.tif"), i2); 00129 } 00130 00131 TileImage_GS_DOUBLE_shptr EdgeDetection::get_horizontal_edges() { 00132 return i1; 00133 } 00134 00135 TileImage_GS_DOUBLE_shptr EdgeDetection::get_vertical_edges() { 00136 return i2; 00137 } 00138 00139 00140 TileImage_GS_DOUBLE_shptr EdgeDetection::get_edge_magnitude_image(TileImage_GS_DOUBLE_shptr probability_map) { 00141 00142 if(i1 == NULL || i2 == NULL) return TileImage_GS_DOUBLE_shptr(); 00143 00144 TileImage_GS_DOUBLE_shptr edge_mag_image(new TileImage_GS_DOUBLE(get_width(), get_height())); 00145 00146 for(unsigned int y = border +2; y < get_height() - border -1; y++) { 00147 for(unsigned int x = border+2; x < get_width() - border -1; x++) { 00148 double gx = i1->get_pixel(x, y); 00149 double gy = i2->get_pixel(x, y); 00150 double g = sqrt(pow(gx,2) + pow(gy, 2)); 00151 if(probability_map != NULL) { 00152 if(probability_map->get_pixel(x, y) > 0) 00153 edge_mag_image->set_pixel(x, y, g); 00154 } 00155 else 00156 edge_mag_image->set_pixel(x, y, g); 00157 } 00158 } 00159 00160 if(has_path) save_normalized_image<TileImage_GS_DOUBLE>(join_pathes(directory, 00161 "03_edge_mag_image.tif"), 00162 edge_mag_image); 00163 return edge_mag_image; 00164 } 00165 00166 00167 00168 TileImage_GS_DOUBLE_shptr EdgeDetection::get_edge_image(TileImage_GS_DOUBLE_shptr probability_map) { 00169 00170 if(i1 == NULL || i2 == NULL) return TileImage_GS_DOUBLE_shptr(); 00171 00172 TileImage_GS_DOUBLE_shptr edge_image(new TileImage_GS_DOUBLE(get_width(), get_height())); 00173 00174 for(unsigned int y = border +2; y < get_height() - border -1; y++) { 00175 for(unsigned int x = border+2; x < get_width() - border -1; x++) { 00176 double gx = i1->get_pixel(x, y); 00177 double gy = i2->get_pixel(x, y); 00178 00179 if(probability_map != NULL) { 00180 if(probability_map->get_pixel(x, y) > 0) 00181 edge_image->set_pixel(x, y, gx + gy); 00182 } 00183 else 00184 edge_image->set_pixel(x, y, gx + gy); 00185 } 00186 } 00187 if(has_path) save_normalized_image<TileImage_GS_DOUBLE>(join_pathes(directory, 00188 "03_edge_image.tif"), 00189 edge_image); 00190 00191 return edge_image; 00192 } 00193
1.7.4