Skip to content

Commit 985127e

Browse files
committed
Tweaked highlight algorithm, fixed some concurrency problem.
1 parent e3f87fd commit 985127e

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

relightlab/alignrow.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ FindAlignment::FindAlignment(Align *_align, bool update) {
2020
}
2121

2222
void FindAlignment::run() {
23-
mutex.lock();
24-
status = RUNNING;
25-
mutex.unlock();
23+
setStatus(RUNNING);
2624

2725
Project &project = qRelightApp->project();
2826
for(size_t i = 0; i < project.images.size(); i++) {
@@ -32,21 +30,18 @@ void FindAlignment::run() {
3230

3331
QImage img(image.filename);
3432
if(img.isNull()) {
35-
mutex.lock();
36-
status = FAILED;
37-
mutex.unlock();
33+
setStatus(FAILED);
3834
progressed(QString("Failed loading image: %1").arg(image.filename), 100);
3935
return;
4036
}
4137
align->readThumb(img, i);
4238

4339
int progress = std::min(99, (int)(100*(i+1) / project.images.size()));
44-
progressed(QString("Collecting patches"), progress);
40+
if(!progressed(QString("Collecting patches"), progress))
41+
return;
4542
}
4643
progressed(QString("Done"), 100);
47-
mutex.lock();
48-
status = DONE;
49-
mutex.unlock();
44+
setStatus(DONE);
5045
}
5146

5247

relightlab/sphererow.cpp

+5-11
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ DetectHighlights::DetectHighlights(Sphere *_sphere, bool update) {
2121
}
2222

2323
void DetectHighlights::run() {
24-
//TODO: create a setStatus function in Task.
25-
mutex.lock();
26-
status = RUNNING;
27-
mutex.unlock();
24+
setStatus(RUNNING);
2825

2926
sphere->sphereImg.fill(0);
3027
sphere->thumbs.clear();
@@ -36,21 +33,18 @@ void DetectHighlights::run() {
3633

3734
QImage img(image.filename);
3835
if(img.isNull()) {
39-
mutex.lock();
40-
status = FAILED;
41-
mutex.unlock();
36+
setStatus(FAILED);
4237
progressed(QString("Failed loading image: %1").arg(image.filename), 100);
4338
return;
4439
}
4540
sphere->findHighlight(img, i, image.skip, update_positions);
4641

4742
int progress = std::min(99, (int)(100*(i+1) / project.images.size()));
48-
progressed(QString("Detecting highlights"), progress);
43+
if(!progressed(QString("Detecting highlights"), progress))
44+
return;
4945
}
5046
progressed(QString("Done."), 100);
51-
mutex.lock();
52-
status = DONE;
53-
mutex.unlock();
47+
setStatus(DONE);
5448
}
5549

5650

relightlab/task.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void Task::runScript(QString program, QString script, QStringList arguments, QSt
9393

9494
void Task::pause() {
9595
mutex.lock();
96+
assert(status == RUNNING);
9697
status = PAUSED;
9798
}
9899

@@ -103,10 +104,24 @@ void Task::resume() {
103104
}
104105
}
105106

107+
void Task::setStatus(Status s) {
108+
if(s == PAUSED) {
109+
assert(status != PAUSED);
110+
pause();
111+
return;
112+
}
113+
if(status != PAUSED) {
114+
mutex.lock();
115+
}
116+
status = s;
117+
mutex.unlock();
118+
}
119+
106120
void Task::stop() {
107121
if(status == PAUSED) { //we were already locked then.
108122
status = STOPPED;
109123
mutex.unlock();
124+
return;
110125
}
111126
status = STOPPED;
112127
}

relightlab/task.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Task: public QThread {
2727
virtual void stop();
2828
virtual void pause();
2929
virtual void resume();
30+
virtual void setStatus(Status s);
3031

3132

3233
void runPythonScript(QString script, QStringList arguments, QString workingdir = QString());

src/sphere.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,15 @@ void Sphere::findHighlight(QImage img, int n, bool skip, bool update_positions)
206206
return;
207207

208208
uchar threshold = 240;
209-
209+
//0.5% of the area allocated to the reflection.
210+
int highlight_area = (inner.width()*inner.height())/200;
210211
vector<int> histo;
211212

212213
//lower threshold until we find something.
213214
QPointF bari(0, 0); //in image coords
214215
int count = 0;
215216
int iter = 0;
216-
while(count < 10 && threshold > 100) {
217+
while(count < highlight_area && threshold > 100) {
217218
bari = QPointF(0, 0);
218219
count = 0;
219220
for(int y = inner.top(); y < inner.bottom(); y++) {
@@ -265,7 +266,7 @@ void Sphere::findHighlight(QImage img, int n, bool skip, bool update_positions)
265266
//threshold now is 10 lower so we get more points.
266267
threshold += 10;
267268

268-
if(threshold < 200) {
269+
if(threshold < 127) {
269270
//highlight in the mid greys? probably all the sphere is in shadow.
270271
lights[n] = QPointF(0, 0);
271272
return;

0 commit comments

Comments
 (0)