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 #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