What Ever I Code

February 18, 2009

stl Sort in C++; simple code for sorting the array of structures

Filed under: STL, code — Tags: , , , , — mohsenam @ 5:59 am

Simple Code that shows how to use the C++ STL function sort()

#include <iostream>
#include <algorithm>

using namespace std;

struct Numb{
double num;
int pos1;

bool operator <(const Numb &n1){
return num < n1.num;
};
};
////////////////////////////////
void main(void){

Numb numb[10];

for(int i=0; i < 10; i++){
numb[i].num = rand();
numb[i].pos1 = i;
}
cout << endl << “———” <<endl;
for(int i=0; i < 10; i++){
cout << ” ” << “(” << numb[i].pos1 << “) ” << numb[i].num;
}
cout << endl << “———” <<endl;

std::sort( numb, numb+10);
cout << endl << “———” <<endl;
for(int i=0; i < 10; i++){
cout << ” ” << “(” << numb[i].pos1 << “) ” << numb[i].num;
}

cout << endl << “———” <<endl;

}

February 16, 2009

OpenCv; Small Code using Erode and Dilate

Filed under: ImageProcessing, OpenCv, code — mohsenam @ 10:55 pm

Small Code that reads pbm image, convert it into 1 channel image. Erodes it, dilates it and shows them 

#include <float.h>

#include <limits.h>

#include <time.h>

#include <ctype.h>

#include <iostream>

 

#include "BlobResult.h"

#include "blob.h"

 

#ifdef _EiC

#define WIN32

#endif

 

using namespace std;

void main(void){

IplImage *inputImage = cvLoadImage( "0584.pbm", 1 );

 

IplImage *gray = cvCreateImage( cvSize(inputImage->width,inputImage->height), 8, 1 );

cvCvtColor( inputImage, gray, CV_BGR2GRAY );

IplImage*img = inputImage;

inputImage = gray;

::cvReleaseImage(&img);

 

cvNamedWindow( "orig", 1 );

    cvShowImage( "orig", inputImage );

 

IplImage *outputImage;

IplImage *outputImage2;

  outputImage = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

outputImage2 = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

 

cvErode(inputImage, outputImage, 0, 1);

cvNamedWindow( "erode", 1 );

 cvShowImage( "erode", outputImage );

 

 

  cvDilate(outputImage, outputImage2, 0, 2);

cvNamedWindow( "dilate", 1 );

 cvShowImage( "dilate", outputImage2 );

 

cvReleaseImage(&outputImage);

cvReleaseImage(&outputImage2);

 cvWaitKey();

 

return;

}>

 

February 11, 2009

converting simple STL list to circular list

Filed under: C++, STL — Tags: — mohsenam @ 5:28 am

/////////////////////////////////////////////////////////
//converting simple STL list to circular list
/////////////////////////////////////////////////////////
list<int>::iterator nextIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
i++;
if(i == end){i = beg;}
return i;
}
/////////////////////////////////////////////////////////
list<int>::iterator prevIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
if(i == beg){i = end; i–;}
else{i–;}
return i;
}
/////////////////////////////////////////////////////////
void printL(list<int> &L){
cout << endl;
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << ” “;
cout << endl;
}

Simple STL code in C++

Filed under: STL — Tags: , — mohsenam @ 5:24 am

Just a general simple program for the Standard Template Library (STL) List

#include <list>
#include <iostream>
using namespace std;
////////////////////////////////
class codeWord{
public:
double vl[3];
double minI;

codeWord(){
minI = 0;
vl[0]=vl[1]=vl[2] = 0;
};
};
////////////////////////////////
void printList(std::list<codeWord *> &ml){
cout <<endl;

codeWord *ptr;
for(std::list<codeWord *>::iterator list_iter = ml.begin(); list_iter != ml.end(); list_iter++)    {
ptr = *list_iter;
std::cout<<” ” << ptr->minI<<endl;
}

cout <<endl;

};
////////////////////////////////
void main(void){

std::list<codeWord *> mlist;
codeWord a;
codeWord b;

a.minI = 10;
b.minI = 5;

mlist.push_front(&a);
mlist.push_front(&b);

printList(mlist);

};

November 20, 2008

Writing PPM image file in C++

Filed under: C++, File-Handling, ImageProcessing — Tags: — mohsenam @ 9:55 pm

Following is the C++ code for writting the ppm image file

strtemp has file name,

fData is the pointer to the image data.

sprintf(strtemp,”%04d.ppm”,imageNo);
ofstream output(strtemp, ios::binary|ios::out);
if(!output){
cout << ” unable to open the output file “<< strtemp << endl;
}
else{
output << “P6″<< endl <<”# foreground “<<endl;
output << itoa(IWidth, strtemp, 10);
output << ” “;
output << itoa(IHeight, strtemp, 10);
output << endl;
output << itoa(255, strtemp, 10) << endl;
output.write( (char *)fData, IHeight*IWidth*3);
output.close();

};//end of else

November 17, 2008

OpenCv IplImage how to clear or initialize the image to scalar

Filed under: C++, ImageProcessing, OpenCv — Tags: , , , — mohsenam @ 4:33 am

just use the cvSet

e.g if img is the IplImage object pointer with 3 channels.

cvSet(img, cvScalar(0,0,0));

will set each and every pixel to zero.

Becarefull with the  channels and cvScalar used.

November 13, 2008

connecting OpenCv with Matlab; Basic

Filed under: OpenCv, basic, matlab — Tags: , , , — mohsenam @ 7:37 am

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;
}

(more…)

November 6, 2008

Principal Component Analysis (Matlab Code)

Filed under: matlab — Tags: , , , — mohsenam @ 8:15 pm

function [evec, eval, meanA] = PCA(A, maxVec)

%Principal Component Analysis
%A has th
[ rows, cols] = size(A);
if(nargin < 2)
maxVec = rows;
end

maxVec = min(maxVec, rows);

meanA = mean(A,2);
A = A – meanA*ones(1, cols);
%[evec eval] = eig(A’*A);
[evec eval] = eig(A*A’);

[eval ind]  =  sort(-1*diag(eval));
%[eval ind]  =  sort(diag(eval), ”);
eval    = -1*eval(1:maxVec);
evec    = evec(:, ind(1:maxVec));
(more…)

May 1, 2008

GLSL: Accessing one row of the texture

Filed under: C++, GLSL, Opengl, Texture — Tags: , — mohsenam @ 11:44 pm

//Fragment Shader Program to access texture and just copying one row to other

uniform sampler2D textureX;
uniform float        texRows;
uniform float        texCols;

void main(void) {
//cols                rows
vec4 x            = texture2D(textureX, vec2(gl_TexCoord[0].s, gl_TexCoord[0].t+1.0/texRows));
gl_FragColor    = x;

}

GLSL Texture

Filed under: C++, GLSL, Opengl — Tags: , — mohsenam @ 10:18 pm

Coordinates of the gl_TexCoord[0]

gl_TexCoord[0].t —— rows

gl_TexCoord[0].s ——cols

vec4 x = texture2D(textureX, vec2(Columns, rows));

gl_FragColor = x;

Older Posts »

Blog at WordPress.com.