Чтение изображений из папки (OpenCV) - PullRequest
0 голосов
/ 01 августа 2011

Ниже приведен код для чтения значений изображения r, g, b изображения с именем Example1.jpg и отображения всех значений в новом текстовом файле out.txt.

Но как прочитать значения r, g, b во всех изображениях в папке?

int main(int argc, char** argv)
{   

//while there are still images inside the folder,
//how to loop al images in a folder and read their r,g,b values//

//load the image in color
IplImage *img = cvLoadImage("Example.jpg",CV_LOAD_IMAGE_COLOR);

//set up pointer to access image data
uchar * data = (uchar*) img->imageData;

//get nchannels and step;
int nchannels = img->nChannels;
int step      = img->widthStep;

//calculate r,g,b value 
int b,g,r;

//display all pixel of the picture
int width = img->width;
int height= img->height;
int row,col;

//declare and open a output text file
ofstream outData;
outData.open("out.txt");

for (row=0; row<height; row++){

    for(col=0; col<width; col++){
    b = data[col*step + row*nchannels + 0];
    g = data[col*step + row*nchannels + 1];
    r = data[col*step + row*nchannels + 2];

    outData<<r<<" "<<g<<" "<<b<<endl;
    }
}

//wait user press key to exit
cvWaitKey(0);

//free memory
cvDestroyWindow("Skin");
cvReleaseImage(&img);

//press any key to continue
system("PAUSE");
}

1 Ответ

1 голос
/ 01 августа 2011

Хорошо, вы можете зациклить каждый элемент в указанной папке, используя что-то вроде следующего:

string filePath = @"C:\Files\";
string[] filePaths = Directory.GetFiles(filePath);

После того, как вы это сделаете, переберите каждую запись в массиве и убедитесь, что она содержит .jpg илилюбой другой тип изображения:

foreach(string s in filePaths)
{
if (s.Contains(".jpg"))
{
    CallYourFunction(System.IO.Path.GetFileName(s));
}
}
...