简体   繁体   English

如何在Python中将2通道的光流灰度JPG图像合并为一个RGB图像?

[英]How to merge 2 channel of optical flow grayscale JPG images into one RGB image in Python?

I'm making a RGB image from the given 2 grayscale jpg images, which is for x, y channel in optical flow respectively. 我正在从给定的2个灰度jpg图像制作RGB图像,分别用于光流中的x,y通道。

sample input images and my current output 样本输入图像和我当前的输出

def optical_flow(one, two, w, h):
    """
    method taken from (https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4)
    """
    one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY)
    two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY)
    hsv = np.zeros((w, h, 3))
    # set saturation
    hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1]
    # obtain dense optical flow paramters
    flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None,
                                        pyr_scale=0.5, levels=1, winsize=15,
                                        iterations=2,
                                        poly_n=5, poly_sigma=1.1, flags=0)
    # convert from cartesian to polar
    mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
    # hue corresponds to direction
    hsv[:,:,0] = ang * (180/ np.pi / 2)
    # value corresponds to magnitude
    hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    # convert HSV to int32's
    hsv = np.asarray(hsv, dtype= np.float32)
    rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
    return rgb_flow 

imgu = cv2.imread('u.jpg')
imgv = cv2.imread('v.jpg')
img = optical_flow(one, two, w, h)

Image.fromarray(imgu, 'RGB').show() // top-left one in img
Image.fromarray(imgv, 'RGB').show() // top-right one in img
Image.fromarray(img, 'RGB').show()  // bottom-left one in img

I think the output image looks strange. 我认为输出图像看起来很奇怪。

Optical flow computes a motion vector field of two consequtive frames. 光流计算两个结果帧的运动矢量场。 In you case one and two . 在你的情况下, one two However, your input images u and v does not show consequtive frames. 但是,您的输入图像u和v没有显示相应的帧。 The problem here is that there is no relation between the images, ie no similar content. 这里的问题是图像之间没有关系,即没有相似的内容。 Cosequently your optical flow field flow will have some random like values. 因此,您的光流场flow将具有一些类似随机的值。

When computing the color coded visualisation of your optical flow field. 计算光流场的颜色编码可视化时。 In your case the saturation channel is initialized not correctly: 您的情况下,饱和通道初始化不正确:

# set saturation
hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1]

Set it to 255. More common solutation is to set the value channel to 255 and use the saturation channel to encode the magnitude. 将其设置为255。更常见的解决方法是将值通道设置为255,并使用饱和度通道对幅度进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM