Ask Your Question
0

How to loop over all the images using openCV 3.0.0 and C++?

asked 2016-02-20 11:22:05 -0600

Faith gravatar image

updated 2016-02-20 14:01:42 -0600

i want to loop over my images that saved on my working folder and apply in all of them a converter to gray and then to equalized. I have the code but just for one image and I want it for 50 images.
Here is the code:

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main()
{

    Mat image;
    image = imread("user2-6.jpg", CV_LOAD_IMAGE_COLOR);

    if (!image.data)
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }

    // Create a new matrix to hold the gray image
    Mat gray;

    // convert RGB image to gray
    cvtColor(image, gray, CV_BGR2GRAY);

    namedWindow("Display window", CV_WINDOW_AUTOSIZE);
    imshow("Display window", image);

    namedWindow("Result window", CV_WINDOW_AUTOSIZE);
    imshow("Result window", gray);


    Mat img = imread("user2-6.jpg", CV_LOAD_IMAGE_COLOR); //open and read the image

    if (img.empty())
    {
        cout << "Image cannot be loaded..!!" << endl;
        return -1;
    }

    cvtColor(img, img, CV_BGR2GRAY); //change the color image to grayscale image

    Mat img_hist_equalized;
    equalizeHist(img, img_hist_equalized); //equalize the histogram

    //create windows
    namedWindow("Original Image", CV_WINDOW_AUTOSIZE);
    namedWindow("Histogram Equalized", CV_WINDOW_AUTOSIZE);

    //show the image
    imshow("Original Image", img);
    imshow("Histogram Equalized", img_hist_equalized);

    waitKey(0); //wait for key press

    destroyAllWindows(); //destroy all open windows

    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-02-20 11:54:00 -0600

berak gravatar image

you can use cv::glob to traverse a whole directory of images:

String folderpath = "d:/mymages/*.jpg"
vector<String> filenames;
cv::glob(folderpath, filenames);

for (size_t i=0; i<filenames.size(); i++)
{
    Mat im = imread(filenames[i]);
    ... your processing here
}
edit flag offensive delete link more

Comments

see also http://answers.opencv.org/question/69712

it will be nice if someone update documentation of the function and add a sample

sturkmen gravatar imagesturkmen ( 2016-02-20 14:05:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-02-20 11:22:05 -0600

Seen: 8,983 times

Last updated: Feb 20 '16