> > > > Sorting neighboring pixel values is a particularly dumb method of > > > > doing a CWM filter (when all you want is the median), so fixing the > > > > dumb method of sorting is not really going to make the code good. > > > > The correct way to do CWM is roughly min(max(center,min(neighbors), > > > > max(neighbors)), at least for weight=4. (IIRC, this is from memory) > > > > If this is only for to find the median, this code seem to produce a good > > approximation of the true median for a 3x3 patch (in fact, this seem to > > return > > the true median value the majority of time) > > > > inline int median3(int a, int b, int c) > > { > > if ( a <= b ) > > { > > if ( b <= c ) > > return b; > > if ( c <= a ) > > return a; > > return c; > > } > > > > // a > b > > if ( a <= c ) > > return a; > > if ( b <= c ) > > return c; > > return b; > > } > > > > > > void fast_median3x3(int *d) > > { > > > > int m1, m2 ,m3, m4; > > > > m1 = median3(d[0], d[1], d[2]); > > m2 = median3(d[3], d[4], d[5]); > > m3 = median3(d[6], d[7], d[8]); > > > > return median3(m1,m2,m3); > > } > > > > Note that the median3() func is coded very ugly and can certainly to be > coded > > for to be very more speed > > (it only return the median from 3 values) > > > > On other side, I have found something that produce one approximation of > > the median for patchs biggers than 3x3, but the value returned can be very > > far > > from the true median value :( > > (n=9 for 3x3 patchs and n have always to be >= 3) > > > > > > int approx_median(int *d , int n) > > { > > > > int i, v, imin=255, imax=-255, sum = 0; > > > > for (i= 0; i < n; i++ ) > > { > > v = d[i]; > > > > if (v < imin ) > > { > > imin = v; > > } > > else > > if ( v > imax ) > > { > > imax = v; > > } > > sum += v; > > } > > > > return (sum - imin - imax)/(n-2); > > } > > > > In fact, this compute the mean but without the minimum and maximum extrems > > values > > (this can for example to be used for to filter pictures with a little > > impulsed > > white noise) > > Note too that with a very simple inversion of the j loop, we have already a > very > big reduction on the number of bubbles swaps (about 50%) > > int BubbleSort_ext(int *data, int n) > { > int i, j, tmp, nswaps = 0; > > for (i = 0 ; i < n ; i++) > { > /* for( j = i + 1 ; j < n ; j++) */ > for( j = n - 1 ; j > i ; j--) > { > if( data[i] > data[j] ) > { > nswaps++; > tmp = data[i]; > data[i] = data[j]; > data[j] = tmp; > } > } > } > > return nswaps; > } > > > Unsorted (16 values) : 206 188 63 212 135 171 238 21 224 153 234 208 16 67 84 > 109 > > > Original Bubble Sort : (70 swaps) > BubbleSorted v0 (16 values) : 16 21 63 67 84 109 135 153 171 188 206 208 212 > 224 > 234 238 > > > Modified Bubble Sort : (30 swaps) > BubbleSorted v1 (16 values) : 16 21 63 67 84 109 135 153 171 188 206 208 212 > 224 > 234 238
I have found something that at http://lodev.org/cgtutor/filtering.html that seem better :) int CombSort(int * data, int n) { int gap = n; bool swapped = false; int nswaps = 0; int i, j, tmp; while(gap > 1 || swapped) { //shrink factor 1.3 gap = (gap * 10) / 13; if(gap == 9 || gap == 10) gap = 11; if (gap < 1) gap = 1; swapped = false; for (i = 0; i < n - gap; i++) { j = i + gap; if (data[i] > data[j]) { tmp = data[i]; data[i] = data[j]; data[j] = tmp; swapped = true; nswaps++; } } } } Unsorted (16 values) : 131 246 150 152 240 228 18 234 57 93 68 216 62 75 78 238 Original Bubble Sort : (69 swaps) BubbleSorted v0 (16 values) : 18 57 62 68 75 78 93 131 150 152 216 228 234 238 240 246 Modified Bubble Sort : (39 swaps) BubbleSorted v1 (16 values) : 18 57 62 68 75 78 93 131 150 152 216 228 234 238 240 246 39 vs 69 swaps/assignments (43% reduction) Comb Sort : (15 swaps) Comb Sort (16 values) : 18 57 62 68 75 78 93 131 150 152 216 228 234 238 240 246 15 vs 69 swaps/assignments (78% reduction) => I begin to work about the "min(max(center,min(neighbors),max(neighbors))" way (I don't really understand what is exactely the method to use, but I think speedly understand this after some tests / researchs on the Net) @+ Yannoo ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Schrodinger-devel mailing list Schrodinger-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/schrodinger-devel