Skip to content

Commit

Permalink
preprocess scanning data scripts;
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwdzh committed Mar 15, 2020
1 parent 8c9a898 commit 7dae4f5
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 182 deletions.
1 change: 1 addition & 0 deletions src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data_path = '/orion/downloads/AdversarialTexture/data'
4 changes: 3 additions & 1 deletion src/preprocessing/CudaRender/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ void FrameBuffer::Initialize(int rows, int cols) {
void FrameBuffer::ClearBuffer() {
cudaMemset(d_z, 0, sizeof(int) * row * col);
cudaMemset(d_depth, 0, sizeof(float) * row * col);
cudaMemset(d_findices, 0, sizeof(int) * row * col);
//cudaMemset(d_findices, 0, sizeof(int) * row * col);
std::vector<int> findices(row * col, -1);
cudaMemcpy(d_findices, findices.data(), sizeof(int) * row * col, cudaMemcpyHostToDevice);
cudaMemset(d_vweights, 0, sizeof(glm::vec3) * row * col);
cudaMemset(d_vindices, 0, sizeof(glm::ivec3) * row * col);
cudaMemset(d_colors, 0, sizeof(int) * row * col);
Expand Down
181 changes: 0 additions & 181 deletions src/preprocessing/Rasterizer/paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,185 +108,4 @@ void PaintToViewNorm(glm::vec3* textureToImage, glm::vec3* points_cam, glm::vec3
}
}

void PaintSharpnessToView(float* sharp_candidates, int* frame_candidates, int* coordx, int* coordy, float* sharpness, glm::vec3* points_cam, unsigned char* mask,
float* depth, glm::ivec2* coords, int num_points, int height, int width, int tex_width, int num_candidates, int frame_id) {
for (int j = 0; j < num_points; ++j) {
glm::vec3& p = points_cam[j];
int px = p[0], py = p[1];
if (p[0] < 0 || p[1] < 0 || p[0] >= 639 || p[1] >= 479)
continue;
if (mask[py * width + px] == 0 || mask[py * width + px + 1] == 0 || mask[(py + 1) * width + px] == 0 || mask[(py + 1) * width + px + 2] == 0)
continue;
float max_depth = depth[py * width + px];
max_depth = std::max(max_depth, depth[(py + 1) * width + px]);
max_depth = std::max(max_depth, depth[py * width + px + 1]);
max_depth = std::max(max_depth, depth[(py + 1) * width + px + 1]);
if (p[2] <= max_depth) {
float sharp_value = sharpness[py * width + px];
int f = coords[j][1] * tex_width + coords[j][0];

int* candidate_list = frame_candidates + f * num_candidates;
int* cx_list = coordx + f * num_candidates;
int* cy_list = coordy + f * num_candidates;
float* sharp_list = sharp_candidates + f * num_candidates;

// check existence
bool found = false;
for (int k = 0; k < num_candidates; ++k) {
if (candidate_list[k] == -1)
break;
if (candidate_list[k] == frame_id) {
found = true;
if (sharp_value > sharp_list[k]) {
sharp_list[k] = sharp_value;
int current_k = k;
while (current_k > 0 && sharp_list[current_k] > sharp_list[current_k - 1]) {
std::swap(sharp_list[current_k], sharp_list[current_k - 1]);
std::swap(candidate_list[current_k], candidate_list[current_k - 1]);
std::swap(cx_list[current_k], cx_list[current_k - 1]);
std::swap(cy_list[current_k], cy_list[current_k - 1]);
current_k -= 1;
}
}
break;
}
}
if (found)
continue;
for (int k = 0; k < num_candidates; ++k) {
if (candidate_list[k] == -1 || sharp_list[k] < sharp_value) {
for (int k1 = num_candidates - 1; k1 > k; k1--) {
sharp_list[k1] = sharp_list[k1 - 1];
candidate_list[k1] = candidate_list[k1 - 1];
cx_list[k1] = cx_list[k1 - 1];
cy_list[k1] = cy_list[k1 - 1];
}
sharp_list[k] = sharp_value;
candidate_list[k] = frame_id;
cx_list[k] = px;
cy_list[k] = py;
break;
}
}
}
}
}

void PaintSharpnessFromView(float* sharp_candidates, int* frame_candidates, int* coords,
float* sharp, float* uv, unsigned char* mask, int image_width, int image_height, int tex_width, int tex_height,
int num_points, int num_candidates, int frame_id) {
for (int i = 0; i < image_height; ++i) {
for (int j = 0; j < image_width; ++j) {
int image_offset = i * image_width + j;
if (mask[image_offset] == 0)
continue;
float x = uv[image_offset * 2];
float y = uv[image_offset * 2 + 1];
if (x == 0 && y == 0) {
continue;
}
float sharp_value = sharp[image_offset];
x = x * (tex_width - 1);
y = (1 - y) * (tex_height - 1);
int px = x, py = y;
for (int dy = 0; dy < 2; ++dy) {
for (int dx = 0; dx < 2; ++dx) {
if (px + dx < tex_width && py + dy < tex_height && px + dx >= 0 && py + dy >= 0) {
int f = coords[(py + dy) * tex_width + (px + dx)];
if (f == -1)
continue;
int* candidate_list = frame_candidates + f * num_candidates;
float* sharp_list = sharp_candidates + f * num_candidates;

// check existence
bool found = false;
for (int k = 0; k < num_candidates; ++k) {
if (candidate_list[k] == -1)
break;
if (candidate_list[k] == frame_id) {
found = true;
if (sharp_value > sharp_list[k]) {
sharp_list[k] = sharp_value;
int current_k = k;
while (current_k > 0 && sharp_list[current_k] > sharp_list[current_k - 1]) {
std::swap(sharp_list[current_k], sharp_list[current_k - 1]);
std::swap(candidate_list[current_k], candidate_list[current_k - 1]);
current_k -= 1;
}
}
break;
}
}
if (found)
continue;
for (int k = 0; k < num_candidates; ++k) {
if (candidate_list[k] == -1 || sharp_list[k] < sharp_value) {
for (int k1 = num_candidates - 1; k1 > k; k1--) {
sharp_list[k1] = sharp_list[k1 - 1];
candidate_list[k1] = candidate_list[k1 - 1];
}
sharp_list[k] = sharp_value;
candidate_list[k] = frame_id;
break;
}
}

}
}
}
}
}
}

void ValidateSharpnessFromView(float* sharp_candidates, int* frame_candidates, int* coords,
float* sharp, float* uv, unsigned char* mask, int image_width, int image_height, int tex_width, int tex_height,
int num_points, int num_candidates, int frame_id) {
for (int i = 0; i < image_height; ++i) {
for (int j = 0; j < image_width; ++j) {
int image_offset = i * image_width + j;
if (mask[image_offset] == 0)
continue;
float x = uv[image_offset * 2];
float y = uv[image_offset * 2 + 1];
if (x == 0 && y == 0) {
continue;
}
float sharp_value = sharp[image_offset];
x = x * (tex_width - 1);
y = (1 - y) * (tex_height - 1);
int px = x, py = y;
bool found = false;
for (int dy = 0; dy < 2; ++dy) {
for (int dx = 0; dx < 2; ++dx) {
if (px + dx < tex_width && py + dy < tex_height && px + dx >= 0 && py + dy >= 0) {
int f = coords[(py + dy) * tex_width + (px + dx)];
if (f == -1)
continue;
int* candidate_list = frame_candidates + f * num_candidates;
float* sharp_list = sharp_candidates + f * num_candidates;
if (sharp_list[num_candidates - 1] <= sharp_value)
found = true;
}
}
}
if (!found)
mask[image_offset] = 0;
}
}
}

void ComputeMask(int* coordx, int* coordy, int* frames, unsigned char* masks,
int tex_width, int tex_height, int num_candidates, int image_width, int image_height) {
for (int i = 0; i < tex_height; ++i) {
for (int j = 0; j < tex_width; ++j) {
for (int k = 0; k < num_candidates; ++k) {
int offset = (i * tex_width + j) * num_candidates + k;
int fid = frames[offset];
int cx = coordx[offset];
int cy = coordy[offset];
masks[(fid * image_height + cy) * image_width + cx] = 255;
}
}
}
}
};
Empty file added src/preprocessing/__init__.py
Empty file.
64 changes: 64 additions & 0 deletions src/preprocessing/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from ctypes import *
import numpy as np
Sens = cdll.LoadLibrary('./Rasterizer/libSens.so')

