0

I'm using the OpenCV C APIs. Now I need to implement the following Matlab code in C using OpenCV.

Matlab Code:

function [cx, cy] = foe(Vx, Vy)

ofs = 10;

% get sub image (using offsets at border) --> STEP 1
subVx = Vx(ofs:end-ofs, ofs:end-ofs);
subVy = Vy(ofs:end-ofs, ofs:end-ofs);

% compute vertical and horizontal magnitudes --> STEP 2
subVx = subVx.^2;
subVy = subVy.^2;

% find index of minimum sums for vertical and horizontal magnitudes --> STEP 3
[v, cy] = min(sum(subVx'));
[v, cx] = min(sum(subVy));

% Calculate the Focus Of Expansion --> STEP 4
cy = cy + ofs;
cx = cx + ofs;

Step 1 is very easily done. I just set the ROI of the image.

Now for Step 2, I need to square each element of the IplImage's imageData element as follows:

for(i = 0; i < subvx->width * subvx->height; i++) {
    ?????
}

What should I write in place of ????? to square each element of imageData? imageData is char*, so max limit of each element would be 255. Squares of each element would most likely exceed this value.

How do I implement the above Matlab code in C in this case?

Also for Step 3, how do I create a transpose of imageData (considered as 2-dim matrix)?

ameyazing
  • 403
  • 12
  • 24
  • 1
    Since you are going to be computing the sum of subVx' and subVy, you could square the values during that operation (since the result of the sum will need to be an int anyway). Instead of doing a transpose of a 2-d matrix, just swap the indices when you access elements of the 'transposed' matrix. – Richante Sep 10 '12 at 10:52
  • Simple and Elegant. Thanks v v much @Richante – ameyazing Sep 11 '12 at 06:20

0 Answers0