Archive

Archive for the ‘basic’ Category

Working with 3D matrix in Matlab

November 16, 2009 mohsenam Leave a comment

To visualize the 3D matrix just consider that them as stack of images or layers.

{There is one more very important way,  that is third dimension representing the feature vector’s length}

So a(:, :, i) = all elements in the layer i, so changing i will give you next image.

where a(r,c, i) will move in the image.

a = [];
a(:,1,:) = [111 112 113 114 ; 121 122 123 124 ; 131 132 133 134];
a(:,2,:) = [211 212 213 214 ; 221 222 223 224 ; 231 232 233 234];
a(:,3,:) = [311 312 113 114 ; 321 322 323 324 ; 331 332 333 334];
a(:,4,:) = [411 412 113 114 ; 421 422 423 424 ; 431 432 433 434];
a(:,5,:)= [511 512 513 514 ; 521 522 523 524 ; 531 532 533 534];

Will make the 3D matrix ‘a’

size(a)

ans =

     3     5     4

That is there are 4 images and each image is of 3 rows and 5 cols.

But let’s Say you want to represent 3rd dimension as the feature vector  so each a(r,c, : ) represents a feature vector.

Now let’s say you want to make a 3D matrix from one feature vector.


vt = squeeze([a( 2,1, : ) ] );
vt = vt(:)';
%//make it into 2 by 3 by length(feature) matrix
%//repmat will repeat this matrix and make a 6 row matrix
%//the reshape picks the elements from the 1st col, 1st row and start moving downward in the row and so on
%//therefore each time it will meet same element as it moves down the row and fills our first image
tempT = (reshape(repmat(vt, 2*3,1), 2, 3, length(vt)));
size(tempT)
ans =
     2     3     4

Matlab Efficient coding

October 14, 2009 mohsenam Leave a comment

The interpreter nature of Matlab keeps me always looking for coding it fast and efficient way, squeezing as much work as we can squeeze from single statement.

Searching for something about Matlab found following blog entry

http://omar-sabih.blogspot.com/2009/05/matlab-tryin-to-code-smarter.html

It’s much cleaner and better presented version is present at http://www.sajidmc.net/bn/2009/05/efficient-matlab-coding/

Although I also have my  Matlab page http://whatevericode.wordpress.com/matlab/ but it much less rigorous

connecting OpenCv with Matlab; Basic

November 13, 2008 mohsenam 9 comments

It took much of the time, but I was able to connect OpenCv with matlab.

Much thanks to the

http://www.mathworks.com/matlabcentral/fx_files/21818/1/OpenCV_And_MEX_Files_quick_guide.pdf

I took the introductory program given in the http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html

Also help from

Writing C Functions in Matlab form Jason Laska http://cnx.org/content/m12348/latest/

And modified it to run through matlab.

I named it showImageMt.cpp

So the Matlab code for compiling it was

mex showImageMt.cpp

But for that you have setup the file ‘mexopts.bat‘ as described in the above pdf . You might not able to find it by searching the matlab folder. You can find the path by running the following command in the Matlab

fullfile(prefdir,’mexopts.bat’)

The contents of the file could be see by type(fullfile(prefdir,’mexopts.bat’))

The C++ opencv code is following

////////////////////////////////////////////////////////////////////////
//
//showImageMt.cpp
//
// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result.
//
////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

#include "mex.h"

void justWork(IplImage *img){
int height,width,step,channels;
uchar *data;
int i,j,k;

// get the image data
height    = img->height;
width     = img->width;
step      = img->widthStep;
channels  = img->nChannels;
data      = (uchar *)img->imageData;
printf("Processing a %dx%d image with %d channels\n",height,width,channels);

// create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);

// invert the image
for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

// show the image
cvShowImage("mainWin", img );

// wait for a key
cvWaitKey(0);

};

void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){

if (nrhs != 0)
{
mexErrMsgTxt("One input required.");
}
else if (nlhs > 0)
{
mexErrMsgTxt("Too many output arguments");
}

IplImage* img = 0;

/*if(argc<2){
printf("Usage: main <image-file-name>\n\7");
exit(0);
}*/

char *name = "lena.jpg";
// load an image
img=cvLoadImage(name);
if(!img){
printf("Could not load image file: %s\n",name);
exit(0);
}
justWork(img);
// release the image
cvReleaseImage(&img );
return;
}

Read more…

Categories: OpenCv, basic, matlab Tags: , , ,