Jefferson Chiu
Jefferson Chiu

Reputation: 121

Porting codes from MATLAB to C(OpenCV)

I've been working on trying to port a small meanshift tracking program, which is sort of different from the meanshift that involves performing back-projection, from MATLAB to C, but I'm having some problems with it.

I don't know how to interpret some of the lines from MATLAB to C(OpenCV).

https://drive.google.com/drive/folders/0B2r9FmkcbNwAbHdtVEVkSW1SQW8

I put the 2 .m files, 1 .cpp file and a directory where pictures are placed for both programs to use on my googledrive.

"demo_MultiMeanShift_1st_ver_0413.m" is what I'd like to port to C,

"boxfilter.m" is the function that's found in the following website: http://blog.csdn.net/wds555/article/details/23176313 (It's a Chinese website however)

and "Meanshift_demo.cpp" is what I've done so far.

There're mainly two parts that I don't know how to interpret from MATLAB to C: 1st part:

bas = zeros(hei,wid,N) ;
for i = 1 : 1 : N
    bas(:,:,i) = boxfilter( ones(hei,wid), floor(r/i) ) ;
end


Ic_mean = zeros(hei,wid,dep,N) ;


for i = 1 : 1 : N
    for d = 1 : 1 : dep
        %Average pixel value(s) of the object being tracked
        Ic_mean(pc(2)-(r+sw) : pc(2)+(r+sw), pc(1)-(r+sw) : pc(1)+(r+sw), d, i) = boxfilter(Ip(pc(2)-(r+sw) : pc(2)+(r+sw), pc(1)-(r+sw) : pc(1)+(r+sw),d), floor(r/i)) ./ bas(pc(2)-(r+sw) : pc(2)+(r+sw), pc(1)-(r+sw) : pc(1)+(r+sw),i);
        %Ic_mean(:,:,d,i) = boxfilter(Ip(:,:,d), floor(r/i)) ./ bas(:,:,i);        
    end
end

dis = zeros(1,N) ;

2nd part:

for i = -sw + pc(2) : 2 : sw + pc(2)
    for j = -sw + pc(1) : 2 : sw + pc(1)
        for d = 1 : 1 : N
            dis(d) = sqrt( ... % (p1(R1, G1, B1) - p2(R2, G2, B2))^2
                  ( Ic_mean(i,j,1,d) - Ip_mean(pc(2),pc(1),1,d) )^2 ...
                + ( Ic_mean(i,j,2,d) - Ip_mean(pc(2),pc(1),2,d) )^2 ...
                + ( Ic_mean(i,j,3,d) - Ip_mean(pc(2),pc(1),3,d) )^2 );
        end
        if  disMin > mean(dis)
            disMin = mean(dis) ;
            i_hold = i ;
            j_hold = j ;
        end
    end
end

In MATLAB I can read, access and change pixel values directly, such as:

Img(x,y) = 0 to set some pixel value to be 0 or Img(:,:,1) = 1 to set the pixels of certain channel to all be 0.

Can I also do those things as rapidly as what's shown above is in OpenCV?

Upvotes: 0

Views: 107

Answers (1)

cyriel
cyriel

Reputation: 3522

In MATLAB I can read, access and change pixel values directly, such as:

Img(x,y) = 0 to set some pixel value to be 0 or Img(:,:,1) = 1 to set the pixels of certain channel to all be 0.

Can I also do those things as rapidly as what's shown above is in OpenCV?

Of course this is possible. To set value of single pixel use:

img.at<img_single_element_type>(y,x) = 0;

where img_single_element_type depends on mat type and might be double, unsigned char, int, etc... See documentation for more information.

To set values in whole image (or part of image) use setTo method.


I don't know much about Matlab, so i can't help you with porting this code, but look at this and this project. It's similar project (object tracker - Open TLD(Tracking Learning Detection) aka Predator) written in Matlab(first link) and ported to C++(second link). Hope it helps.

Upvotes: 1

Related Questions