Python filters images through. Cube LUT files

Sauce_ oil 2021-10-14 08:08:04

Python adopt .cube LUT File filters the image

A good one python Add filter code to the picture : 

https://github.com/CKboss/PyApplyLUT

This is the C++ Encapsulation of code ,  And used openmp In parallel ,  fast , 4k Pictures and filters are tested locally as long as they are less than 0.2 second .

Need to compile .  rely on pybind11 and eigen. Fortunately, these two libraries are the kind that can be used only with header files .  Go to the official website for a good source code (pybind11 2.7.1, eigen 3.4), stay CMakeLists Named in the pybind11 and eigen The path of , Just compile it .

obtain .so After the document ,  You need to put it in python Where you can find .  Write the path directly here .

 

Usage is as follows :

 1 import cv2
 2 import numpy as np
 3 from pathlib2 import Path
 4
 5 import sys
 6 # the path of .so where python can find it
 7 sys.path.append("Q:/WorkSpace/bfood/lut-master/build/Debug")
 8 from python.PyApplyLUT import PyApplyLUT
 9 from python.lut_tools import cube_to_npy
10
11 INPUT_IMG = Path(r".\test\1.jpg")
12 LUT_FILE = Path(r".\test\1.cube")
13
14 # normlizer the input picture to 0~1
15 img = cv2.imread(INPUT_IMG.as_posix())
16 img = img / 255
17
18 # apply lut 
19
20 # method 1 load lut from a .cube file
21 alut = PyApplyLUT(lut_file=LUT_FILE)
22 new_img = alut.apply_lut(img)
23 # recover to 0~255
24 new_img = new_img * 255
25 cv2.imwrite("./test/new_img_1.jpg",new_img)
26
27 # method 2 load lut from the np array
28 cubenpy = cube_to_npy(LUT_FILE)
29 alut = PyApplyLUT(lut_dim=32, lut_cube=cubenpy)
30 new_img = alut.apply_lut(img)
31 # recover to 0~255
32 new_img = new_img * 255
33 cv2.imwrite("./test/new_img_2.jpg",new_img)

 

design sketch :

 Before conversion ( Original picture )   ----->     After the transformation ( Add filter )

 

There are two uses : 

1.  Use .cube file

Filter (.cube) The file format is as follows :  The value in it is 0~1 Between

# Created by Adobe Lightroom plugin Export LUT (1.17.0)
LUT_3D_SIZE 32
DOMAIN_MIN 0.0 0.0 0.0
DOMAIN_MAX 1.0 1.0 1.0
0.000000 0.000000 0.000000
0.047791 0.000000 0.000000
0.080140 0.000000 0.000000
0.118013 0.000000 0.000000
0.169955 0.000000 0.000000
...

The input image should also be normalized to 0~1 Between ,  The final output should be zoomed back to 0~255

 

2.  Use one numpy Filter file of array lattice test

The format is 3,32,32,32 Such an array ,  .cube The switch to npy The code for is as follows :

def load_lut_file_to_input_cube(cube_path,dim=None):
with open(cube_path,'r') as f:
lines = f.readlines()
for i in range(len(lines)):
lines[i] = lines[i].strip()
if dim is None:
if 'LUT_3D_SIZE' in lines[i]:
dim = int(lines[i].split(' ')[-1])
lines = lines[-dim*dim*dim:]
cube = np.zeros((3,dim,dim,dim),dtype=np.float32)
for i in range(0,dim):
for j in range(0,dim):
for k in range(0,dim):
n = i * dim*dim + j * dim + k
line = lines[n].split(' ')
x = line
try:
cube[0,i,j,k] = float(x[0]) # r
cube[1,i,j,k] = float(x[1]) # g
cube[2,i,j,k] = float(x[2]) # b
except Exception:
print(lines[n])
cube = np.array(cube,dtype=np.float32)
return cube

 

Please bring the original link to reprint ,thank
Similar articles