def rgb2ycbcr(self): """main colorspace transformation from r,g,b to y,cb,cr input: return: y, cb, cr values as lists containing data for one image (y,cb,cr)""" if(args.verbose): print("Performing colorspace transformation from RGB to YCbCr") if self.img.mode != 'RGB' and self.img.mode != 'YCbCr' and self.img.mode != 'RGBA': raise Exception("The Picture has mode: " + self.img.mode + " but it must be RGB!") if args.numba: y, cb, cr = numba_rgb2ycbcr(self.trafo_table, self.rgb) else: ycbcr = self.trafo_table @ self.rgb y = ycbcr[0] cb = ycbcr[1] + 128 cr = ycbcr [2] + 128 y = np.rint(y).astype(np.uint8) cb = np.rint(cb).astype(np.uint8) cr = np.rint(cr).astype(np.uint8) if(args.debug): self.save_ycbcr_channels(y, cb, cr) if args.entropy: with open(os.path.join(inputpath, args.output, infilename + "_entropy.txt"), "w") as entropy_txt: entropy_txt.write("Entropy before jpeg encoding".center(48,"*") + "\n") print("- Entropy before jpeg encoding") Entropy.execute_entropy(self, self.r, self.g, self.b, rgb=True, pixels=True) Entropy.execute_entropy(self, y, cb, cr, rgb=False, pixels=True) return y, cb, cr
def numba_ycbcr2rgb(trafo_table, ycbcr): ycbcr[1] -= 128 ycbcr[2] -= 128 rgb = trafo_table @ ycbcr np.rint(rgb, rgb) rgb = rgb.astype(nb.int16) return rgb
def numba_rgb2ycbcr(trafo_table, rgb): rgb = rgb.astype(nb.float32) ycbcr = trafo_table @ rgb np.rint(ycbcr, ycbcr) y = ycbcr[0] cb = (ycbcr[1] + 128) cr = (ycbcr[2] + 128) return y, cb, cr
def numba_quantize(img8x8, quality_table, forward): if forward: for i in nb.prange(img8x8.shape[0]): img8x8[i] /= quality_table np.rint(img8x8, img8x8) else: for i in nb.prange(img8x8.shape[0]): img8x8[i] *= quality_table return img8x8
def __quantize(self, img8x8, quality_table, forward): """divides dct coefficients with quantization matrix""" quality_table = quality_table.astype(np.float64) if args.numba: img8x8 = numba_quantize(img8x8, quality_table, forward) else: if forward: img8x8 /= quality_table np.rint(img8x8, img8x8) else: img8x8 *= quality_table return img8x8
def execute_entropy(self, c1,c2,c3, rgb, pixels): """Takes list of lists of 64 elements as input. Computes the mean information H per symbol and writes it to entropy.txt. rgb = true for rgb image, false for ycbcr image pixels = true for pixelvalues, false for DCT coefficients""" if rgb == True: with open (os.path.join(inputpath, args.output, infilename + "_entropy.txt"), "a") as entropy_txt: for i, channel in enumerate([c1, c2, c3]): H = Entropy.__compute_entropy(self, channel) if i == 0: channel_string = "R " elif i == 1: channel_string = "G " elif i == 2: channel_string = "B " print("- Entropy in channel", str(channel_string) + ":", str(H), "bit per pixel") entropy_txt.write("Entropy in channel " + str(channel_string) + " = " + str(H) + " bit per pixel" + "\n") else: with open (os.path.join(inputpath, args.output, infilename + "_entropy.txt"), "a") as entropy_txt: for i, channel in enumerate([c1, c2, c3]): channel = np.rint(channel.flatten()).astype(np.int16) H = Entropy.__compute_entropy(self, channel) if i == 0: channel_string = "Y " elif i == 1: channel_string = "Cb" elif i == 2: channel_string = "Cr" if pixels == True: print("- Entropy in channel", str(channel_string) + ":", str(H), "bit per pixel") entropy_txt.write("Entropy in channel " + str(channel_string) + " = " + str(H) + " bit per pixel" + "\n") else: print("- Entropy in channel", str(channel_string) + ":", str(H), "bit per coefficient") entropy_txt.write("Entropy in channel " + str(channel_string) + " = " + str(H) + " bit per coefficient" + "\n")
def _apply_transform_cp(self): source_shape_y: cp.int32 = cp.int32(self._source_cp.shape[0]) source_shape_x: cp.int32 = cp.int32(self._source_cp.shape[1]) frame_shape_y: cp.int32 = cp.int32(self.frame_shape.y) frame_shape_x: cp.int32 = cp.int32(self.frame_shape.x) matrix = cp.asarray(self._matrix, dtype=cp.float32) vector = cp.asarray(self._vector, dtype=cp.float32) frame_to_source_fp32 = cp.matmul(self._frame_pixels_cp, matrix.T) + vector frame_to_source_int32 = cp.rint(frame_to_source_fp32).astype(cp.int32) # for 3D array: 0 is x, 1 is y source_x = frame_to_source_int32[:, :, 0] # 2D array of x pixel in source source_y = frame_to_source_int32[:, :, 1] # 2D array of y pixel in source # boolean 2-D array of portal pixels that map to a pixel on the source # will be false if the pixel on source would fall outside of source mapped = (source_y >= 0) & (source_y < source_shape_y) & \ (source_x >= 0) & (source_x < source_shape_x) frame_rgba = cp.zeros(shape=(frame_shape_y, frame_shape_x, 4), dtype=cp.uint8) # if cp.all(mapped): # frame_rgba[:, :] = self._source_cp[source_y, source_x, :] # else: # frame_rgba[mapped, :] = self._source_cp[source_y[mapped], source_x[mapped], :] frame_rgba[mapped, :] = self._source_cp[source_y[mapped], source_x[mapped], :] # self.image_rgba[~mapped, :] = self._zero_uint probably slower than just zeroing everything first self._frame_rgba = cp.asnumpy(frame_rgba)
def calc_rad(size, binning, return_float=False): '''Calculate radius and binned radius of 3D grid of voxels with given size''' cen = size // 2 ind = np.arange(size, dtype='f4') - cen x, y, z = np.meshgrid(ind, ind, ind, indexing='ij') rad = np.sqrt(x * x + y * y + z * z) if return_float: return rad return np.rint(rad / binning).astype('i4')
def get_slice(self, theta_n, phi_n, dist_n): theta = theta_n*math.pi phi = math.radians(phi_n*PHI_MAX) dist = dist_n*self.x0/2 # +/- 104 pixels # --- 1: Get bounding box dims --- h1, h2, z_min, z_max = self.get_bounding_box(theta=theta, phi=phi, dist=dist) w = self.y0 h = self.z0 # --- 2: Extract slice from volume --- # Get x_i and y_i for current layer x_offsets = cp.linspace(-h/2, h/2, h) * math.sin(phi) * math.cos(theta) y_offsets = cp.linspace(-h/2, h/2, h) * math.sin(phi) * math.sin(theta) # Tile and transpose x_offsets = cp.transpose(cp.tile(x_offsets,(w,1))) y_offsets = cp.transpose(cp.tile(y_offsets,(w,1))) x_i = cp.tile(cp.linspace(h1[0], h2[0], w),(h,1)) x_i = cp.asnumpy(cp.array(cp.rint(x_i + x_offsets),dtype='int')) y_i = cp.tile(cp.linspace(h1[1], h2[1], w),(h,1)) y_i = cp.asnumpy(cp.array(cp.rint(y_i + y_offsets),dtype='int')) # Don't forget to include the index offset from z! z_i = cp.transpose(cp.tile(cp.linspace(z_min, z_max, h),(w,1))) z_i = cp.asnumpy(cp.array(cp.rint(z_i),dtype='int')) # Flatten flat_inds = np.ravel_multi_index((x_i,y_i,z_i),(self.x0,self.y0,self.z0),mode='clip') # Fill in entire slice at once slice = cp.take(cp.array(self.data), cp.array(flat_inds)) # --- 3: Mask slice --- slice = cp.multiply(slice, cp.array(self.mask)) return cp.asnumpy(slice)
def __init__(self, imgs8x8, quality, infilename): self.quality = quality self.y8x8 = imgs8x8[0] self.cb8x8 = imgs8x8[1] self.cr8x8 = imgs8x8[2] # quantization table of luminances self.qbase_y = np.array([ [16,11,10,16,24,40,51,61], [12,12,14,19,26,58,60,55], [14,13,16,24,40,57,69,56], [14,17,22,29,51,87,80,62], [18,22,37,56,68,109,103,77], [24,35,55,64,81,104,113,92], [49,64,78,87,103,121,120,101], [72,92,95,98,112,100,103,99]]).astype(np.int8) # quantization table of chrominance self.qbase_c = np.array([ [17,18,24,47,99,99,99,99], [18,21,26,66,99,99,99,99], [24,26,56,99,99,99,99,99], [47,66,99,99,99,99,99,99], [99,99,99,99,99,99,99,99], [99,99,99,99,99,99,99,99], [99,99,99,99,99,99,99,99], [99,99,99,99,99,99,99,99]]).astype(np.int8) self.quality_qtable_y = self.__compute_qual_qtable(self.qbase_y) self.quality_qtable_c = self.__compute_qual_qtable(self.qbase_c) if(args.debug): self.__write_qtables_to_textfile(self.quality_qtable_y, self.quality_qtable_c) tmp = self.__check_qualityvalue(self.quality_qtable_y, self.quality_qtable_c) Q = np.rint(tmp) if(args.verbose): print("approximated computed check for quality value: " + str(Q)) print(" jfake - beginning backtransformation ".center(80,"*"))
def ycbcr2rgb(self, y=None, cb=None, cr=None): """colorspacetransformation from y,cb,cr to rgb input: y,cb,cr values saved as lists channels = true for rgb arrays, false for pil image return: rgb image as PIL.Image""" if(args.verbose): print("Performing colorspace transformation from YCbCr to RGB") ycbcr = np.array((y,cb,cr)) if args.numba: rgb = numba_ycbcr2rgb(self.backtrafo_table, ycbcr) else: ycbcr[1] -= 128 ycbcr[2] -= 128 rgb = self.backtrafo_table @ ycbcr rgb = np.rint(rgb).astype(np.int16) rgb = rgb.reshape(3, self.height_e, self.width_e) rgb = np.clip(rgb, 0, 255) # bring axes in correct order rgb = np.swapaxes(rgb, 0,2) rgb = np.swapaxes(rgb, 0,1) if args.cupy: rgb_img = Image.fromarray(np.asnumpy(rgb).astype(np.uint8),'RGB') else: rgb_img = Image.fromarray(rgb.astype(np.uint8),'RGB') crop_recomb = self.cropping_back(rgb_img) r = np.array(crop_recomb.getchannel('R')).flatten().astype(np.int16) g = np.array(crop_recomb.getchannel('G')).flatten().astype(np.int16) b = np.array(crop_recomb.getchannel('B')).flatten().astype(np.int16) rgb_out = np.concatenate((np.array([r]), np.array([g]), np.array([b]))) if(args.debug): crop_recomb.save(os.path.join(inputpath, args.output, "debug", infilename + "_rücktrafo.png")) return crop_recomb, rgb_out
def find_prob(measured_qubits, sub_state, states): # Make sure measured qubit numbers are in ascending order qubits = measured_qubits qubits.sort() # Make a copy of given states in order not to alter them a = states.copy() d1, d2 = a.shape # d1 = number of circuit runs, d2 = 2 ** N N = int(rint(log2(d2))) # Reshape to rank-(N+1) tensor a = a.reshape([d1] + [2] * N) # K = number of measured qubits, M = number of qubits not measured K = len(qubits) M = N - K # Reorder qubit number axes for i in range(K): a = swapaxes(a, i + 1, qubits[i] + 1) # Flatten arrays for 2 groups of qubits a = a.reshape([d1] + [2**K] + [2**M]) # Broadcast multiply coefficients a = swapaxes(a, 0, 1) a = multiply(a.T, sub_state).T # Sum over coefficients a = a.sum(axis=0) a = abs(a)**2 a = a.sum(axis=1) # Return probability of measuring a substate for all circuit runs return a
def map_coordinates(input, coordinates, output=None, order=None, mode='constant', cval=0.0, prefilter=True): """Map the input array to new coordinates by interpolation. The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order. The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found. Args: input (cupy.ndarray): The input array. coordinates (array_like): The coordinates at which ``input`` is evaluated. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. If it is not given, order 1 is used. It is different from :mod:`scipy.ndimage` and can change in the future. Currently it supports only order 0 and 1. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray: The result of transforming the input. The shape of the output is derived from that of ``coordinates`` by dropping the first axis. .. seealso:: :func:`scipy.ndimage.map_coordinates` """ _check_parameter('map_coordinates', order, mode) if mode == 'opencv' or mode == '_opencv_edge': input = cupy.pad(input, [(1, 1)] * input.ndim, 'constant', constant_values=cval) coordinates = cupy.add(coordinates, 1) mode = 'constant' ret = _get_output(output, input, coordinates.shape[1:]) if mode == 'nearest': for i in six.moves.range(input.ndim): coordinates[i] = coordinates[i].clip(0, input.shape[i] - 1) elif mode == 'mirror': for i in six.moves.range(input.ndim): length = input.shape[i] - 1 if length == 0: coordinates[i] = 0 else: coordinates[i] = cupy.remainder(coordinates[i], 2 * length) coordinates[i] = 2 * cupy.minimum(coordinates[i], length) - coordinates[i] if input.dtype.kind in 'iu': input = input.astype(cupy.float32) if order == 0: out = input[tuple(cupy.rint(coordinates).astype(cupy.int32))] else: coordinates_floor = cupy.floor(coordinates).astype(cupy.int32) coordinates_ceil = coordinates_floor + 1 sides = [] for i in six.moves.range(input.ndim): # TODO(mizuno): Use array_equal after it is implemented if cupy.all(coordinates[i] == coordinates_floor[i]): sides.append([0]) else: sides.append([0, 1]) out = cupy.zeros(coordinates.shape[1], dtype=input.dtype) if input.dtype in (cupy.float64, cupy.complex128): weight = cupy.empty(coordinates.shape[1], dtype=cupy.float64) else: weight = cupy.empty(coordinates.shape[1], dtype=cupy.float32) for side in itertools.product(*sides): weight.fill(1) ind = [] for i in six.moves.range(input.ndim): if side[i] == 0: ind.append(coordinates_floor[i]) weight *= coordinates_ceil[i] - coordinates[i] else: ind.append(coordinates_ceil[i]) weight *= coordinates[i] - coordinates_floor[i] out += input[ind] * weight del weight if mode == 'constant': mask = cupy.zeros(coordinates.shape[1], dtype=cupy.bool_) for i in six.moves.range(input.ndim): mask += coordinates[i] < 0 mask += coordinates[i] > input.shape[i] - 1 out[mask] = cval del mask if ret.dtype.kind in 'iu': out = cupy.rint(out) ret[:] = out return ret
def apply(gate, states, global_phase=False): # A shorthand for the original states a = states # d1 = number of circuit runs with noise, d2 = 2 ** N = dimension of state vector d1, d2 = states.shape N = int(rint(log2(d2))) # A copy of state a, to be flipped by qubit-wise Pauli operations b = copy(a) # print("d1 = ", d1) # print("d2 = ", d2) # print("N = ", N) # Reshape to rank-(N+1) tensor b = b.reshape([d1] + [2] * N) for k in range(len(gate[0])): basis = gate[0][k] q = gate[1][k] if basis == identity: pass if basis == x: b = roll(b, 1, q + 1) if basis == y: b = roll(b, 1, q + 1) b = swapaxes(b, 0, q + 1) b[0] *= -1j b[1] *= 1j b = swapaxes(b, 0, q + 1) if basis == s_phi: phi = array(gate[3][k]) b = roll(b, 1, q + 1) b = swapaxes(b, 0, q + 1) b = swapaxes(b, N, q + 1) phase1 = cos(phi) + 1j * sin(phi) phase2 = cos(phi) - 1j * sin(phi) b[0] = multiply(phase2, b[0]) b[1] = multiply(phase1, b[1]) b = swapaxes(b, N, q + 1) b = swapaxes(b, 0, q + 1) if basis == z: b = swapaxes(b, 0, q + 1) b[1] *= -1 b = swapaxes(b, 0, q + 1) b = b.reshape(d1, d2) angles = array(gate[2][0]) states = (cos(angles / 2) * a.T - 1j * sin(angles / 2) * b.T).T # Remove global phase (may be awkward if first amplitude is close to zero) if global_phase == False: pass return states
def ccg_slow(st1, st2, nbins, tbin): # this function efficiently computes the crosscorrelogram between two sets # of spikes (st1, st2), with tbin length each, timelags = plus/minus nbins # and then estimates how refractory the cross-correlogram is, which can be used # during merge decisions. st1 = cp.sort( st1) # makes sure spike trains are sorted in increasing order st2 = cp.sort(st2) dt = nbins * tbin N1 = max(1, len(st1)) N2 = max(1, len(st2)) T = cp.concatenate((st1, st2)).max() - cp.concatenate((st1, st2)).min() # we traverse both spike trains together, keeping track of the spikes in the first # spike train that are within dt of spikes in the second spike train ilow = 0 # lower bound index ihigh = 0 # higher bound index j = 0 # index of the considered spike K = cp.zeros(2 * nbins + 1) # (DEV_NOTES) the while loop below is far too slow as is while j <= N2 - 1: # traverse all spikes in the second spike train while (ihigh <= N1 - 1) and (st1[ihigh] < st2[j] + dt): ihigh += 1 # keep increasing higher bound until it's OUTSIDE of dt range while (ilow <= N1 - 1) and (st1[ilow] <= st2[j] - dt): ilow += 1 # keep increasing lower bound until it's INSIDE of dt range if ilow > N1 - 1: break # break if we exhausted the spikes from the first spike train if st1[ilow] > st2[j] + dt: # if the lower bound is actually outside of dt range, means we overshot (there were no # spikes in range) # simply move on to next spike from second spike train j += 1 continue for k in range(ilow, ihigh): # for all spikes within plus/minus dt range ibin = cp.rint( (st2[j] - st1[k]) / tbin).astype(int) # convert ISI to integer K[ibin + nbins] += 1 j += 1 irange1 = cp.concatenate( (cp.arange(1, nbins // 2), cp.arange(3 * nbins // 2, 2 * nbins))) irange2 = cp.arange(nbins - 50, nbins - 10) irange3 = cp.arange(nbins + 11, nbins + 50) # normalize the shoulders by what's expected from the mean firing rates # a non-refractive poisson process should yield 1 Q00 = cp.sum(K[irange1]) / (len(irange1) * tbin * N1 * N2 / T) # do the same for irange 2 Q01 = cp.sum(K[irange2]) / (len(irange2) * tbin * N1 * N2 / T) # compare to the other shoulder Q01 = max(Q01, cp.sum(K[irange3]) / (len(irange3) * tbin * N1 * N2 / T)) R00 = max(mean(K[irange2]), mean(K[irange3])) # take the biggest shoulder R00 = max(R00, mean(K[irange1])) # compare this to the asymptotic shoulder # test the probability that a central area in the autocorrelogram might be refractory # test increasingly larger areas of the central CCG a = K[nbins] K[nbins] = 0 Qi = cp.zeros(10) Ri = cp.zeros(10) for i in range(1, 11): irange = cp.arange(nbins - i, nbins + i + 1) # for this central range of the CCG # compute the normalised ratio as above. this should be 1 if there is no refractoriness Qi0 = cp.sum(K[irange]) / (2 * i * tbin * N1 * N2 / T) Qi[i - 1] = Qi0 # save the normalised probability n = cp.sum(K[irange]) / 2 lam = R00 * i # log(p) = log(lam) * n - lam - gammaln(n+1) # this is tricky: we approximate the Poisson likelihood with a gaussian of equal mean and # variance that allows us to integrate the probability that we would see <N spikes in the # center of the cross-correlogram from a distribution with mean R00*i spikes p = 1 / 2 * (1 + erf((n - lam) / cp.sqrt(2 * lam))) Ri[i - 1] = p # keep track of p for each bin size i K[nbins] = a # restore the center value of the cross-correlogram return K, Qi, Q00, Q01, Ri
def _convert(image, dtype, force_copy=False, uniform=False): """ Convert an image to the requested data-type. Warnings are issued in case of precision loss, or when negative values are clipped during conversion to unsigned integer types (sign loss). Floating point values are expected to be normalized and will be clipped to the range [0.0, 1.0] or [-1.0, 1.0] when converting to unsigned or signed integers respectively. Numbers are not shifted to the negative side when converting from unsigned to signed integer types. Negative values will be clipped when converting to unsigned integers. Parameters ---------- image : ndarray Input image. dtype : dtype Target data-type. force_copy : bool, optional Force a copy of the data, irrespective of its current dtype. uniform : bool, optional Uniformly quantize the floating point range to the integer range. By default (uniform=False) floating point values are scaled and rounded to the nearest integers, which minimizes back and forth conversion errors. .. versionchanged :: 0.15 ``_convert`` no longer warns about possible precision or sign information loss. See discussions on these warnings at: https://github.com/scikit-image/scikit-image/issues/2602 https://github.com/scikit-image/scikit-image/issues/543#issuecomment-208202228 https://github.com/scikit-image/scikit-image/pull/3575 References ---------- .. [1] DirectX data conversion rules. https://msdn.microsoft.com/en-us/library/windows/desktop/dd607323%28v=vs.85%29.aspx .. [2] Data Conversions. In "OpenGL ES 2.0 Specification v2.0.25", pp 7-8. Khronos Group, 2010. .. [3] Proper treatment of pixels as integers. A.W. Paeth. In "Graphics Gems I", pp 249-256. Morgan Kaufmann, 1990. .. [4] Dirty Pixels. J. Blinn. In "Jim Blinn's corner: Dirty Pixels", pp 47-57. Morgan Kaufmann, 1998. """ dtypeobj_in = image.dtype if dtype is cp.floating: dtypeobj_out = cp.dtype("float64") else: dtypeobj_out = cp.dtype(dtype) dtype_in = dtypeobj_in.type dtype_out = dtypeobj_out.type kind_in = dtypeobj_in.kind kind_out = dtypeobj_out.kind itemsize_in = dtypeobj_in.itemsize itemsize_out = dtypeobj_out.itemsize # Below, we do an `issubdtype` check. Its purpose is to find out # whether we can get away without doing any image conversion. This happens # when: # # - the output and input dtypes are the same or # - when the output is specified as a type, and the input dtype # is a subclass of that type (e.g. `cp.floating` will allow # `float32` and `float64` arrays through) if cp.issubdtype(dtype_in, cp.obj2sctype(dtype)): if force_copy: image = image.copy() return image if not (dtype_in in _supported_types and dtype_out in _supported_types): raise ValueError("Can not convert from {} to {}.".format( dtypeobj_in, dtypeobj_out)) if kind_in in 'ui': imin_in = cp.iinfo(dtype_in).min imax_in = cp.iinfo(dtype_in).max if kind_out in 'ui': imin_out = cp.iinfo(dtype_out).min imax_out = cp.iinfo(dtype_out).max # any -> binary if kind_out == 'b': return image > dtype_in(dtype_range[dtype_in][1] / 2) # binary -> any if kind_in == 'b': result = image.astype(dtype_out) if kind_out != 'f': result *= dtype_out(dtype_range[dtype_out][1]) return result # float -> any if kind_in == 'f': if kind_out == 'f': # float -> float return image.astype(dtype_out) if cp.min(image) < -1.0 or cp.max(image) > 1.0: raise ValueError("Images of type float must be between -1 and 1.") # floating point -> integer # use float type that can represent output integer type computation_type = _dtype_itemsize(itemsize_out, dtype_in, cp.float32, cp.float64) if not uniform: if kind_out == 'u': image_out = cp.multiply(image, imax_out, dtype=computation_type) else: image_out = cp.multiply(image, (imax_out - imin_out) / 2, dtype=computation_type) image_out -= 1.0 / 2.0 cp.rint(image_out, out=image_out) cp.clip(image_out, imin_out, imax_out, out=image_out) elif kind_out == 'u': image_out = cp.multiply(image, imax_out + 1, dtype=computation_type) cp.clip(image_out, 0, imax_out, out=image_out) else: image_out = cp.multiply(image, (imax_out - imin_out + 1.0) / 2.0, dtype=computation_type) cp.floor(image_out, out=image_out) cp.clip(image_out, imin_out, imax_out, out=image_out) return image_out.astype(dtype_out) # signed/unsigned int -> float if kind_out == 'f': # use float type that can exactly represent input integers computation_type = _dtype_itemsize(itemsize_in, dtype_out, cp.float32, cp.float64) if kind_in == 'u': # using cp.divide or cp.multiply doesn't copy the data # until the computation time image = cp.multiply(image, 1. / imax_in, dtype=computation_type) # DirectX uses this conversion also for signed ints # if imin_in: # cp.maximum(image, -1.0, out=image) else: image = cp.add(image, 0.5, dtype=computation_type) image *= 2 / (imax_in - imin_in) return image.astype(dtype_out, copy=False) # unsigned int -> signed/unsigned int if kind_in == 'u': if kind_out == 'i': # unsigned int -> signed int image = _scale(image, 8 * itemsize_in, 8 * itemsize_out - 1) return image.view(dtype_out) else: # unsigned int -> unsigned int return _scale(image, 8 * itemsize_in, 8 * itemsize_out) # signed int -> unsigned int if kind_out == 'u': image = _scale(image, 8 * itemsize_in - 1, 8 * itemsize_out) result = cp.empty(image.shape, dtype_out) cp.maximum(image, 0, out=result, dtype=image.dtype, casting='unsafe') return result # signed int -> signed int if itemsize_in > itemsize_out: return _scale(image, 8 * itemsize_in - 1, 8 * itemsize_out - 1) image = image.astype(_dtype_bits('i', itemsize_out * 8)) image -= imin_in image = _scale(image, 8 * itemsize_in, 8 * itemsize_out, copy=False) image += imin_out return image.astype(dtype_out)
#% One view rendering (test) viewdirection = 0 viewpoint.lon = cp.deg2rad(0) viewpoint.lat = cp.deg2rad(0) viewpoint.pos_x = 0 viewpoint.pos_y = 0 if (viewdirection == 0): viewpoint.lon = viewpoint.lon elif (viewdirection == 1): viewpoint.lon = viewpoint.lon + 180 OutViewF, OutFlowF = RenderingUserViewLF_AllinOne5K(LF[0], LFDisp_forward, 1, viewpoint, 1) OutFlowF = cp.rint((OutFlowF * 10000)) OutFlowF = OutFlowF / 10000 OutViewB, OutFlowB = RenderingUserViewLF_AllinOne5K(LF[1], LFDisp_backward, 2, viewpoint, 1) OutFlowB = cp.rint((OutFlowB * 10000)) OutFlowB = OutFlowB / 10000 OutViewF = cp.array(OutViewF, dtype=cp.int8) OutViewB = cp.array(OutViewB, dtype=cp.int8) OutViewF = cp.asnumpy(OutViewF) OutViewB = cp.asnumpy(OutViewB) OutFlowF = cp.asnumpy(OutFlowF) OutFlowB = cp.asnumpy(OutFlowB)
def inter8_mat5K(LF=None, P_r=None, P_1=None, P_2=None, U_r=None, U_1=None, U_2=None, H_r=None, H_1=None, H_2=None, c=None): print("inter8_mat5K...\n") height = Params.HEIGHT width = Params.WIDTH P_1[P_r == 1] = 0 P_2[P_r == 0] = 0 #print P_2 U_1[U_r == 1] = 0 U_2[U_r == 0] = 0 #print U_2 H_1[H_r == 1] = 0 H_2[H_r == 0] = 0 #print H_2 #arrays used as indices must be of integer (or boolean) type, unt64 for long data P_1 = cp.array(P_1, dtype=cp.int64) P_2 = cp.array(P_2, dtype=cp.int64) U_1 = cp.array(U_1, dtype=cp.int64) U_2 = cp.array(U_2, dtype=cp.int64) H_1 = cp.array(H_1, dtype=cp.int64) H_2 = cp.array(H_2, dtype=cp.int64) if (c == 1): IMAGE_MAT = ((1.0 - P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 1 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 1 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 1 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 1 -1]))))) + \ ((P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 1 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 1 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 1 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 1 -1]))))) elif (c == 2): IMAGE_MAT = ((1.0 - P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 2 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 2 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 2 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 2 -1]))))) + \ ((P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 2 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 2 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 2 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 2 -1]))))) elif (c == 3): IMAGE_MAT = ((1.0 - P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 3 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 3 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 3 -1]) + \ H_r * im2double(LF[(P_1) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 3 -1]))))) + \ ((P_r) * \ ((1.0 - U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_1 * 3 + 3 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_1 * (height * 3) + H_2 * 3 + 3 -1])) + \ ((U_r) * ((1.0 - H_r) * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_1 * 3 + 3 -1]) + \ H_r * im2double(LF[(P_2) * (height * width * 3) + U_2 * (height * 3) + H_2 * 3 + 3 -1]))))) print() IMAGE_MAT = cp.rint((IMAGE_MAT * 10000)) IMAGE_MAT = IMAGE_MAT / 10000 IMAGE_MAT = IMAGE_MAT * 255 IMAGE_MAT = cp.rint((IMAGE_MAT)) return IMAGE_MAT
def RenderingUserViewLF_AllinOne5K(LF=None, LFDisparity=None, FB=None, viewpoint=None, DIR=None): sphereW = Params.WIDTH sphereH = Params.HEIGHT CENTERx = viewpoint.lon CENTERy = viewpoint.lat # output view is 3:4 ratio new_imgW = cp.floor(viewpoint.diag * 4 / 5 + 0.5) new_imgH = cp.floor(viewpoint.diag * 3 / 5 + 0.5) new_imgW = int(new_imgW) new_imgH = int(new_imgH) OutView = cp.zeros((new_imgH, new_imgW, 3)) TYwarp, TXwarp = cp.mgrid[0:new_imgH, 0:new_imgW] TX = TXwarp TY = TYwarp TX = (TX - 0.5 - new_imgW / 2) TY = (TY - 0.5 - new_imgH / 2) #의심 TX = TX + 1 TY = TY + 1 r = (viewpoint.diag / 2) / cp.tan(viewpoint.fov / 2) R = cp.sqrt(TY**2 + r**2) # Calculate LF_n ANGy = cp.arctan(-TY / r) ANGy = ANGy + CENTERy if (FB == 1): ANGn = cp.cos(ANGy) * cp.arctan(TX / r) ANGn = ANGn + CENTERx Pn = (Params.LFU_W / 2 - viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) elif (FB == 2): ANGn = cp.cos(ANGy) * cp.arctan(-TX / r) ANGn = ANGn - CENTERx Pn = (Params.LFU_W / 2 + viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) X = cp.sin(ANGy) * R Y = -cp.cos(ANGy) * R Z = TX ANGx = cp.arctan2(Z, -Y) RZY = cp.sqrt(Z**2 + Y**2) ANGy = cp.arctan(X / RZY) #or ANGy = atan2(X, RZY); RATIO = 1 ANGy = ANGy * RATIO ANGx = ANGx + CENTERx ANGx[abs(ANGy) > pi / 2] = ANGx[abs(ANGy) > pi / 2] + pi ANGx[ANGx > pi] = ANGx[ANGx > pi] - 2 * pi ANGy[ANGy > pi / 2] = pi / 2 - (ANGy[ANGy > pi / 2] - pi / 2) ANGy[ANGy < -pi / 2] = -pi / 2 + (ANGy[ANGy < -pi / 2] + pi / 2) Px = (ANGx + pi) / (2 * pi) * sphereW + 0.5 Py = ((-ANGy) + pi / 2) / pi * sphereH + 0.5 if (DIR == 2): Px = Px + Params.WIDTH / 4 elif (DIR == 3): Px = Px + Params.WIDTH / 2 elif (DIR == 4): Px = Px - Params.WIDTH / 4 Px[Px < 1] = Px[Px < 1] + Params.WIDTH Px[Px > Params.WIDTH] = Px[Px > Params.WIDTH] - Params.WIDTH INDxx = cp.argwhere(Px < 1) Px[INDxx] = Px[INDxx] + sphereW Pn0 = cp.floor(Pn) Pn1 = cp.ceil(Pn) Pnr = Pn - Pn0 Px0 = cp.floor(Px) Px1 = cp.ceil(Px) Pxr = Px - Px0 Py0 = cp.floor(Py) Py1 = cp.ceil(Py) Pyr = Py - Py0 Pnr = cp.rint((Pnr * 10000)) Pnr = Pnr / 10000 Pxr = cp.rint((Pxr * 10000)) Pxr = Pxr / 10000 Pyr = cp.rint((Pyr * 10000)) Pyr = Pyr / 10000 #210->012 rgb #cv2 사용 안하면 그대로 OutView[:, :, 2] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 1) OutView[:, :, 1] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 2) OutView[:, :, 0] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 3) OutFlow = inter8_mat_flow5K(LFDisparity, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1) Py = cp.pad(Py, [(1, 1), (0, 0)], mode='edge') Py = cp.ceil((Py * 10000)) Py = Py / 10000 My = 2 / (Py[2:cp.size(Py, 0), :] - Py[0:(cp.size(Py, 0) - 2), :]) My[0, :] = My[0, :] / 2 My[cp.size(My, 0) - 1, :] = My[cp.size(My, 0) - 1, :] / 2 OutFlow = My * OutFlow return OutView, OutFlow