2011-08-01 181 views
0

下面是讀取名爲Example1.jpg的圖像的r,g,b值並在新的文本文件out.txt中顯示值的代碼。從文件夾(OpenCV)讀取圖像

但是如何讀取文件夾內所有圖像中的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

那麼你可以遍歷每一個項目中使用類似下面的一個指定的文件夾:

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)); 
} 
}