C# 从文件夹(OpenCV)读取图像
下面是读取名为Example1.jpg的图像的r、g、b值并在新文本文件out.txt中显示所有值的代码 但是如何读取文件夹中所有图像中的r、g、b值呢C# 从文件夹(OpenCV)读取图像,c#,c++,programming-languages,opencv,C#,C++,Programming Languages,Opencv,下面是读取名为Example1.jpg的图像的r、g、b值并在新文本文件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 i
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");
}
int main(int argc,char**argv)
{
//虽然文件夹中仍有图像,
//如何在文件夹中循环al图像并读取其r、g、b值//
//加载彩色图像
IplImage*img=cvLoadImage(“Example.jpg”,CV\u LOAD\u IMAGE\u COLOR);
//设置指针以访问图像数据
uchar*数据=(uchar*)img->imageData;
//获得通道和台阶;
int nchannels=img->nchannels;
int step=img->widthStep;
//计算r、g、b值
int b,g,r;
//显示图片的所有像素
int width=img->width;
int height=img->height;
int row,col;
//声明并打开输出文本文件
数据流输出;
outData.open(“out.txt”);
对于(row=0;row),您可以使用以下内容循环指定文件夹中的每个项目:
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));
}
}