Archive

Archive for the ‘basic’ Category

srun and matlab

November 19, 2011 Leave a comment

ok Mr. Cluster, here I come, I have unlocked the power to issue commands to your nodes for my maltab function ha ha ha ha!

so if you are in slurm
srun -n1 -o t.txt matlab -nodesktop -nosplash -nodisplay -r “temp4(29)” &

t.txt <— will get all the output

after -r you can give all the commands as the form of script i.e. ” ”

in above case temp4 was matlab file function, and 29 was input

& so that it can run in background

-n1 only one measly node ūüė¶

But then you can run multiple of such commands and each one gets node ūüôā

Don’t forget to open the matlabpool in the file you are running if you are using the parallel commands.

Categories: basic, code, matlab Tags: , , , , ,

calculating distance matrix; Matlab

July 7, 2010 Leave a comment

A simple code to calculate the L2 distance matrix between any features.

UPDATE: found pdist and pdist2 functions in matlab, so no more need of following function

function D = calculateDistMatrix( points1, points2, f)
%D = calculateDistMatrix( points1, points2)
%points1: is m by n matrix, where n is number of points
if nargin ‚̧
    f = 0;
end
totalPoints1 = size(points1,2);
totalPoints2 = size(points2,2);
if f == 0
D= zeros(totalPoints1, totalPoints2);
else
D = sparse(totalPoints1, totalPoints2);
end
for i=1:totalPoints1
    D(i,:) = sqrt(sum((points2-repmat(points1(:,i), 1,totalPoints2)).^2));
end

Simple Selecting Points in 2 images using Matlab GUIDE

January 9, 2010 4 comments

Following is the program that lets you load two images and click points in both of them. It does not store the correspondence however because it stores points in the array so if you keep the order same in the both images it could be used for the point correspondence.
Use the guide command to make it them you can work on the each component by clicking on each component and selecting callback function.

function varargout = momentsGUI(varargin)
% MOMENTSGUI M-file for momentsGUI.fig
%      MOMENTSGUI, by itself, creates a new MOMENTSGUI or raises the existing
%      singleton*.
%
%      H = MOMENTSGUI returns the handle to a new MOMENTSGUI or the handle to
%      the existing singleton*.
%
%      MOMENTSGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOMENTSGUI.M with the given input arguments.
%
%      MOMENTSGUI('Property','Value',...) creates a new MOMENTSGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before momentsGUI_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to momentsGUI_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help momentsGUI

% Last Modified by GUIDE v2.5 09-Jan-2010 14:30:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @momentsGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @momentsGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before momentsGUI is made visible.
function momentsGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to momentsGUI (see VARARGIN)

% Choose default command line output for momentsGUI
handles.output   = hObject;
handles.selected = 0;
handles.point1 = [];
handles.point2 = [];

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes momentsGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = momentsGUI_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.*');
img1 = imread([PathName FileName]);
%img1 = imread('carstick.bmp');
axes(handles.axes1);
imshow(img1);
handles.img1 = img1;
handles.size1 = size(img1);
% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.*');
img2 = imread([PathName FileName]);
axes(handles.axes2);
imshow(img2);
handles.img2 = img2;
handles.size2 = size(img2);
% Update handles structure
guidata(hObject, handles);
%ginput(2)

% --- Executes on button press in selectImg1PtsPushButton.
function selectImg1PtsPushButton_Callback(hObject, eventdata, handles)
% hObject    handle to selectImg1PtsPushButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axes(handles.axes1);
pt = ginput(1);
pt=fliplr(pt);

if (sum(pt <=handles.size1(1:2))*sum(pt>[0 0]))
    handles.point1 = [handles.point1 ; pt];
    guidata(hObject, handles);
    handles.point1
    pt = round(pt);
    axes(handles.axes1);
    hold on;
    plot(pt(2), pt(1), 'r*');
end

% --- Executes on button press in selectPointImg2.
function selectPointImg2_Callback(hObject, eventdata, handles)
% hObject    handle to selectPointImg2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes2);
pt = ginput(1);
pt=fliplr(pt);

if (sum(pt <=handles.size2(1:2))*sum(pt>[0 0]))
    handles.point2 = [handles.point2 ; pt];
    guidata(hObject, handles);
    handles.point1
    pt = round(pt);
    axes(handles.axes2);
    hold on;
    plot(pt(2), pt(1), 'r*');
end

Working with 3D matrix in Matlab

November 16, 2009 2 comments

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 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 https://whatevericode.wordpress.com/matlab/ but it much less rigorous

connecting OpenCv with Matlab; Basic

November 13, 2008 24 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: basic, matlab, OpenCv Tags: , , ,