aboutsummaryrefslogtreecommitdiff
path: root/src/itrans-threshold.c
blob: 1aee5ca27f13ae189f46b260bfc831a2b643e00c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * itrans-threshold.c
 *
 * Threshold and adaptive threshold peak searches
 *
 * (c) 2007 Thomas White <taw27@cam.ac.uk>
 *
 *  dtr - Diffraction Tomography Reconstruction
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>

#include "control.h"
#include "imagedisplay.h"
#include "reflections.h"

unsigned int itrans_peaksearch_threshold(ImageRecord imagerecord, ControlContext *ctx) {

	int width, height;
	int x, y;
	unsigned int n_reflections = 0;
	uint16_t *image = imagerecord.image;
	double tilt_degrees = imagerecord.tilt;
	
	width = imagerecord.width;
	height = imagerecord.height;
	
	/* Simple Thresholding */
	for ( y=0; y<height; y++ ) {
		for ( x=0; x<width; x++ ) {

			if ( image[x + width*y] > 10 ) {
				if ( imagerecord.fmode == FORMULATION_PIXELSIZE ) {
					reflection_add_from_reciprocal(ctx, (signed)(x-imagerecord.x_centre)*imagerecord.pixel_size, (signed)(y-imagerecord.y_centre)*imagerecord.pixel_size,
									tilt_degrees, image[x + width*y]);
				} else {
					reflection_add_from_dp(ctx, (signed)(x-imagerecord.x_centre)/imagerecord.resolution, (signed)(y-imagerecord.y_centre)/imagerecord.resolution, tilt_degrees, image[x + width*y]);
				}
				n_reflections++;
			}
		
		}
	}
	
	return n_reflections;
	
}

unsigned int itrans_peaksearch_adaptive_threshold(ImageRecord imagerecord, ControlContext *ctx) {

	uint16_t max_val = 0;
	int width, height;
	unsigned int n_reflections = 0;
	uint16_t *image = imagerecord.image;
	double tilt_degrees = imagerecord.tilt;
	
	width = imagerecord.width;
	height = imagerecord.height;
	
	/* Adaptive Thresholding */
	do {
	
		int max_x = 0;
		int max_y = 0;;
		int x, y;
	
		/* Locate the highest point */
		max_val = 0;
		for ( y=0; y<height; y++ ) {
			for ( x=0; x<width; x++ ) {

				if ( image[x + width*y] > max_val ) {
					max_val = image[x + width*y];
					max_x = x;  max_y = y;
				}
			
			}
		}
		
		if ( max_val > 50 ) {
			if ( imagerecord.fmode == FORMULATION_PIXELSIZE ) {
				reflection_add_from_reciprocal(ctx, (signed)(x-imagerecord.x_centre)*imagerecord.pixel_size, (signed)(y-imagerecord.y_centre)*imagerecord.pixel_size,
									tilt_degrees, image[x + width*y]);
			} else {
				reflection_add_from_dp(ctx, (signed)(x-imagerecord.x_centre)/imagerecord.resolution, (signed)(y-imagerecord.y_centre)/imagerecord.resolution, tilt_degrees, image[x + width*y]);
			}
			n_reflections++;
			
			/* Remove it and its surroundings */
			for ( y=((max_y-10>0)?max_y-10:0); y<((max_y+10)<height?max_y+10:height); y++ ) {
				for ( x=((max_x-10>0)?max_x-10:0); x<((max_x+10)<width?max_x+10:width); x++ ) {
					image[x + width*y] = 0;
				}
			}
		}
		
	} while ( max_val > 50 );
	
	return n_reflections;

}