gpt4 book ai didi

c# - 从文件夹中读取图像 (OpenCV)

转载 作者:行者123 更新时间:2023-11-28 08:18:18 25 4
gpt4 key购买 nike

下面的代码用于读取名为 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 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");
}

最佳答案

好吧,您可以使用类似以下的方法遍历指定文件夹中的每个项目:

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

关于c# - 从文件夹中读取图像 (OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6897101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com