Converting Image data to a Matrix (Help :])

Hi Guys, I am having some problems implementing some things in C++. I currently have a text file which has the information for a picture within it... Here is some sample code to give you an idea:

1
2
3
4
0.0000000e+000  0.0000000e+000  0.0000000e+000  0.0000000e+000
0.0000000e+000  1.0000000e+000  1.0000000e+000  0.0000000e+000
1.0000000e+000  1.0000000e+000  1.0000000e+000  1.0000000e+000
1.0000000e+000  0.0000000e+000  0.0000000e+000  1.0000000e+000


Basically I have managed to read this into my C++ (console) program like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double* readTxt(char *fileName, int sizeW, int sizeH)
{
  double* data = new double[sizeW*sizeH];
  int i=0;
  ifstream mytxtfile (fileName);
  if (mytxtfile.is_open())
  {
	 
	while ( mytxtfile.good())
    {
       if (i>sizeW*sizeH-1) break;
		 mytxtfile >> *(data+i);
         cout << *(data+i) << ' '; 
	     i++;                                                             
	}
    mytxtfile.close();
  }

  else cout << "Unable to open file"; 
  cout << i;

  return data;
}


This leaves me with a double called 'data'. My Problem/Question is how do I get this into a matrix.

Say for example I am given a binary image 512x512, my goal is to read this in via a .txt file, put it in a 1 dimensional matrix representing the image, and then be able to split it into smaller 32x32 squares for analysis.

I have tried to make my problem as clear as possible. A simple Matrix class solution would suffice just to show me how to put it into a Matrix. From the research I have done I found the following code for making a new Matrix starting with 1000, I am just struggling to apply it to my problem:

1
2
3
4
5
6
7
8
9
10
Matrix::Matrix(int M, int N){
	data = new double[M*N];
	for (int i=0; i<M; i++){
		for (int j=0; j<N; j++){
			data[i*N+j] = (double) 1000 + i*N + j;
			cout<<data[i*N+j]<<"\t";
		}
		cout<<"\n";
	}
}


Thanks in advance for any help, I hope this isn't too advanced for the beginners section.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Matrix::Matrix(int rows, int cols)
   {
      if(rows == 0){ rows = 1; cout << "Error: You specified a row size of 0\n" << endl; }
      if(cols == 0){ cols = 1; cout << "Error: You specified a column size of 0\n" << endl; }
      row = rows;
      col = cols;                
      mydata = new double *[row]; //double pointer  double **mydata
            
      for( int i = 0; i < row; i++){
           mydata[i] = new double[col];     
      }       
      
      this->initialize();           //function to set all values to 0.. 
   } 


1
2
3
4
5
6
7
void Matrix::set_element(int row, int col, ElementType new_value)
   {
        if(row < num_rows() || col < num_cols()){
        mydata[row][col] = new_value;
        }
        else{ cout << "Incorrect parameters" << endl; }
   } 


just create a double for loop using the second function

Last edited on
Cheers, I am hopefully going to spending a full day tomorrow trying to implement and finish my project :) I may be back yet ;)

Thanks :D
Topic archived. No new replies allowed.