hi,
i am doing project on image process. i am getting OutOfMemory Exception.
i reduced size of image from 84*106 to 40*60 and further down.
but it don't work. i have to process only 10 images.
please tell me solution how can i solve it?
hi,
i am doing project on image process. i am getting OutOfMemory Exception.
i reduced size of image from 84*106 to 40*60 and further down.
but it don't work. i have to process only 10 images.
please tell me solution how can i solve it?
If it's happening on the emulator, then you need to trace the exception, and find out where it is being thrown.
mention the size of images and the procedure to initialize them. Post ur code here.
thanks,
jitu_goldie..
KEEP TRYING..
thanks....
image size is 84*106 and m using JAMA package for calculating
matrix operation
The line which are written in bold may cause problem
as i removed them its working properly....
/**
* image size 84*106
*/
public class Spl123 {
double[][] eigenFace(double[][] face_v){ //face_v is [10][84*106]
int length = 84*106;
int nrfaces = 10;
int i, j, col,rows, pix, image;
double temp = 0.0;
//double[][] faces = new double[nrfaces][length];
double[] avgF = new double[length];
/*
Compute average face of all of the faces.
*/
for ( pix = 0; pix < length; pix++) {
temp = 0;
for ( image = 0; image < nrfaces; image++) {
temp += face_v[image][pix];
}
avgF[pix] = temp / nrfaces;
}
/*
Compute difference.
*/
for ( pix = 0; pix < length; pix++){
for ( image = 0; image < nrfaces; image++) {
face_v[image][pix] = face_v[image][pix] - avgF[pix];
}
}
//System.arraycopy(face_v,0,faces,0,face_v.length);
Sample faceM = new Sample(face_v, nrfaces,length);
Sample faceM_transpose = faceM.transpose();
Sample covarM = faceM.times(faceM_transpose);
int a = covarM.getRowDimension();
int b = covarM.getColumnDimension();
// double[][] v = covarM.getArray();
System.out.println(a);
System.out.println(b);
// System.out.println(avgF[1]);
System.out.println(nrfaces);
/*
for(i = 0;i<a;i++)
{
for(j=0;j < b ; j++)
{
System.out.println(v[i][j]);
}
}
/*
Compute eigenvalues and eigenvector.
*/
EigenvalueDecomposition E = covarM.eig();
double[] eigValue = diag(E.getD().getArray());
double[][] eigVector = E.getV().getArray();
/*
* We only need the largest associated values of the eigenvalues.
* Thus we sort them (and keep an index of them)
*/
int[] index = new int[nrfaces];
double[][] tempVector = new double[nrfaces][nrfaces]; /* Temporary new eigVector */
for ( i = 0; i <nrfaces; i++) /* Enumerate all the entries */
index[i] = i;
doubleQuickSort(eigValue, index,0,nrfaces-1);
// Put the index in inverse
int[] tempV = new int[nrfaces];
for ( j = 0; j < nrfaces; j++)
tempV[nrfaces-1-j] = index[j];
index = tempV;
/*
* Put the sorted eigenvalues in the appropiate columns.
*/
for ( col = nrfaces-1; col >= 0; col --) {
for ( rows = 0; rows < nrfaces; rows++ ){
tempVector[rows][col] = eigVector[rows][index[col]];
}
}
eigVector = tempVector;
eigValue = null;
tempVector = null;
Sample eigVectorM = new Sample(eigVector, nrfaces,nrfaces);
eigVector = eigVectorM.times(faceM).getArray();
/* Normalize our eigen vector matrix. */
for ( image = 0; image < nrfaces; image++) {
temp = max(eigVector[image]); // Our max
for ( pix = 0; pix < eigVector[0].length; pix++)
// Normalize
eigVector[image][pix] = Math.abs( eigVector[image][pix] / temp);
}
return eigVector;
}
//*******************************************************
public void fisherFace(double[][] face_f) //face_v is [10][84*106]
{
int length = 84*106;
int nrfaces = 10;
int nusr = 5;
int i, j,k, m, pix, image,tp;
double temp = 0.0;
double[][] tmp = face_f;
double[][] meanUser = new double[2][84*106];
double[][] row1= new double[10][length];
double[] avgTot = new double[length];
double[][] row = new double[2][length];
/*
Compute average face of all of the faces.
*/
for ( pix = 0; pix < length; pix++) {
temp = 0;
for ( image = 0; image < nrfaces; image++) {
temp += tmp[image][pix];
}
avgTot[pix] = temp / nrfaces;
}
/*
Compute difference.
*/
for ( pix = 0; pix < length; pix++){
for ( image = 0; image < nrfaces; image++) {
tmp[image][pix] = tmp[image][pix] - avgTot[pix];
}
}
/*
* Calculate mean of one user
*/
//m=0;
for(k=0;k<2;k++)
{
if(k==0)
{
image= j =0;
tp = nusr;
}else
{
image = j = k*nusr;
tp = image + nusr;
}
for ( pix = 0; pix < length; pix++) {
temp = 0;
for (image = j; image < tp; image++) {
temp += face_f[image][pix];
}
meanUser[k][pix] = temp / nusr;
}
}
/*Calculate diff bet one user and avg */
for(k=0;k<2;k++)
{
for ( pix = 0; pix < length; pix++){
row[k][pix] = meanUser[k][pix] - avgTot[pix];
}
}
Sample faceM = new Sample(row);
Sample faceM_transpose = faceM.transpose();
double[][] Sb = (faceM_transpose.times(faceM)).getArray();
Sample Sbb = new Sample(Sb);
//Sb = Sbb.times(faceM).getArray();
/*
* Calculate (each of user image) - (avg of that user)
*/
m=0;
for(k=0;k<2;k++)
{
for(i=0;i<5;i++,m++)
{
for ( pix = 0; pix < length; pix++)
{
row1[m][pix] = face_f[m][pix] - meanUser[k][pix];
}
}
}
faceM = new Sample(row1);
faceM_transpose = faceM.transpose();
// Sample Sw = faceM.times(faceM_transpose);
double[][] Sw = (faceM_transpose.times(faceM)).getArray();
Sample Sww = new Sample(Sw);
//Sw = Sww.times(faceM).getArray();
/*
* Calculation of face space
*/
meanUser = null;
row = row1 = null;
avgTot = null;
// length = nusr = null;
tmp = eigenFace(face_f);
Sbb = new Sample(Sb);
Sample tr = new Sample(tmp);
Sample tr1 = tr.times(Sbb);
tr = tr.transpose();
Sbb = tr1.times(tr);
Sww = new Sample(Sw);
tr = new Sample(tmp);
tr1 = tr.times(Sww);
tr = tr.transpose();
Sww = tr1.times(tr);
}
//*****************************************************************
static double max(double[] a) {
double b = a[0];
for (int i = 0; i < a.length; i++)
if (a[i] > b) b = a[i];
return b;
}
static double[] diag(double[][] m) {
double[] d = new double[m.length];
for (int i = 0; i< m.length; i++)
d[i] = m[i][i];
return d;
}
/**
* Quick sort on a vector with an index.
*
* @param a the array of numbers. This will be modified and sorted
* ascendingly (smalles to highest)
* @param index the index of the numbers as related to original
* location.
* @param lo the index where to start from. Usually 0.
* @param hi the index where to stop. Usually a.length()
*/
static void doubleQuickSort(double a[], int index[], int lo0, int hi0) {
int lo = lo0;
int hi = hi0;
double mid;
if ( hi0 > lo0) {
/* Arbitrarily establishing partition element as the midpoint of
* the array.
*/
mid = a[ ( lo0 + hi0 ) / 2 ];
// loop through the array until indices cross
while( lo <= hi ) {
while( ( lo < hi0 ) && ( a[lo] < mid )) {
++lo;
}
while( ( hi > lo0 ) && ( a[hi] > mid )) {
--hi;
}
// if the indexes have not crossed, swap
if( lo <= hi ) {
swap(a, index, lo, hi);
++lo;
--hi;
}
}
if( lo0 < hi ) {
doubleQuickSort( a, index, lo0, hi );
}
if( lo < hi0 ) {
doubleQuickSort( a, index,lo, hi0 );
}
}
}
static private void swap(double a[], int[] index, int i, int j) {
double T;
T = a[i];
a[i] = a[j];
a[j] = T;
// Index
index[i] = i;
index[j] = j;
}
}
Last edited by akshaychaudhari; 2009-04-08 at 19:15.
Assign null value to the objects and arrays which are not required this may help you KEEP TRYING
i tried it
its not working properly
Last edited by akshaychaudhari; 2009-04-08 at 18:40.
That code looked expensive!
Remember that an array of doubles is eight bytes per element. So, (84 * 106 * 10) doubles at eight bytes each, comes to around 800k.
I recommend you consider a less memory-intensive algorithm.
thanks for replying....
i reduced size to 40*60 and no of images from 5 to 3 still it shows error
I suggest you use the memory analyser in the Wireless Toolkit. Then you will see where the memory is being used.
The problem is simply that you are using too much memory.
Hi,
You must check this with the memory monitor...as grahm suggest...
And after a while you keep on calling Garbage collector from the memory monitor...and you will come to know that exactly how much is the memory required...watch the graph carefully,
check the object by clicking the objects Tab..
Also make sure that you are not creating any arrays,images,or so in in a loop...A deep code review is recommended..
Thanks with Regards,
R a j - The K e r n e l
Join Delhi-NCR Nokia Developer's Community,