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

发布于 2024-11-27 12:52:34 字数 1218 浏览 1 评论 0原文

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

Below is the code to read the r,g,b values of an image, named Example1.jpg, and display al the values in a new text file, out.txt.

But how to read the r,g,b values in all images inside a folder?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哭泣的笑容 2024-12-04 12:52:34

那么,您可以使用如下所示的方法循环遍历指定文件夹中的每个项目:

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

Well you could loop over every item in a specified folder using something like the following:

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

Once you have done that, loop over each entry in the array and check that it contains .jpg or any other image type:

foreach(string s in filePaths)
{
if (s.Contains(".jpg"))
{
    CallYourFunction(System.IO.Path.GetFileName(s));
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文