从文件夹中读取图像(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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,您可以使用如下所示的方法循环遍历指定文件夹中的每个项目:
完成后,循环遍历数组中的每个条目并检查它是否包含 .jpg 或任何其他图像类型:
Well you could loop over every item in a specified folder using something like the following:
Once you have done that, loop over each entry in the array and check that it contains .jpg or any other image type: