Picking multiple objects from background

Hello,
Quick version:
Does anyone know of any image processing libraries/techniques that can pick out shapes from a background accurately, even given significant gradients of brightness between different shapes, and a gradually changing background?

Detailed:
I need to pick out some crystals in a darker background (.png images). The program should pick what it believes to be a crystal and color it red, while the rest of the image remains black. Here are some issues that make this challenging (i.e. can't just use a typical threshold feature):

1. Crystals vary in brightness dramatically. Some are bright, while others almost melt into the background.
2. The background itself is somewhat noisy changes and brightness gradually, so a single baseline value can't be used.
3. Especially dark blotches in background exist: these may make it look as if a crystal starts right after one if an algorithm is not properly configured.
4. Some of the bright crystals have a fuzz halo around them that should not be counted as part of the crystal.
5. The program needs to successfully and accurately find at least 90-95% of the crystals.
I don't see an option for posting an example image here, unfortunately.

What I have so far:
1. Have a progressive baseline average for hue moving across the image, and count anything above that value beyond a certain threshold as a crystal.
2. Move both left to right and down an image to improve detection.
3. Using SFML for image processing as that's the easiest thing I know of.
4. Plan to use statistically "unusual" points (i.e. >3 constantly rising hue values) as an indication of a crystal.
5. Plan to use consistently falling values as possible indication of fuzz to "demote" some found crystal pixels to non-pixel status.
The code uses a helper "support.png" file in which a red region on the left and at the top of the image (for the two travel directions) identifies the initial background regions for the program.

Haven't implemented 4 and 5 yet, both should help, but now my program still doesn't fully find some of the darker crystals (though I can fairly clearly distinguish them by eye) and picks up a bit too much fuzz on the brighter ones. Clearly changing the threshold will merely cause a tradeoff between these two, while I need to improve them simultaneously. Any ideas would be greatly appreciated.

Here is my code so far:
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>

int X;	// Resolution
int Y;

using namespace std;

class List10 {
private:
	vector<int> list;
public:
	int size();
	void add(int);
	void reset()
	{
		list.clear();
	}
	double average();
	int operator[](int i)
	{
		return list[i];
	}
};

int List10::size()
{
	return list.size();
}
void List10::add(int val)
{
	if (list.size() < 10)
	{
		list.push_back(val);
	}
	else
	{
		for (int i = 1; i < list.size(); i++)
		{
			list[i - 1] = list[i];
		}
		list[9] = val;
	}
}

double List10::average()
{
	double sum = 0;
	for (int i = 0; i <= list.size(); i++)
		sum += list[i];
	return sum / list.size();
}


int main()
{
	string inFile;
	cout << "Enter the input .png file: ";
	cin >> inFile;
	cout << "Enter a threshold (pixels): ";
	int threshold;
	cin >> threshold;

	sf::Image inImage;
	sf::Image inImage2;
	sf::Image outImage;
	inImage.loadFromFile(inFile);
	inImage2.loadFromFile("support.png");

	sf::Vector2u frameSize = inImage.getSize();
	X = frameSize.x;
	Y = frameSize.y;

	outImage.create(X, Y, sf::Color::Black);

	vector<vector<int>> hue(Y, vector<int>(X));
	vector<vector<bool>> crystal(Y, vector<bool>(X));

	sf::Color color;

	// Load image into 2d vector
	for (int y = 0; y < Y; ++y)
	{
		for (int x = 0; x < X; ++x)
		{
			color = inImage.getPixel(x, y);
			sat[y][x] = hue[y][x] = color.b + color.g + color.r;
			if (color.g != 0 && color.r == 0)
				hue[y][x] += 255;
			else if (color.r != 0)
				hue[y][x] += 510;
		}
	}

	int x2;
	int y2;
	int up = 0;
	List10 base10;
	int base;

	for (int y = 0; y < Y; ++y)
	{
		for (int x = 0; x < X; ++x)
		{
			color = inImage2.getPixel(x, y);
			if (color == sf::Color::Red)
			{
				base10.add(hue[y][x]);
				base = base10.average();
				//cout << "1\n";
			}
			else if (base10.size() == 0)
			{
				x2 = x + 1;
				//cout << "2\n";
				while (inImage2.getPixel(x2, y) != sf::Color::Red)
				{
					++x2;
				}
				base10.add(hue[y][x2]);
				base = base10.average();
			}
			else if (hue[y][x] - base > threshold || up > 3)
			{
				//cout << "3\n";
				crystal[y][x] = true;
				outImage.setPixel(x, y, sf::Color::Red);
			}
			else if (hue[y][x] - base > -6)	// Avoid black blotches in background
			{
				//cout << "4\n";
				base10.add(hue[y][x]);
				base = base10.average();
			}
			//cout << "Row " << y << " done.\n";
		}
		base10.reset();
	}

	for (int x = 0; x < X; ++x)
	{
		for (int y = 0; y < Y; ++y)
		{
			color = inImage2.getPixel(x, y);
			if (color == sf::Color::Red)
			{
				base10.add(hue[y][x]);
				base = base10.average();
				//cout << "1\n";
			}
			else if (base10.size() == 0)
			{
				y2 = y + 1;
				//cout << "2\n";
				while (inImage2.getPixel(x, y2) != sf::Color::Red)
				{
					++y2;
				}
				base10.add(hue[y2][x]);
				base = base10.average();
			}
			else if (hue[y][x] - base > threshold || up > 3)
			{
				//cout << "3\n";
				crystal[y][x] = true;
				outImage.setPixel(x, y, sf::Color::Red);
			}
			else if (hue[y][x] - base > -6)	// Avoid black blotches in background
			{
				//cout << "4\n";
				base10.add(hue[y][x]);
				base = base10.average();
			}
			//cout << "Out\n";
		}
		base10.reset();
	}

	// Remove noise and smooth image
	int neighbors;

	for (int y = 1; y < Y - 1; ++y)
	{
		for (int x = 1; x < X - 1; ++x)
		{
			neighbors = 0;
			if (crystal[y][x] == 1)
			{
				if (crystal[y][x + 1] == 1)
					++neighbors;
				if (crystal[y][x - 1] == 1)
					++neighbors;
				if (crystal[y + 1][x] == 1)
					++neighbors;
				if (crystal[y - 1][x] == 1)
					++neighbors;
				if (crystal[y + 1][x + 1] == 1)
					++neighbors;
				if (crystal[y - 1][x - 1] == 1)
					++neighbors;
				if (crystal[y + 1][x - 1] == 1)
					++neighbors;
				if (crystal[y - 1][x + 1] == 1)
					++neighbors;

				if (neighbors < 4)
				{
					crystal[y][x] = 0;
					outImage.setPixel(x, y, sf::Color::Black);
				}
			}
		}
	}

	// Smoothed image
	outImage.saveToFile("output2.png");

	ofstream fout;
	string outFile = inFile;

	fout.open(outFile + "-hue.csv");
	for (int row = 0; row < Y; ++row)
	{
		for (int col = 0; col < X; ++col)
			fout << hue[row][col] << ',';
		fout << endl;
	}
	fout.close();

	fout.open(outFile + "-sat.csv");
	for (int row = 0; row < Y; ++row)
	{
		for (int col = 0; col < X; ++col)
			fout << sat[row][col] << ',';
		fout << endl;
	}
	fout.close();

	system("pause");
	return 0;
}



Topic archived. No new replies allowed.