Ask Your Question
0

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

asked 2018-08-13 07:29:17 -0500

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

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-08-13 07:43:16 -0500

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 ?
edit flag offensive delete link more

Comments

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

Samjith888 gravatar imageSamjith888 ( 2018-08-13 07:56:07 -0500 )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 ( 2018-08-13 07:59:06 -0500 )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 ( 2018-08-13 08:04:21 -0500 )edit

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

try to print out the filename before imread().

berak gravatar imageberak ( 2018-08-13 08:07:47 -0500 )edit

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

Samjith888 gravatar imageSamjith888 ( 2018-08-13 08:16:17 -0500 )edit
1

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

berak gravatar imageberak ( 2018-08-13 08:32:31 -0500 )edit

Yeah..Solved this simple issue through your suggestions.

Samjith888 gravatar imageSamjith888 ( 2018-08-14 06:23:18 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-13 07:29:17 -0500

Seen: 1,683 times

Last updated: Aug 13 '18