First time here? Check out the FAQ!

Ask Your Question
0

How to read multiple images from a path in c++?

asked Aug 13 '18

Samjith888 gravatar image

I have to extract HOG features of 37 images from a folder(RGB images), hence i wrote the following code. But when i run this ,the program breaks in 'if (!img.data)' .

using namespace cv; using namespace std;

int main()

{

HOGDescriptor hog;

hog.winSize = Size(32, 48);

for (size_t i = 1; i <= 37; ++i)
{

    ostringstream os;
    os << " C:/Users/Sam/Desktop/HumanCount/Training Images/Content/ " << "positive\\" << std::setw(4) << std::setfill('0') << i << ".JPG";

    Mat img = imread(os.str(), CV_LOAD_IMAGE_GRAYSCALE);

    if (!img.data)
    {
        break;
    }
    else
    {

        // obtain feature vector:
        vector<float> featureVector;
        hog.compute(img, featureVector, Size(8, 8), Size(0, 0));

    }


    return 0;
}

}

Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Aug 13 '18

berak gravatar image

there are alternatives to std::stringstream, e.g:

String path = format("C:/Users/Sam/Desktop/HumanCount/Training Images/Content/positive/%04d.JPG", i);

or avoid the whole number parsing:

vector<String> fn;
glob("C:/Users/Sam/Desktop/HumanCount/Training Images/Content/positive/*.JPG", fn);
for (auto f:fn) 
    Mat img = imread(f,IMREAD_GRAYSCALE);

but the main problem in your code is a silly typo:

os << " C:/Users/ .....
       ^ there should be no space here, right ?
Preview: (hide)

Comments

this can't solve my issue. Still the program can't read the image , it fails in 'if(!img.data)'.

Samjith888 gravatar imageSamjith888 (Aug 13 '18)edit

all the image images in the folder are BGR images . is they need to convert into grayscale before 'Mat img = imread(f,IMREAD_GRAYSCALE);' this function??

Samjith888 gravatar imageSamjith888 (Aug 13 '18)edit

The 'positive' is not a folder in my directory. That is a tag to classify those images as positive. link text

Samjith888 gravatar imageSamjith888 (Aug 13 '18)edit

no conversion nessecary, then. (only if you omit the flag)

try to print out the filename before imread().

berak gravatar imageberak (Aug 13 '18)edit

@berak : cout << os.str(); Did you meant this before imread? this not working

Samjith888 gravatar imageSamjith888 (Aug 13 '18)edit
1

"is not working" is not a working description of anything.

berak gravatar imageberak (Aug 13 '18)edit

Yeah..Solved this simple issue through your suggestions.

Samjith888 gravatar imageSamjith888 (Aug 14 '18)edit

Question Tools

1 follower

Stats

Asked: Aug 13 '18

Seen: 3,208 times

Last updated: Aug 13 '18