def LoadSens(filename):
print(filename)
Sens.Parse(c_char_p(filename.encode('utf8')))
depth_width = Sens.DW()
depth_height = Sens.DH()
color_width = Sens.CW()
color_height = Sens.CH()
frames = Sens.Frames()
depths = np.zeros((frames, depth_height, depth_width), dtype='float32')
colors = np.zeros((frames, color_height, color_width, 3), dtype='uint8')
cam2worlds = np.zeros((frames, 4, 4), dtype='float32')
intrinsic = np.zeros((4,4), dtype='float32')

Sens.GetData(c_void_p(depths.ctypes.data), c_void_p(colors.ctypes.data), c_void_p(cam2worlds.ctypes.data), c_void_p(intrinsic.ctypes.data))
Sens.Clear()
depths = np.nan_to_num(depths)
return colors, depths, cam2worlds, intrinsic

def LoadOBJ(filename):
lines = [l.strip() for l in open(filename)]
V = []
VT = []
VN = []
F = []
FT = []
FN = []
for l in lines:
words = [w for w in l.split(' ') if w != '']
if words[0] == 'v':
V.append([float(words[1]), float(words[2]), float(words[3])])
elif words[0] == 'vt':
VT.append([float(words[1]), float(words[2])])
elif words[0] == 'vn':
VN.append([float(words[1]), float(words[2]), float(words[3])])
elif words[0] == 'f':
f = []
ft = []
fn = []
for j in range(1, 4):
w = words[j].split('/')
f.append(int(w[0])-1)
ft.append(int(w[1])-1)
fn.append(int(w[2])-1)
F.append(f)
FT.append(ft)
FN.append(fn)

V = np.array(V, dtype='float32')
VT = np.array(VT, dtype='float32')
VN = np.array(VN, dtype='float32')
F = np.array(F, dtype='int32')
FT = np.array(FT, dtype='int32')
FN = np.array(FN, dtype='int32')

return V, F, VT, FT, VN, FN

if __name__ == "__main__":
import sys
LoadSens(sys.argv[1])
V,F,VT,FT,_,_ = LoadOBJ(sys.argv[2])
20 changes: 20 additions & 0 deletions src/preprocessing/painter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from ctypes import *
import numpy as np
Painter = cdll.LoadLibrary('./Rasterizer/libPainter.so')

def ProjectPaint(points, normals, point_colors, color, depth, world2cam, intrinsic):
Painter.ProjectPaint(c_void_p(points.ctypes.data), c_void_p(normals.ctypes.data), c_void_p(point_colors.ctypes.data),
c_void_p(color.ctypes.data), c_void_p(depth.ctypes.data), c_void_p(world2cam.ctypes.data), c_void_p(intrinsic.ctypes.data),
c_int(points.shape[0]), c_int(depth.shape[1]), c_void_p(depth.shape[0]), c_void_p(color.shape[1]), c_void_p(color.shape[0]))

def PaintToTexturemap(texturemap, point_colors, coords):
Painter.PaintToTexturemap(c_void_p(texturemap.ctypes.data), c_void_p(point_colors.ctypes.data), c_void_p(coords.ctypes.data),
c_int(point_colors.shape[0]), c_int(texturemap.shape[1]), c_int(texturemap.shape[0]))

