2011-08-01 2 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)); 
} 
} 
관련 문제