如何在图像上应用逆光流向量?

时间:2021-03-19 20:46:24

I have a small project on Moving object detection in moving camera in which i have to use negative optical flow vector to minimize ego motion compensation. I have a video and some particular consecutive frames in which average of negative optical flow vector has to be computed. I have already calculated Optical flow between say, (k-1)th and kth frame. Also, I have calculated average of optical flow vector V=[u,v], where v is the average of horizontal optical flow and u is the average of vertical flow. Now, I have to apply inverse of optical flow vector i.e., -V to the (k-1)th frame. I'm new to matlab and i don't know much about it. Please help

我有一个关于在移动相机中移动物体检测的小项目,其中我必须使用负光流向量来最小化自我运动补偿。我有一个视频和一些特定的连续帧,其中必须计算负光流向量的平均值。我已经计算了说,(k-1)和第k帧之间的光流。此外,我计算了光流向量V = [u,v]的平均值,其中v是水平光流的平均值,u是垂直流的平均值。现在,我必须将光流向量的逆,即-V应用于第(k-1)帧。我是matlab的新手,我不太了解它。请帮忙

I have tried this code segment to do so but the results aren't as expected

我已经尝试过这段代码,但结果并不像预期的那样

function I1=reverseOF(I,V) R=I(:,:,1); G=I(:,:,2); B=I(:,:,3); [m,n]=size(rgb2gray(I)); for i=1:m for j=1:n v1=[j i]; v2=-V; v3=v1.*v2; R(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=R(i,j); G(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=G(i,j); B(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=B(i,j); I1(floor(1+abs(v3(1,2))),floor(1+abs(v3(1,2))))=I(i,j); end end I1=cat(3,R,G,B); enter code here

I have used abs() function because otherwise some error was occuring like "attempted to access negative location; index must be a positive or logical".

我使用了abs()函数,因为否则会出现一些错误,例如“试图访问负位置;索引必须是正数或逻辑”。

Image A and Image B are the images that i have used to estimate the optical flow.enter image description here

图像A和图像B是我用来估计光流的图像。这里的图像描述

This is the result that i am obtaining after applying the above function. enter image description here

这是我在应用上述功能后获得的结果。在此处输入图像描述

1 个解决方案

#1


2  

Unfortunately, you cant do this easily. This is a quite advanced research problem, because obtaining the inverse of a vector field on a mesh grid is not an easy problem, actually its quite hard.

不幸的是,你不能轻易做到这一点。这是一个非常先进的研究问题,因为在网格上获得矢量场的倒数并不是一个容易的问题,实际上它很难。

Notice that your vector field (optical flow) start in a mesh grid, but it doesnt end in a mesh grid, it ends in random subpixel positions. If you just invert this field, doing -V is not enough! The result wont be the inverse!

请注意,您的矢量场(光流)在网格中开始,但它不以网格结束,它以随机子像素位置结束。如果你只是反转这个字段,做-V是不够的!结果不会相反!

This is a open research problem, look for example at this 2010 paper that addresses exactly this issue, and proposes a method to create "pseudoinverses".

这是一个开放的研究问题,例如在2010年的论文中,正好解决了这个问题,并提出了一种创建“伪逆”的方法。

Suppose you have that inverse, because you computed it somehow. Your code is quite bad for it, and the solutions (abs!) are showing (no offense) that you are not really understanding what you are doing. For a known vector field {Vx,Vy}, size equals to the image size (if its not, you can figure out easily how to interpolate it unsig interp2 ) the code would look something like:

假设你有逆,因为你以某种方式计算它。你的代码非常糟糕,并且解决方案(绝对!)显示(没有冒犯)你并不真正理解你在做什么。对于已知的矢量场{Vx,Vy},大小等于图像大小(如果不是,你可以很容易地弄清楚如何插入它unsig interp2)代码看起来像:

newimg=zeros(size(I));

[ix,iy]=meshgrid(1:size(I,1),1:size(I,2));

newimg(:,:,1)=interp2(I(:,:,1),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,2)=interp2(I(:,:,3),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,3)=interp2(I(:,:,2),ix+Vx,iy+Vy); % this is your whole loop.

#1


2  

Unfortunately, you cant do this easily. This is a quite advanced research problem, because obtaining the inverse of a vector field on a mesh grid is not an easy problem, actually its quite hard.

不幸的是,你不能轻易做到这一点。这是一个非常先进的研究问题,因为在网格上获得矢量场的倒数并不是一个容易的问题,实际上它很难。

Notice that your vector field (optical flow) start in a mesh grid, but it doesnt end in a mesh grid, it ends in random subpixel positions. If you just invert this field, doing -V is not enough! The result wont be the inverse!

请注意,您的矢量场(光流)在网格中开始,但它不以网格结束,它以随机子像素位置结束。如果你只是反转这个字段,做-V是不够的!结果不会相反!

This is a open research problem, look for example at this 2010 paper that addresses exactly this issue, and proposes a method to create "pseudoinverses".

这是一个开放的研究问题,例如在2010年的论文中,正好解决了这个问题,并提出了一种创建“伪逆”的方法。

Suppose you have that inverse, because you computed it somehow. Your code is quite bad for it, and the solutions (abs!) are showing (no offense) that you are not really understanding what you are doing. For a known vector field {Vx,Vy}, size equals to the image size (if its not, you can figure out easily how to interpolate it unsig interp2 ) the code would look something like:

假设你有逆,因为你以某种方式计算它。你的代码非常糟糕,并且解决方案(绝对!)显示(没有冒犯)你并不真正理解你在做什么。对于已知的矢量场{Vx,Vy},大小等于图像大小(如果不是,你可以很容易地弄清楚如何插入它unsig interp2)代码看起来像:

newimg=zeros(size(I));

[ix,iy]=meshgrid(1:size(I,1),1:size(I,2));

newimg(:,:,1)=interp2(I(:,:,1),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,2)=interp2(I(:,:,3),ix+Vx,iy+Vy); % this is your whole loop.
newimg(:,:,3)=interp2(I(:,:,2),ix+Vx,iy+Vy); % this is your whole loop.