Skip to content

Commit

Permalink
added LRGB PTM in relightlab interface
Browse files Browse the repository at this point in the history
tuned luminosity in LPTM.
  • Loading branch information
ponchio committed Aug 28, 2022
1 parent 3b6795d commit 4ab3407
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 112 deletions.
126 changes: 29 additions & 97 deletions relight-cli/rtibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ bool RtiBuilder::init(std::function<bool(std::string stage, int percent)> *_call
nsamples = resample.npixels();

pickBases(resample);
} catch(std::exception e) {
} catch(std::exception &e) {
error = "Could not create a base.";
return false;
} catch(...) {
Expand Down Expand Up @@ -385,19 +385,12 @@ MaterialBuilder RtiBuilder::pickBasePTM(std::vector<Vector3f> &lights) {
assert(nplanes == 9);
//we could generalize for different polinomials

//nplanes should be 9 here!
std::vector<float> &proj = mat.proj;
proj.resize(nplanes*dim, 0.0);
for(uint32_t p = 0; p < nplanes; p++) {
proj.resize((nplanes-3)*ndimensions, 0.0);
for(uint32_t p = 0; p < nplanes-3; p++) {
for(uint32_t k = 0; k < lights.size(); k++) {
uint32_t off = k*3 + p*dim;
if(p >= 3) {
proj[off+0] = float(0.2125*iA(p-3, k));
proj[off+1] = float(0.7154*iA(p-3, k));
proj[off+2] = float(0.0721*iA(p-3, k));
} else {
proj[off + p] = 1.0f/lights.size();
}
uint32_t off = k + p*ndimensions;
proj[off] = float(iA(p, k));
}
}
} else {
Expand Down Expand Up @@ -583,10 +576,17 @@ void RtiBuilder::finalizeMaterial() {
//tested different scales for different coefficients (taking into account quantization for instance)
// for all datasets the quality/space is worse.
//rangecompress allows better quality at a large cost.
int c = 0;
for(Material::Plane &plane: material.planes) {
plane.scale = rangecompress*(plane.max - plane.min) + (1 - rangecompress)*maxscale;
plane.bias = -plane.min/plane.scale;
plane.scale /= 255.0f;
if(colorspace == LRGB && c < 3) { //no need to scale the base image.
plane.scale = 1;
plane.bias = 0;
plane.scale = 1;
}
c++;
}

if(callback && !(*callback)("Coefficients quantization:", 100))
Expand Down Expand Up @@ -1322,61 +1322,6 @@ size_t RtiBuilder::save(const string &output, int quality) {

futures[y] = QtConcurrent::run(&pool, [worker](){worker->run(); });
}






//classify samples
//lower error but increase size of the files (why?)
//getPixelBestMaterial(resample, indices);
//getPixelMaterial(resample, indices);
/*
for(uint32_t x = 0; x < width; x++) {
uint32_t m = indices[x];
Material &mat = materials[m];
segments.setPixel(x, y, m);
vector<float> pri = toPrincipal(m, (float *)(resample(x)));
if (savenormals) {
Vector3f n = getNormalThreeLights(pri);
normals.setPixel(x, y, qRgb(255 * n[0], 255 * n[1], 255 * n[2]));
}
if(savemeans) {
Vector3f n = extractMean(sample(x), lights.size());
means.setPixel(x, y, qRgb(n[0], n[1], n[2]));
}
if(savemedians) {
Vector3f n = extractMedian(sample(x), lights.size());
medians.setPixel(x, y, qRgb(n[0], n[1], n[2]));
}
if(colorspace == LRGB){
for(uint32_t j = 0; j < nplanes/3; j++) {
for(uint32_t c = 0; c < 3; c++) {
uint32_t p = j*3 + c;
if(j >= 1)
pri[p] = mat.planes[p].quantize(pri[p]);
line[j][x*3 + c] = pri[p];
}
}
} else {
for(uint32_t j = 0; j < nplanes/3; j++) {
for(uint32_t c = 0; c < 3; c++) {
uint32_t p = j*3 + c;
line[j][x*3 + c] = mat.planes[p].quantize(pri[p]);
}
}
}
}*/
}

size_t total = 0;
Expand Down Expand Up @@ -1448,7 +1393,6 @@ void RtiBuilder::processLine(PixelArray &sample, PixelArray &resample, std::vect
}

if(colorspace == LRGB){

for(uint32_t j = 0; j < nplanes/3; j++) {
for(uint32_t c = 0; c < 3; c++) {
uint32_t p = j*3 + c;
Expand Down Expand Up @@ -1838,41 +1782,29 @@ std::vector<float> RtiBuilder::toPrincipal(Pixel &pixel, MaterialBuilder &materi

if(colorspace == LRGB) {

for(size_t p = 0; p < nplanes; p++) {
for(size_t k = 0; k < dim; k++) {
res[p] += v[k] * materialbuilder.proj[k + p*dim];
}
}

//get average luminance
vector<float> luma(ndimensions);
float max = 0.0;
//get average color, but penalize top and raking lights.
Color3f mean(0, 0, 0);
float weight = 0;
for(uint32_t i = 0; i < ndimensions; i++) {
luma[i] = (0.2125f*v[i*3+0] + 0.7154f*v[i*3+1] + 0.0721f*v[i*3+2])/(255.0f);
max = std::max(luma[i], max);
Color3f &c = pixel[i];
float w = 0.25 - pow(asin(lights[i][2])/(M_PI/2) - 0.5, 2);
mean += c*w;
weight += w;
}
if(max > 0)
for(float &l: luma)
l /= max;
mean /= weight;

float r = 0.0, g = 0.0, b = 0.0, y = 0.0;
for(uint32_t i = 0; i < ndimensions; i++) {
r += (v[i*3+0]/255.0)*(luma[i]);
g += (v[i*3+1]/255.0)*(luma[i]);
b += (v[i*3+2]/255.0f)*(luma[i]);
y += luma[i]*luma[i];
}
res[0] = std::max(0.0, std::min(255.0, 255.0*r/y));
res[1] = std::max(0.0, std::min(255.0, 255.0*g/y));
res[2] = std::max(0.0, std::min(255.0, 255.0*b/y));
float luma = (mean[0] + mean[1] + mean[2])/255; //actually 3 times luma, but balances in the equation below.

// double totluma = (res[0]+ res[1] + res[2])/(3.0 * 255.0);
//fit luminosity.
for(size_t p = 3; p < nplanes; p++)
for(size_t k = 0; k < ndimensions; k++)
res[p] += ((v[k*3] + v[k*3+1] + v[k*3+2])/luma)* materialbuilder.proj[k + (p-3)*ndimensions];

double totluma = (0.2125*res[0]+ 0.7154*res[1] + 0.0721*res[2])/255.0;

for(uint32_t p = 3; p < nplanes; p++)
res[p] /= totluma;
res[0] = mean[0];
res[1] = mean[1];
res[2] = mean[2];



} else { //RGB, YCC
vector<float> col(dim);
Expand Down
24 changes: 13 additions & 11 deletions relight/rtiexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,27 @@ void RtiExport::showImage(QPixmap pix) {

void RtiExport::changeBasis(int n) {
switch(n) {
case 0: //ptm
case 0: //ptm
ui->planes->setValue(9);
break;
case 1: //ptm
ui->planes->setValue(18);
break;
case 1: //hsh 4
case 2: //hsh 4
ui->planes->setValue(12);
break;
case 2: //hsh 27
case 3: //hsh 27
ui->planes->setValue(27);
break;
case 3: //bilinear
case 4: //bilinear
break;
case 4: //rbf
case 5: //rbf
break;
case 5: //yrbf
case 6: //yrbf
break;
}
ui->chroma->setEnabled(n == 5 || n == 6);
ui->planes->setEnabled(n >= 3);
ui->chroma->setEnabled(n == 6 || n == 7);
ui->planes->setEnabled(n >= 4);
}

void RtiExport::changePlanes(int n) {
Expand All @@ -138,15 +141,14 @@ void RtiExport::changePlanes(int n) {

Rti::Type basis(int index) {
//int b = ui->basis->currentIndex();
Rti::Type table[] = { Rti::PTM, Rti::HSH, Rti::HSH, Rti::BILINEAR, Rti::RBF, Rti::BILINEAR, Rti::RBF, Rti::DMD, Rti::SH, Rti::H };
Rti::Type table[] = { Rti::PTM, Rti::PTM, Rti::HSH, Rti::HSH, Rti::BILINEAR, Rti::RBF, Rti::BILINEAR, Rti::RBF, /* to do! */ Rti::DMD, Rti::SH, Rti::H };
return table[index];
}

Rti::ColorSpace colorSpace(int index) {
//int b = ui->basis->currentIndex();
Rti::ColorSpace table[] = { Rti::RGB, Rti::RGB, Rti::RGB, Rti::MRGB, Rti::MRGB, Rti::MYCC, Rti::MYCC };
Rti::ColorSpace table[] = { Rti::LRGB, Rti::RGB, Rti::RGB, Rti::RGB, Rti::MRGB, Rti::MRGB, Rti::MYCC, Rti::MYCC };
return table[index];

}


Expand Down
10 changes: 9 additions & 1 deletion relight/rtiexport.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<item>
<widget class="QTabWidget" name="export_frame">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="tabWidgetPage1">
<attribute name="title">
Expand All @@ -49,6 +49,14 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="currentIndex">
<number>3</number>
</property>
<item>
<property name="text">
<string>LPTM 9 - Polinomial tecture map</string>
</property>
</item>
<item>
<property name="text">
<string>PTM 18 - Polinomial texture map</string>
Expand Down
2 changes: 1 addition & 1 deletion relight/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Task: public QThread {
void runScript(QString program, QString script, QStringList arguments, QString workingdir = QString());
public slots:
void test() {}
virtual bool progressed(std::string str, int percent){return false;};
virtual bool progressed(std::string /*str*/, int /*percent*/){return false;};

signals:
void progress(QString str, int n);
Expand Down
15 changes: 13 additions & 2 deletions src/relight_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,18 @@ template <class T> struct Color3 {
}
Color3 clip() {
return Color3(r / 255, g / 255, b / 255);
}
}
void operator+=(const Color3 &c) {
r += c.r;
g += c.g;
b += c.b;
}
void operator-=(const Color3 &c) {
r -= c.r;
g -= c.g;
b -= c.b;
}

void operator*=(float v) {
r *= v;
g *= v;
Expand All @@ -119,7 +130,7 @@ template <class T> struct Color3 {
return Color3(r / v, g / v, b / v);
}
Color3 operator*(float v) {
return Color3(r / v, g / v, b / v);
return Color3(r * v, g * v, b * v);
}
float mean() {
return (r + g + b)/3;
Expand Down

0 comments on commit 4ab3407

Please sign in to comment.