$search
00001 /* 00002 * Copyright (c) 2011, Mårten Björkman (celle@csc.kth.se) 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are 00007 * met: 00008 * 00009 * 1.Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2.Redistributions in binary form must reproduce the above 00012 * copyright notice, this list of conditions and the following 00013 * disclaimer in the documentation and/or other materials provided 00014 * with the distribution. 00015 * 3.The name of Mårten Björkman may not be used to endorse or 00016 * promote products derived from this software without specific 00017 * prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00023 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 00032 #ifndef LARGEST_H 00033 #define LARGEST_H 00034 00037 #include "pyra/tpimage.h" 00038 00040 00046 void KeepLargestSegment(Image<unsigned char> &segment, 00047 int fromLabel, int toLabel, int minArea = 0); 00048 00050 00053 template<class T> void FillHoles(Image<T> &segment) 00054 { 00055 int width = segment.GetWidth(); 00056 int height = segment.GetHeight(); 00057 T *masd = segment.GetData(); 00058 int w = width; 00059 int h = height; 00060 unsigned char *lastrow = new unsigned char[w]; 00061 unsigned char *changes = new unsigned char[w]; 00062 unsigned char *tmd = new unsigned char[w*h]; 00063 for (int x=0;x<w;x++) { 00064 unsigned char bit = (masd[x]>0 ? 1 : 0); 00065 lastrow[x] = bit; 00066 changes[x] = bit; 00067 } 00068 for (int y=1;y<h;y++) { 00069 int l = 1; 00070 unsigned char *ptr = &masd[y*w]; 00071 for (int x=0;x<w;x++) { 00072 unsigned char bit = (ptr[x]>0 ? 1 : 0); 00073 changes[x] += (bit^lastrow[x]); 00074 lastrow[x] = bit; 00075 } 00076 for (int x=0;x<w;x++) { 00077 unsigned char c = changes[x]; 00078 c -= (c>l ? 2 : 0); 00079 l = c + 1; 00080 changes[x] = c; 00081 } 00082 l = 1; 00083 for (int x=w-1;x>=0;x--) { 00084 unsigned char c = changes[x]; 00085 c -= (c>l ? 2 : 0); 00086 changes[x] = c; 00087 l = c + 1; 00088 } 00089 for (int x=0;x<w;x++) { 00090 unsigned char c = changes[x]; 00091 tmd[y*w+x] = (c>0 ? 1 : 0); 00092 } 00093 } 00094 for (int x=0;x<w;x++) { 00095 unsigned char bit = (masd[(h-1)*w+x]>0 ? 1 : 0); 00096 lastrow[x] = bit; 00097 changes[x] = bit; 00098 } 00099 for (int y=h-2;y>=0;y--) { 00100 int l = 1; 00101 unsigned char *ptr = &masd[y*w]; 00102 for (int x=0;x<w;x++) { 00103 unsigned char bit = (ptr[x]>0 ? 1 : 0); 00104 changes[x] += (bit^lastrow[x]); 00105 lastrow[x] = bit; 00106 } 00107 for (int x=0;x<w;x++) { 00108 unsigned char c = changes[x]; 00109 c -= (c>l ? 2 : 0); 00110 l = c + 1; 00111 changes[x] = c; 00112 } 00113 l = 1; 00114 for (int x=w-1;x>=0;x--) { 00115 unsigned char c = changes[x]; 00116 c -= (c>l ? 2 : 0); 00117 changes[x] = c; 00118 l = c + 1; 00119 } 00120 for (int x=0;x<w;x++) { 00121 unsigned char c = changes[x]; 00122 ptr[x] = tmd[y*w+x] & (c>0 ? 1 : 0); 00123 } 00124 } 00125 delete [] tmd; 00126 delete [] changes; 00127 delete [] lastrow; 00128 } 00129 00130 00131 #endif