把这段代码转为python:void unwrap::Branch_cutting(Mat I,Mat mask) { Mat residue_charge=PhaseResidues(I); Mat branch_outs=BranchCuts(residue_charge,max_box_radius,mask); Mat unwrapped=FloodFill(I,branch_outs); unwrapped.copyTo(PhaseUnwrap); residue_charge.release(); branch_outs.release(); unwrapped.release(); }
时间: 2023-06-04 07:04:29 浏览: 283
把这段代码转化为 Python:
def branch_cutting(I, mask, max_box_radius):
residue_charge = phase_residues(I)
branch_outs = branch_cuts(residue_charge, max_box_radius, mask)
unwrapped = flood_fill(I, branch_outs)
phase_unwrap = unwrapped.copy()
residue_charge.release()
branch_outs.release()
unwrapped.release()
return phase_unwrap
相关问题
把这段c++代码转为python:void unwrap::reunwrap(Mat I,Mat mask,int choose) { unwraprow=I.rows; unwrapcol=I.cols; switch(choose) { case 1: RC(I,mask); break; case 2: Branch_cutting(I,mask); break; default: break; } } void unwrap::RC(Mat I,Mat mask) { int roww, coll, half; roww = I.rows;//540 coll = I.cols;//720 half = ceil(coll / 2);//360 Mat pp = Mat::zeros(roww, 1, CV_64FC1); Mat aa = Mat::zeros(1, coll, CV_64FC1); Mat bb = Mat::zeros(1, coll, CV_64FC1); Mat left = Mat::zeros(roww, half, CV_64FC1); Mat leftt = Mat::zeros(roww, half, CV_64FC1); Mat right = Mat::zeros(roww, half + 1, CV_64FC1); Mat phase = Mat::zeros(roww, coll, CV_64FC1); I.col(half - 1).copyTo(pp); Unwrap(pp, pi); pp.copyTo(I.col(half - 1)); for (int i = 0; i < half; i++) { I.col(half - i - 1).copyTo(left.col(i)); } for (int i = half - 1; i < coll; i++) { I.col(i).copyTo(right.col(i - half + 1)); } for (int j = 0; j < roww; j++) { left.row(j).copyTo(aa); right.row(j).copyTo(bb); Unwrap(aa, pi); Unwrap(bb, pi); aa.copyTo(left.row(j)); bb.copyTo(right.row(j)); } for (int i = 0; i < half - 1; i++) { left.col(half - i - 1).copyTo(leftt.col(i)); leftt.col(i).copyTo(phase.col(i)); } for (int i = half - 1; i < coll; i++) { right.col(i - half + 1).copyTo(phase.col(i)); } for(int i=0;i<roww;i++) { for(int j=0;j<coll;j++) { if(mask.at<double>(i,j)==0) { phase.at<double>(i,j)=0; } } } phase.copyTo(PhaseUnwrap); pp.release(); aa.release(); bb.release(); left.release(); leftt.release(); right.release(); phase.release(); }
import numpy as np
import cv2
class unwrap:
def reunwrap(self, I, mask, choose):
self.unwraprow = I.shape[0]
self.unwrapcol = I.shape[1]
if choose == 1:
self.RC(I, mask)
elif choose == 2:
self.Branch_cutting(I, mask)
def RC(self, I, mask):
roww, coll = I.shape
half = int(np.ceil(coll / 2))
pp = np.zeros((roww, 1), dtype=np.float64)
aa = np.zeros((1, coll), dtype=np.float64)
bb = np.zeros((1, coll), dtype=np.float64)
left = np.zeros((roww, half), dtype=np.float64)
leftt = np.zeros((roww, half), dtype=np.float64)
right = np.zeros((roww, half - 1), dtype=np.float64)
phase = np.zeros((roww, coll), dtype=np.float64)
pp[:,0] = I[:,half - 1]
pi = np.pi
self.Unwrap(pp, pi)
I[:,half - 1] = pp[:,0]
for i in range(half):
left[:,i] = I[:,half - i - 1]
for i in range(half - 1, coll):
right[:,i - half + 1] = I[:,i]
for j in range(roww):
aa[0,:] = left[j,:]
bb[0,:] = right[j,:]
self.Unwrap(aa, pi)
self.Unwrap(bb, pi)
left[j,:] = aa[0,:]
right[j,:] = bb[0,:]
for i in range(half - 1):
leftt[:,i] = left[:,half - i - 1]
phase[:,i] = leftt[:,i]
for i in range(half - 1, coll):
phase[:,i] = right[:,i - half + 1]
for i in range(roww):
for j in range(coll):
if mask[i,j] == 0:
phase[i,j] = 0
self.PhaseUnwrap = phase
pp = None
aa = None
bb = None
left = None
leftt = None
right = None
phase = None
def Unwrap(self, ph, pi):
for k in range(ph.shape[0]):
for i in range(1, ph.shape[1]):
diff = ph[k,i] - ph[k,i-1]
if diff > pi:
ph[k,i:] -= 2 * pi
elif diff < -pi:
ph[k,i:] += 2 * pi
I = np.random.rand(540, 720)
mask = np.random.rand(540, 720)
choose = 1
unwrapper = unwrap()
unwrapper.reunwrap(I, mask, choose)
pub fn plotly_spectra(path: &std::path::Path, tof_len: Option<i64>) -> Result<(), Box<dyn Error>> { let base_name = path.file_stem().unwrap().to_str().unwrap(); let spectrum_file = path.with_file_name(base_name.to_owned() + "_report_spectrum.html"); let mut plot = Plot::new(); let layout = Layout::new() .x_axis(Axis::new().title(Title::new("Time (ns)"))) .y_axis(Axis::new().title(Title::new("Pixels activated"))); plot.set_layout(layout); // this is backwards -> TODO: we should pass the data to this function let (time_axis, intensity_axis) = mass::spectrum(path, tof_len)?; let (time_axis, intensity_axis) = mass::zero_pad(&time_axis, &intensity_axis); let trace1 = Scatter::new(time_axis.clone(), intensity_axis.clone()) .name("Full spectrum") .mode(Mode::Lines); plot.add_trace(trace1); let full_csv_file = path.with_file_name(base_name.to_owned() + "_report_full_spectrum.csv"); let csv_strings: Vec<String> = time_axis.iter().zip(&intensity_axis).map(|(t, i)| format!("{},{}", t, i)).collect(); let mut file = std::fs::File::create(full_csv_file).unwrap(); // scope / file dropped at end of fn writeln!(file, "{}", csv_strings.join("\n")).unwrap(); plot.to_html(spectrum_file); Ok(()) }
这段代码定义了一个名为 `plotly_spectra` 的公共函数,用于绘制质谱图。
函数接受一个 `path` 参数,表示文件路径,以及一个可选的 `tof_len` 参数,表示 TOF(飞行时间)的长度。
首先,通过 `path.file_stem()` 获取文件名的 stem(不包含扩展名部分),然后通过 `to_str()` 转换为字符串类型,并使用 `unwrap()` 解包获取字符串值,并将其赋值给 `base_name` 变量。
接下来,通过将 `base_name` 和 "_report_spectrum.html" 拼接起来,创建一个新的文件路径 `spectrum_file`。
然后,创建一个 `Plot` 对象,并创建一个包含 x 轴和 y 轴标题的 `Layout` 对象,***
阅读全文