def PaintToViewNorm(points_cam, normals_cam, mask, depth, coords, textureToImage):
Painter.PaintToViewNorm(c_void_p(textureToImage.ctypes.data), c_void_p(points_cam.ctypes.data), c_void_p(normals_cam.ctypes.data), c_void_p(mask.ctypes.data),
c_void_p(depth.ctypes.data), c_void_p(coords.ctypes.data), c_int(points_cam.shape[0]), c_int(mask.shape[0]), c_int(mask.shape[1]), c_int(textureToImage.shape[1]))

def PaintToView(points_cam, mask, depth, coords, textureToImage):
Painter.PaintToView(c_void_p(textureToImage.ctypes.data), c_void_p(points_cam.ctypes.data), c_void_p(mask.ctypes.data),
c_void_p(depth.ctypes.data), c_void_p(coords.ctypes.data), c_int(points_cam.shape[0]), c_int(mask.shape[0]), c_int(mask.shape[1]), c_int(textureToImage.shape[1]))
45 changes: 45 additions & 0 deletions src/preprocessing/rasterizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from ctypes import *
import numpy as np
import os
libpath = os.path.dirname(os.path.abspath(__file__))

Rasterizer = cdll.LoadLibrary(libpath + '/Rasterizer/libRasterizer.so')

def RasterizeTexture(VT, FT, height, width):
color = np.zeros((height,width,3), dtype='float32')
zbuffer = np.zeros((height, width), dtype='float32')
findices = np.zeros((height, width), dtype='int32')
findices[:,:] = -1
Rasterizer.RasterizeTexture(c_void_p(VT.ctypes.data), c_void_p(FT.ctypes.data), c_void_p(color.ctypes.data),
c_void_p(zbuffer.ctypes.data), c_void_p(findices.ctypes.data), c_int(FT.shape[0]), width, height)

return color, findices


def RasterizeImage(V, F, width, height, intrinsic, world2cam):
vweights = np.zeros((height, width, 3), dtype='float32')
findices = np.zeros((height, width), dtype='int32')
zbuffer = np.zeros((height, width), dtype='float32')
findices[:,:] = -1
Rasterizer.RasterizeImage(c_void_p(V.ctypes.data), c_void_p(F.ctypes.data), c_void_p(world2cam.ctypes.data), c_void_p(intrinsic.ctypes.data),
c_void_p(vweights.ctypes.data), c_void_p(zbuffer.ctypes.data), c_void_p(findices.ctypes.data), c_int(F.shape[0]), c_int(width), c_int(height))
return vweights, findices

def GeneratePoints(V, F, VN, FN, vweights, findices):
num_points = np.sum(findices >= 0)
points = np.zeros((num_points, 3), dtype='float32')
normals = np.zeros((num_points, 3), dtype='float32')
coords = np.zeros((num_points, 2), dtype='int32')
Rasterizer.GenerateTextiles(c_void_p(V.ctypes.data), c_void_p(F.ctypes.data), c_void_p(VN.ctypes.data), c_void_p(FN.ctypes.data),
c_void_p(points.ctypes.data), c_void_p(normals.ctypes.data), c_void_p(coords.ctypes.data),
c_void_p(findices.ctypes.data), c_void_p(vweights.ctypes.data), c_int(findices.shape[1]), c_int(findices.shape[0]))
return points, normals, coords

def RenderUV(vweights, findices, VT, FT):
uv = np.zeros((vweights.shape[0], vweights.shape[1], 3), dtype='float32')
#void RenderUV(glm::vec3* uv, glm::vec3* vweights, int* findices, glm::vec2* VT, glm::ivec3* FT, int width, int height)

Rasterizer.RenderUV(c_void_p(uv.ctypes.data), c_void_p(vweights.ctypes.data), c_void_p(findices.ctypes.data),
c_void_p(VT.ctypes.data), c_void_p(FT.ctypes.data),
c_int(uv.shape[1]), c_int(uv.shape[0]))
return uv
Loading

0 comments on commit 7dae4f5

Please sign in to comment.