在开发一些三线性插值代码时,我收到了opencv错误1820: error: (-215:Assertion failed)
我试图弄清楚这是什么意思。
我目前还想知道我的编码方法对Trilinear是否正确。
此外,有没有一种方法可以使流算法以彩色显示 而不是灰度?我尝试过彩色,但是收到了类似的错误。
三个框架可在此处下载
代码:
import numpy as np
import cv2
import scipy as sp
Image1_Fp = ".\\3325_71.png" to
Image2_Fp = ".\\3325_72.png" to
Image3_Fp = ".\\3325_73.png" to
Write_Image_Location_Tri_Sampled = ".\\3325_syn_Tri.png"
def imread(filename):
im = cv2.imread(filename)
return im
if __name__ == '__main__':
im1 = imread(Image1_Fp)
im2 = imread(Image2_Fp)
im3 = imread(Image3_Fp)
im3gray = cv2.cvtColor(im3, cv2.COLOR_BGR2GRAY)
im1gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
flow = cv2.calcOpticalFlowFarneback(im1gray, im3gray, flow=1, pyr_scale=0.5, levels=2, winsize=10, iterations=5, poly_n=7, poly_sigma=1.2, flags=0)
# Trilinear Interpolation using meshgrid and remap
half_flow = 0.5 * flow # reduce the flow lines all by half.
h, w = flow.shape[:2]
grid_x, grid_y = np.meshgrid(h, w) # Identifies the coordinates of the grid with x, and y value,
coor_x_1 = grid_x + half_flow[:, :, 0] # So the flow is a displacement, and you need to add the pixel location to it to get where that displacement came from on the grid
coor_y_1 = grid_y + half_flow[:, :, 1]
# Finds the interpolated intensity from the pixels closest to the flow line from RGB Image 1
output_1 = cv2.remap(im1, coor_x_1, coor_y_1, cv2.INTER_CUBIC, borderMode =cv2.BORDER_REPLICATE)
coor_x_2 = grid_x - half_flow[:, :, 0]
coor_y_2 = grid_y - half_flow[:, :, 1]
# Finds the interpolated intensity from the pixels closest to the flow line from RGB Image 2
output_2 = cv2.remap(im2, coor_x_2, coor_y_2, cv2.INTER_CUBIC, borderMode =cv2.BORDER_REPLICATE)
# combined half of each
Combined_Output = (output_1 / 2) + (output_2 / 2) # Applies Trilinear part for a synthesized batch
cv2.imwrite(Write_Image_Location_Tri_Sampled, Combined_Output)
我希望看到的合成图像看起来与两个图像之间的图像相似,但是我收到了此错误。
Exception has occurred: error
OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:1820: error: (-215:Assertion failed) ((map1.type() == CV_32FC2 || map1.type() == CV_16SC2) && map2.empty()) || (map1.type() == CV_32FC1 && map2.type() == CV_32FC1) in function 'cv::remap'
File "..\OpenCVOpticalFLowAndTrilinearInterp.py", line 90, in <module>
output_1 = cv2.remap(im1, coor_x_1, coor_y_1, cv2.INTER_CUBIC, borderMode =cv2.BORDER_REPLICATE)