110 lines
3.1 KiB
Python
110 lines
3.1 KiB
Python
# -*- coding: UTF-8 -*-
|
|
"""
|
|
@Project:__init__.py
|
|
@File:pspFreemanDecomposition.py
|
|
@Function:
|
|
@Contact:
|
|
@Author:LVY
|
|
@Date:2021/10/12 18:45
|
|
@Version:1.0.0
|
|
"""
|
|
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import logging
|
|
logger = logging.getLogger("mylog")
|
|
|
|
|
|
class PspFreemanDecomposition:
|
|
"""
|
|
Freeman分解
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
exeDir,
|
|
inT3Dir,
|
|
outDir,
|
|
exeDecomposeName='freeman_decomposition_T3.exe'):
|
|
"""
|
|
:param exeDir:exe所在目录
|
|
:param inT3Dir:T3矩阵目录
|
|
:param outDir:输出目录
|
|
"""
|
|
self.__exeName = exeDecomposeName
|
|
self.__exeDir = exeDir
|
|
self.__inT3Dir = inT3Dir
|
|
self.__outDir = outDir
|
|
self.__DecompostFlag = False
|
|
pass
|
|
|
|
def api_freeman_decomposition_T3(
|
|
self,
|
|
rectX,
|
|
rectY,
|
|
rectWidth,
|
|
rectHeight,
|
|
Nwin=1):
|
|
"""
|
|
:param rectX:有效区域x
|
|
:param rectY:有效区域y
|
|
:param rectWidth:有效区域宽
|
|
:param rectHeight:有效区域高
|
|
:param Nwin :Size of the (Nwin, Nwin) sliding window used to compute local estimates. (int)
|
|
"""
|
|
if self.__DecompostFlag:
|
|
return True
|
|
if len(self.__exeDir) == 0:
|
|
if not os.path.exists(self.__exeName):
|
|
logger.error(self.__exeName + ' not exists.')
|
|
return False
|
|
exePath = self.__exeName
|
|
else:
|
|
if not os.path.exists(self.__exeDir + '\\' + self.__exeName):
|
|
logger.error(self.__exeName + ' not exists.')
|
|
return False
|
|
exePath = self.__exeDir + '\\' + self.__exeName
|
|
|
|
if not self._checkT3Matrix(self.__inT3Dir):
|
|
logger.error('T3 Matrix check failed.')
|
|
return False
|
|
if not os.path.exists(self.__outDir):
|
|
os.makedirs(self.__outDir)
|
|
|
|
Off_lig = rectX
|
|
Off_col = rectY
|
|
Sub_Nlig = rectWidth
|
|
Sub_Ncol = rectHeight
|
|
|
|
para_list = [
|
|
exePath,
|
|
self.__inT3Dir,
|
|
self.__outDir,
|
|
Nwin,
|
|
Off_lig,
|
|
Off_col,
|
|
Sub_Nlig,
|
|
Sub_Ncol]
|
|
cmd = " ".join(str(i) for i in para_list)
|
|
config_path = os.path.join(self.__inT3Dir, 'config.txt')
|
|
shutil.copyfile(config_path, os.path.join(self.__outDir, 'config.txt'))
|
|
result_tuple = subprocess.getstatusoutput(cmd)
|
|
|
|
if result_tuple[0] != 1 or result_tuple[1].find('error') != -1:
|
|
raise Exception(result_tuple[1])
|
|
self.__DecompostFlag = True
|
|
return True
|
|
@staticmethod
|
|
def _checkT3Matrix(T3Dir):
|
|
# 检测T3矩阵
|
|
if not os.path.exists(T3Dir):
|
|
return False
|
|
file_name_in_out = ['T11.bin', 'T12_real.bin', 'T12_imag.bin',
|
|
'T13_real.bin', 'T13_imag.bin', 'T22.bin',
|
|
'T23_real.bin', 'T23_imag.bin', 'T33.bin']
|
|
for item in file_name_in_out:
|
|
if not os.path.exists(T3Dir + "\\" + item):
|
|
return False
|
|
return True
|