Load, show and convert multiple images

Hi there,
I'm starting to write my first program in C++ with OpenCV and I would like to represent a set of images (stored in my project and libelled "brain_mri_001.jpg -> brain_mri_015.jpg) as vectors of length LxL where L is the number of pixels in the x(y) direction.
Here is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// essai.cpp : définit le point d'entrée pour l'application console.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
using namespace std;

int main()
	{
		int height,width;
		//Load image
		for(int j=0; j<=15; j++)
			
			{
				IplImage *img=cvLoadImage("brain_mri_00%j.jpg", CV_LOAD_IMAGE_GRAYSCALE);
				if (!img)	
					{
						printf("Error: Can't open the file.\n");
						return 2;//erreur pas de fichier trouvé
					}
				cvNamedWindow("Projet Image", CV_WINDOW_AUTOSIZE);// créer une fenêtre
				IplImage *img2=cvCloneImage(img); //cloner img
				cvShowImage("Projet Image", img2); //afficher image fenêtre
		
				int height,width;
				height = img->height;  // récupérer la hauteur de img
				width = img->width; // récupérer la largeur de img
				printf("the size of the image %j is %dx%d",height,width);
//convertir une matrice heightxwidth à un vecteur unique 1x(heightxwidth)
		//matrice 3x3 à un vecteur 1x9
		CvMat* mat = cvCreateMat(height,width, CV_32F); //CvMat : 2D array
		CvMat row_header, *row;
		row = cvReshape(mat, &row_header, 0, 1);

			}
	}

I should have made a mistake but I don't know where :(
I would be grateful if anyone can answer me !
P.S. Excuse my bad English...
Last edited on
http://www.cplusplus.com/forum/articles/40071/#msg218019
cvLoadImage("brain_mri_00%j.jpg", CV_LOAD_IMAGE_GRAYSCALE); AFAIK the function expects a file name, it doesn't work as printf to deduce it. ¿And what does the '%j' do?

In order to construct the file name use sprintf or stringstream
Thanks for your answer, I didn't explain my question well... Sorry !

What I wanted to do is to load a set of 25 images (brain_mri_001.jpg, ..., brain_mri_025.jpg) in order to convert them later on into Matrix and then into Vectors.

So I started by loading them using a loop and increment j, you are right about j, I made a syntax mistake.

The problem is there are a lot of arguments in cvLoadImage and I don't know how to use stringstream.

The aim is to compute the average image (vector actually after the conversion) of my set. I tried to improve my code and here is what I have :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
using namespace std;

int main()
	{
		//load images
		for(int i=1; i<=25; i++)
			{
				char filename[50];
				sprintf( filename, "brain_mri_%d.jpg", i ); 
				IplImage *img=cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE);
				if (!img)	
					{
						printf("Error: Image not found.\n");
						return 2; //error : not found image
					}

				cvNamedWindow("Projet Image", CV_WINDOW_AUTOSIZE);// create a window
				IplImage *img2=cvCloneImage(img); //cloner img
				cvShowImage("Projet Image", img2); //display the image in a window

				cvWaitKey(0); //attendre touche
				cvDestroyWindow("Projet Image"); //destroy the window
				cvReleaseImage(&img); //memory

				return 0; //finish with success
			
				//convert IplImage -> Matrix 
				int height = img->height;  
				int width = img->width; 
				CvMat *mat = cvCreateMat(height,width,CV_32FC3 );

				//convert Matrix -> Vector 
				//CvMat row_header, *row;
				//row = cvReshape(mat, &row_header, 0, 1);
				CvMat vector_header;
				cvReshape(img, &vector_header, 0, 1);

				//check the height and width of vector_header
				if(vector_header.height != 1)
					{
						fprintf(stderr, "vector_header's height is %d\n", vector_header.height);
					}
				if(vector_header.width != width*height)
					{
						fprintf(stderr, "vector_header's width is %d\n", vector_header.width);
					}
			}
	}
		

		



Is what I write correct ? I'm trying to learn programming for the first time and making an effort :) Thanks again !



Last edited on
¿Does it compile?
http://www.ai.rug.nl/vakinformatie/pas/content/Highgui/opencvref_highgui.htm ¿Is this what you are using?
IplImage* cvLoadImage( const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR ); where filename is the name of the file.

stringstream works like cout
sprintf works like printf
¿Are you familiar with any of those? How you use them is the same, except that you print to a "string" rather than the console.
1
2
3
char filename[big_enough];
sprintf( filename, "brain_mri_%d.jpg", i ); //look for the correct format to get the leading zeros
IplImage *img=cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE);


By the way, I think that you are using this too early cvReleaseImage(&img);
To answer your questions :

1) Yes, it compiles but only the first image : "brain_mri_1.jpg" appears. Displaying the images is not what I want to do, it was just for test. Load all the images is most important for me. How can I, please, be sure that they (the images) are all loaded and converted into matrix and then into vectors of lengh LxL where L is the number of pixels in the x(y) direction ?

2) Yes, I'm using the Simple GUI Window, I updated my code in the previous post as you advised me to do.

3) No, I'm not familiar with sprintf and stringstream, I know more about cout and printf. Thanks for the explanation :)

Finally, I put cvReleaseImage(&img); below.
only the first image : "brain_mri_1.jpg" appears.
That's because you terminate the program in line 28...
¿what is the filename brain_mri_1.jpg or brain_mri_001.jpg ?

http://opensourcecollection.blogspot.com/2011/02/how-to-convert-iplimage-to-matrix.html
Topic archived. No new replies allowed.