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 :

   ----->    

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

Python adopt .cube LUT File more articles about adding filters to images

  1. pure Python to ulaw wav File header

    I've been working on it recently wav Related documents , When you meet a tool ualw The document does not come with header, Handy python to wav Format file header processing , Let ordinary players play . ( original text :http://www.cnblogs.com/ryh ...

  2. python Medium pth The function of documents

    python There is one of them. .pth file , The usage of this file is : First xxx.pth There will be some paths written in the file , A line of one . take xxx.pth The files are in a specific location , Then you can make python When loading modules , Read xxx.pth Specified in the ...

  3. The third chapter :Python Basics の Function and file operation

    Topic of this lesson Set Assemble and operate the actual combat Function introduction and operation practice In depth introduction of parameters and actual operation format  Function operation lambda Expression Introduction File operation function introduction and operation practice Homework this week Set Assemble and operate the actual combat Se ...

  4. Study with me opencv Lesson 1 image loading , modify , preservation

    Use opencv Remember to import the storage and header file : #include<opencv2\opencv.hpp> 1. Load image (cv::imread)(OPENCV Support JPG,PNG,TIFF And so on ...

  5. python Part VI document processing types

    Read the directory One File operations Two Open file mode 3、 ... and How to operate the file Four Cursor movement in file 5、 ... and File modification   Document processing                                             ...

  6. 【 note 】Python Foundation three : file

    One , File operations ( One ), Document processing flow 1, Open file , Get file handle (open Function provides ) And the assignment 2, Operate on the file through the handle 3, Closing handle f = open(' Chen grain ',encoding='utf-8')#op ...

  7. python Character encoding and file processing

    One . Understand the knowledge reserve of character encoding 1. How text editors access files (nodepad++,python,word) Open the editor and start a process , It's in memory , therefore , The content written in the editor is also stored in memory , break ...

  8. python Reading and writing 、 establish Method of file ( Must see )

    python Reading and writing . establish Method of file ( Must see ) Update time :2016 year 09 month 12 Japan 10:26:41 contribute :jingxian I would like to comment on the following small series to bring you an article python Reading and writing . establish Method of file ( Must see ). ...

  9. python The journey : Document processing

    One Document operation and theory 1. Introduce The computer system is divided into : computer hardware . operating system . The three parts of the application we use python Or other programs , Want to keep the data forever , You have to write it to the hard disk , But there is no way for applications to directly operate the hardware , This is ...

  10. centos Regular ,grep,egrep, Streaming editor sed,awk -F Multiple separators wildcard Special symbols . * + ? summary Ask Jiaxing cat -n nl Output the contents of the file and add the line number alias discharge ~/.bash_profile 2015-4-10 Lesson 13

    centos Regular ,grep,egrep, Streaming editor sed,awk -F Multiple separators   wildcard Special symbols . * + ? summary   Ask Jiaxing cat -n  nl   Output the contents of the file and add the line number alias discharge ~ ...

Random recommendation

  1. virtual machine virtualBox After setting up shared files ,linux To configure

    1. stay /mnt Create a shared directory under mkdir /mnt/share 2. Associate external directory mount -t vboxsf Shared folder name /mnt/share/ Such as :mount -t vboxsf BaiduS ...

  2. cognos Report making process

    Chapter one   Create report Make ReportStudio report form , The steps are as follows : Chapter two   Add query 1. Click query explorer in the middle column , Then click query , You can create a new query . The steps are as follows : 2. Click to query the page that appears : 3. stay ...

  3. Android Unable to receive the problem summary for the specified broadcast

    First, there can't be only one broadcast in the application , There has to be one activity perhaps service( This situation is complicated http://www.cnblogs.com/welcoming/p/4120497.html) ordinary ...

  4. Hadoop Basic course of distributed environment construction

    front , We've put... On a stand-alone computer Hadoop It's working , But we know that Hadoop Support distributed , And its advantage is that it is outstanding in distribution , So we have to build an environment to simulate . ad locum , We use this strategy to simulate the environment , We use 3 platform ubu ...

  5. Hibernate Have Mybits Of SQL/HQL characteristic ( annotation 、XML No mistake )

         Blog for the first time . The article is a bit of a mess , If you like, just have a look , I don't like passing by and giving a compliment .      effect : There are many ways to use a sentence directly     FROM User A    WHERE    1=1    <#if id??&g ...

  6. php Realize web page HTML Label completion method

    If your web content html Incomplete label display , Some form labels are incomplete, resulting in page confusion , Or put parts outside your content html The page is included , We can write a function method to complete html Label and filter out useless html label . php ...

  7. C# Struct Structure

    C# Structure type and class type are very similar in syntax , They're all data structures , Both can include data members and method members . The difference between structure and class : 1. A structure is a value type , It allocates space in the stack : And classes are reference types , It allocates space in the heap , All that's stored in the stack is references ...

  8. Apache shiro Cluster implementation ( Two ) shiro Of INI To configure

    Apache shiro Cluster implementation ( One ) shiro Introduction Apache shiro Cluster implementation ( Two ) shiro Of INI To configure Apache shiro Cluster implementation ( 3、 ... and )shiro Identity Authentication (Shiro ...

  9. Database usage cpu Higher query

    Recently, I saw other people's database queries cpu Take up a lot of sql sentence ( I am sql Not good ), So I checked the information , Make a note of , Easy to understand and apply . First , Post the statement here SELECT TOP 10 -- Average cpu Time tot ...

  10. Django: For full-text search function, please refer to blog

    https://blog.csdn.net/AC_hell/article/details/52875927 https://www.zmrenwu.com/courses/django-blog-t ...