Skip to content

Commit

Permalink
merge from jdt
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinNose committed Nov 17, 2022
2 parents ed9a8ef + a42418f commit 9a26da4
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ src/objs
build
.DS_Store
*.exr
.ipynb_checkpoints
82 changes: 82 additions & 0 deletions src/rpf/RPF.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "RPF.h"

void initializeFromPbrt(FeatureVector* pbrtSamples, size_t pbrtWidth,
size_t pbrtHeight, size_t pbrtSpp, int numFeatures) {
// Define in header
samples = pbrtSamples;
width = pbrtWidth;
height = pbrtHeight;
spp = pbrtSpp;
mFeatures = numFeatures;
}

// Algorithm 1: Random Parameter Filtering (RPF) Algorithm
void RPF(CImg<float>* img) {
boxSizes = new int[4];
boxSizes[0] = 55;
boxSizes[1] = 35;
boxSizes[2] = 17;
boxSizes[3] = 7;
for (int t = 0; t < 4; t++) {
int b = boxSizes[t];
size_t maxNumOfSamples = (b * b * spp) / 2;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
FeatureVector* neighborSamples;
preprocessSamples(samples, b, maxNumOfSamples, neighborSamples, j, i);
float* alpha = neighborSamples->getAlpha();
float* beta = neighborSamples->getBeta();
float newColor = computeFeatureWeights(t, neighborSamples);
float finalColor = filterColorSamples(samples, neighborSamples, alpha, beta, newColor);
samples->setColor(j, i, 0, finalColor, spp);
}
}
samples->setColor();
}
boxFilter(img);
}

// Algorithm 2: Preprocess Samples
void preprocessSamples(FeatureVector* samples, int b, size_t maxNumOfSamples,
FeatureVector* neighborSamples, int x, int y) {
float sigmaP = b / 4.0f;
neighborSamples = samples;

// Compute mean (mfP) and standard deviation (σfP) of the features of samples in pixel P for clustering

// Add samples to neighborhood
for (int q = 0; q < maxNumOfSamples - spp; q++) {
// Select a random sample j from samples inside the box but outside P with distribution based on σp
flag = true;
// Perform clustering
for (int k = 0; k < mFeatures; k++) {

}
}
// Compute normalized vector for each sample by removing mean and dividing by standard deviation
neighborSamples->getStatistics();
}

// Algorithm 3: Compute Feature Weights
float computeFeatureWeights(int t, FeatureVector* neighborSamples)

// Algorithm 4: Filter Color Samples
float filterColorSamples(FeatureVector* samples,
FeatureVector* neighborSamples,
float* alpha, float* beta, float newColor)

void boxFilter(CImg<float>* img) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
float c[3] = {0.0f, 0.0f, 0.0f};
for (size_t k = 0; k < samplesPerPixel; k++) {
c[0] += samples->getColor(i, j, k, 0);
c[1] += samples->getColor(i, j, k, 1);
c[2] += samples->getColor(i, j, k, 2);
}
for (int k = 0; k < 3; k++) {
(*img)(j, i, 0, k) = color[k] / spp;
}
}
}
}
3 changes: 3 additions & 0 deletions src/rpf/RPF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern size_t width, height, spp, sampleLength;
extern int mFeatures;
extern FeatureVector* samples;
128 changes: 128 additions & 0 deletions tools/rpf.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import struct\n",
"import cv2 as cv\n",
"from sklearn.feature_selection import mutual_info_regression\n",
"from sklearn.preprocessing import scale"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 100 8 28\n",
"[ 5.82861328e+00 1.05459538e+01 2.56491732e-02 2.56491732e-02\n",
" 4.10386808e-02 -4.00000000e+02 2.95105316e+02 -1.33042297e+01\n",
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 5.00000000e-01\n",
" 5.00000000e-01 8.00000012e-01 0.00000000e+00 0.00000000e+00\n",
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n",
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.25136793e-01\n",
" 3.10504138e-01 5.03994346e-01 0.00000000e+00 0.00000000e+00]\n"
]
}
],
"source": [
"with open('../scenes/sample.dat', 'rb') as f:\n",
" fileContent = f.read()\n",
" width, height, spp, SL = struct.unpack(\"QQQQ\", fileContent[:32])\n",
" samples = struct.unpack(\"f\" * ((len(fileContent) - 32) // 4), fileContent[32:])\n",
" raw = np.array(samples)\n",
" samples = np.reshape(raw, (width, height, spp, SL))\n",
"print(width, height, spp, SL)\n",
"print(samples[10,5,4,:]) "
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"def preprocess(x, y, b, M):\n",
" # Sample neighbors based on gaussian distribution\n",
" indexN = np.random.multivariate_normal(mean=[x, y], cov=(b/4) * np.identity(2), size=M-spp)\n",
" indexN = np.clip(indexN, 0, 100)\n",
" indexN = np.around(indexN)\n",
" randSampleInPixel = np.random.randint(7, size=(M-spp, 1))\n",
" indexN = np.concatenate((indexN,randSampleInPixel), axis = 1)\n",
" sampleMapper = lambda v: samples[int(v[0]), int(v[1]), int(v[2]),:]\n",
" N = np.apply_along_axis(sampleMapper, 1, indexN)\n",
" # TODO: cluster samples\n",
" # Standardize neighbors\n",
" N = scale(N)\n",
" return N"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-31-06a1d910b3c0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mheight\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mN\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpreprocess\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmaxNumOfSamples\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32m<ipython-input-30-77019b851b6e>\u001b[0m in \u001b[0;36mpreprocess\u001b[1;34m(x, y, b, M)\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;31m# TODO: cluster samples\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;31m# Standardize neighbors\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 12\u001b[1;33m \u001b[0mN\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mscale\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 13\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mN\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\users\\tinak\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\utils\\validation.py\u001b[0m in \u001b[0;36minner_f\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 70\u001b[0m FutureWarning)\n\u001b[0;32m 71\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m{\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0marg\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0marg\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 72\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 73\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0minner_f\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 74\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mc:\\users\\tinak\\appdata\\local\\programs\\python\\python37\\lib\\site-packages\\sklearn\\preprocessing\\_data.py\u001b[0m in \u001b[0;36mscale\u001b[1;34m(X, axis, with_mean, with_std, copy)\u001b[0m\n\u001b[0;32m 164\u001b[0m \u001b[0mXr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrollaxis\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 165\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mwith_mean\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 166\u001b[1;33m \u001b[0mXr\u001b[0m \u001b[1;33m-=\u001b[0m \u001b[0mmean_\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 167\u001b[0m \u001b[0mmean_1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnanmean\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mXr\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 168\u001b[0m \u001b[1;31m# Verify that mean_1 is 'close to zero'. If X contains very\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"boxSizes = [55, 35, 17, 7]\n",
"for t in range(4):\n",
" b = boxSizes[t]\n",
" maxNumOfSamples = (b * b * spp) // 2\n",
" for i in range(height):\n",
" for j in range(width):\n",
" N = preprocess(i, j, b, maxNumOfSamples)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

0 comments on commit 9a26da4

Please sign in to comment.