把这段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(); }
时间: 2023-06-04 21:04:14 浏览: 105
unwrap_phase-master.zip_phase unwrapping_unwrap phase_相位解_相位解缠_解
5星 · 资源好评率100%
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)
阅读全文