Reading images using successive frames in
OpenCv without using VideoCapture function:
OR
Reading
images using a for loop in OpenCV
We want to read images in a
folder by using for loop.
Thus we need to number the
images sequentially
Here i have renamed the images
as image001.png , image002.png etc.
Note:
You just need to rename the
image sequentially and provide a proper path to it.
It can be any type of image
.bmp , .jpg, .png etc.
For e.g Arjun001.jpg ,
Arjun002.jpg ,Arjun003.jpg etc
But the name which you are
using before the sequence should be given in the path while coding.
Here is the code below:
Here the
path to my image file is C:\Users\arjun\Desktop.
While the name of my image starts from "image" i.e. image001.png
If it was Arjun001.png then the path
should have been:
C:\\Users\\arjun\\Desktop\\Arjun
%03i denotes the no of zeros in the name
and total size of the string for e.g 001 , 002 , 003, 004 etc
So change your name accordingly if you
make changes in the code.
Here %03i denotes there are there digits
in the number with the last digit being ‘i’ and preceding it are all zeroes.
Explanation of the code:
//defines the variable 'filename' taking
character input of max 100.
char filename[100]
sprintf(filename,
"C:\\Users\\arjun\\Desktop\\image,number");
//The sprintf() function
prototype is
int sprintf(char *str, const char
*format, arg_1, arg_2, arg_3, ... , arg_n);
Composes a string with the same text
that would be printed if format was used on printf, but instead of being
printed, the content is stored as a C string in the buffer pointed
by str.
//The image is loaded by a function
cvLoadImage in a variable called 'image'
image = cvLoadImage(filename);
//No. of image =6
int nImages = 6;
//For loop for concating the path of the
image
for (int i = 1; i <
nImages; ++i);
{
loadImage(im, i);
char filename[100];
strcpy(filename,
"C:\\Users\\arjun\\Desktop\\image");
char frameNo[10];
sprintf(frameNo,
"%03i", i);
strcat(filename, frameNo);
strcat(filename,
".png");
//Load the image
IplImage *im = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR);
cvNamedWindow("pic"); //Name of the image window
cvShowImage("pic",im); //Display the image window
cvWaitKey(1000);
//Wait for 1 sec
}
Here is the link of the code:
Here is the code:
/* Read images using successive frames in OpenCv without using VideoCapture function*/ #include "stdlib.h" #include "math.h" #include "opencv/cv.h" // include it to used Main OpenCV functions. #include "opencv/highgui.h" //include it to use GUI functions. using namespace std; using namespace cv; void loadImage(IplImage *image, int number) { // Store path to directory char filename[100]; sprintf(filename, "C:\\Users\\arjun\\Desktop\\image,number"); image = cvLoadImage(filename); } int main(int argc, char* argv[]) { IplImage *im=0; int nImages = 6; for (int i = 1; i < nImages; ++i) { loadImage(im, i); char filename[100]; strcpy(filename, "C:\\Users\\arjun\\Desktop\\image"); char frameNo[10]; sprintf(frameNo, "%03i", i); strcat(filename, frameNo); strcat(filename, ".png"); IplImage *im = cvLoadImage(filename,CV_LOAD_IMAGE_COLOR); cvNamedWindow("pic"); cvShowImage("pic",im); cvWaitKey(1000); } }
No comments:
Post a Comment