解释如下代码double half = (d>=0)?0.5:-0.5; int i = (int)(d+half); System.out.println(i);
时间: 2023-07-15 09:10:41 浏览: 156
这段代码主要是将一个 double 类型的变量 d 转换成 int 类型的变量 i。这里的 half 变量是用来进行四舍五入的,当 d 大于等于 0 时,half 的值为 0.5,否则为 -0.5。通过将 d 加上 half 再转换为 int 类型,就可以将 d 四舍五入得到最接近的整数值,并赋值给 i。最后将 i 输出到控制台。
相关问题
把这段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)
解释一下这段报错 2023-05-09 08:40:46.405891: E external/org_tensorflow/tensorflow/core/framework/node_def_util.cc:675] NodeDef mentions attribute input_para_type_list which is not in the op definition: Op<name=Sum; signature=input:T, reduction_indices:Tidx -> output:T; attr=keep_dims:bool,default=false; attr=T:type,allowed=[DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, 6034766930529145842, DT_UINT16, DT_COMPLEX128, DT_HALF, DT_UINT32, DT_UINT64]; attr=Tidx:type,default=DT_INT32,allowed=[DT_INT32, DT_INT64]> This may be expected if your graph generating binary is newer than this binary. Unknown attributes will be ignored. NodeDef: {{node PartitionedCall_/ReduceSum_ReduceSum_670}}
这个错误信息是TensorFlow给出的一个提示,意思是在节点定义中使用了不存在于操作定义中的属性。具体来说,这个错误信息给出了一个节点的名称 `PartitionedCall_/ReduceSum_ReduceSum_670`,并指出这个节点定义中使用了一个名为 `input_para_type_list` 的属性,但是这个属性并不存在于 `Sum` 操作的定义中。
这个错误信息也提到了一个可能的原因,即生成图的二进制文件版本比当前TensorFlow二进制文件版本更新,导致节点定义中使用了新版本中新增的属性,而当前TensorFlow版本中并不支持这些属性。不过这只是一种猜测,具体原因还需要根据实际情况来分析。
阅读全文