Archive

Posts Tagged ‘code’

Running Variance

November 9, 2009 mohsenam Leave a comment

Just in mid of the code wanted to find the Running Variance, just as habit typed and found not so good links.

1/N*[sum(i=1:N-1, Xi*Xi') + X*X'] – (1/N^2)*[ sum(i=1:(N-1), Xi) + X][ sum(i=1:(N-1), Xi) + X]‘

Matlab code for calculating the running Variance is as follow

%updating Mean

newMean = (newMean*(N-1) + X)/N;

sumOfSq = sumOfSq*(N-1)  + X*X';

covMat =  (1/N)*sumOfSQ - newMean*newMean'

Externally there are few links that are worth reading

OpenCv; Small Code using Erode and Dilate

February 16, 2009 mohsenam Leave a comment

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;

}>

Writing PPM image file in C++

November 20, 2008 mohsenam Leave a comment

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

Categories: C++, File-Handling, ImageProcessing Tags:

GLSL: Accessing one row of the texture

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

}

Categories: C++, GLSL, Opengl, Texture Tags: ,

Hexagonal Sampling and Interpolation

April 3, 2008 mohsenam Leave a comment

%Cartesian Sampling
x1 = -10:0.5:10;
y1 = -10:0.5:10;

[x y]= meshgrid(x1, y1);
f = cos(cos(sqrt(x.^2+y.^2)));
figure;
imagesc(f);
figure
mesh(x,y, f);

%the sampling lattice is
%[x y]‘ = |sqrt(3)/2 0| |i|
% |1/2 1| |j|
%hexagonal smapling
i = x;
j = y;
x = i*sqrt(3)/2 +0;
y = i*1/2 + j;
%x = i*sqrt(3)/2 +0;
f = cos(cos(sqrt(x.^2+y.^2)));
figure;
imagesc(f);
figure
mesh(x,y, f);

%TRYING TO RECOVER ORIGNAL SAMPLING
%i = -20:0.1/1.5:20;
minx = min(x(:));
maxx = max(x(:));
miny = min(y(:));
maxy = max(y(:));

miny = y(1,end);
maxy = y(end,1);

i = minx:0.5/4:maxx;
j = miny:0.5/4:maxy;
%j = -20:0.1:20;

[x2d y2d]= meshgrid(i, j);
figure;
plot(x2d,y2d, ‘b.’);hold on; plot(x,y, ‘r*’)

v = zeros(size(x2d));
for i=1:size(x2d,1)
i
for j=1:size(x2d,2)
b = boxSplineD2(x-x2d(i,j),y- y2d(i,j));
v(i,j)= sum(sum(b.*f));

end
end

figure;
imagesc(v);
figure
mesh(x2d,y2d, v);

Hexagonal Box-Spline; Matlab Code

April 3, 2008 mohsenam Leave a comment

Implementation of the Matlab Code Present in the “Three-Directional Box-Splines: Characterization and Efficient Evaluation” by Laurent Condat, Student Member, IEEE, and Dimitri Van De Ville, Member, IEEE
————————————————————————————————————

%2 degree

————————————————————————————————————

function b = boxSplineD2(x,y)

u = abs(x)-abs(y)/sqrt(3);
v = abs(x)+abs(y)/sqrt(3);
b = zeros(size(x));

ind = find(u <0);
u(ind) = -u(ind);
v(ind) = v(ind)+ u(ind);

ind = find(2*u <v);
u(ind) = v(ind)- u(ind);

g = u – v/2;

b = 5/6 + ((1+(1/3 – v/8).*v).*v/4-1).*v + …
((1-v./4).*v+g.*g./6-1).*g.*g;

ind = find(v < 1);
vi = v(ind);
gi = g(ind);
b(ind) = 0.5 + ((5/3-vi/8).*vi-3).*vi.*vi/4+((1-vi/4).*vi+gi.*gi/6.0-1).*gi.*gi;
clear vi gi;

————————————————————————————————————

%n-degree

————————————————————————————————————

function val = boxSplineDn(x,y, n)
x = -abs(x);
y = abs(y);

u = x-y/sqrt(3);
v = x+y/sqrt(3);
id = find(v>0);     v(id) = -v(id); u(id)=u(id)+v(id);

id = find(v > u/2); v(id) = u(id)-v(id);
val = zeros(size(x));

for K=-n:(ceil(max(max(u)))-1)
for L = -n:(ceil(max(max(v)))-1)
for i=0:min(n+K,n+L)
coeff = (-1)^(K+L+i)*nchoosek(n,i-K)*nchoosek(n,i-L)*nchoosek(n,i);
for d=0:n-1
aux = abs(v-L-u+K);
aux2= (u-K+v-L-aux)/2;
aux2(find(aux2<0))=0;
val =val + coeff*nchoosek(n-1+d,d)/…
factorial(2*n-1+d)/factorial(n-1-d)*…
aux.^(n-1-d).*aux2.^(2*n-1+d);
end
end
end
end
ind = find(u > 1);

b(ind) = ((v(ind)-2).^3).*(g(ind)-1)/6;
ind = find(v>2);
b(ind) = 0;      %outside support

%b = b/(sum(sum(b)));