2

I am using Matlab to analyse video optical flow and what I want to do is weighing the optical flow between video frames.

My problem is I don't know how to modify the read only opticalFlow object property, specially, magnitude property.

Here is my test code (this simple code is on the MathWork help documentation http://uk.mathworks.com/help/vision/ref/opticalflow-class.html):

opflow = opticalFlow(randn(5,5),randn(5,5))

check the output, we have:

opticalFlow with properties:

         Vx: [5x5 double]
         Vy: [5x5 double]
Orientation: [5x5 double]
  Magnitude: [5x5 double]

Then we check the opflow.Magnitude property, we have:

>>opflow.Magnitude

ans =

1.1661    1.5809    1.9726    0.2291    0.6722
1.9727    1.2263    3.0523    0.2715    1.2187
2.2791    1.1224    1.0470    1.5235    0.9531
0.9109    3.6688    1.3717    1.4433    1.9629
0.8494    4.0421    1.8548    1.6603    1.2122

When I try to modify the opticalFlow object(opflow here), the Matlab report an error:

>> opflow.Magnitude(1,1)=0
You cannot set the read-only property 'Magnitude' of opticalFlow.

I then checked setter methods and googled some other documents but still cannot find a solution. I know I could copy them out to another matrix and then modify that new matrix, but it will waste memory while calculating optical flow for a long video sequence. Is there any way I could modify this read only property?

1 Answer 1

2

There's a reason Magnitude is a read-only property. If you look at the source code, you will notice that it is a Dependent property. Corresponding getter method calculates it on the fly from Vx and Vy. So it simply makes no sense for you to modify this property directly as otherwise the object would become inconsistent.

function out = get.Magnitude(this)
    out = computeMagnitude(this.pVx, this.pVy);
end

function mag = computeMagnitude(Vx, Vy)
    mag = sqrt(Vx.*Vx + Vy.*Vy);
end

Moreover, all these properties, including Vx and Vy are declared with SetAccess='private' attribute, so even though Vx and Vy have setter methods, they are not public. Basically you cannot change these properties.

The only way for you to change the property would be to create a new object:

Vx = opflow.Vx;
Vx(1,1) = 0;
opflow2 = opticalFlow(Vx, opflow.Vy);

So you will end up using more memory, but only temporarily, until your local matrix copies go out of scope and are cleared by the garbage collector.

Alternatively you can just create your own class (copy-paste) and modify property attributes to make them public.

3
  • Thanks for this explanation, but it still failed when I try to modify Vx or Vy directly... 'opflow.Vx(1,1)=0' is still not working....
    – drinking
    Commented Jul 11, 2016 at 21:14
  • Turns out that you cannot set it at all, not to mention amending a single element as in your comment. Please see updated answer. Commented Jul 11, 2016 at 21:27
  • Thank you so much for this detailed explanation!
    – drinking
    Commented Jul 11, 2016 at 21:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.