Ask Your Question
0

SVM training image coming out full white? [closed]

asked 2017-04-10 12:43:29 -0600

cartycrabber gravatar image

I'm attempting to create an SVM classifier that trains from images in a couple folders. I'm able to get the images fine, but when I followed another post to create the training image, I put an imshow at the end to see what it looked like and it is just a fully white image. Is that normal, or have I done something wrong?

Here is my code:

//List of files in each training directory
std::vector<cv::String> ltvFiles;
std::vector<cv::String> mtvFiles;
std::vector<cv::String> htvFiles;

//Get the training image file names
cv::glob("data/training/ltv", ltvFiles);
cv::glob("data/training/mtv", mtvFiles);
cv::glob("data/training/htv", htvFiles);

//Number of files across all directories
int numFiles = ltvFiles.size() + mtvFiles.size() + htvFiles.size();

//Look at one image to get the area (assuming all the other images are the same size)
cv::Mat sample = cv::imread(ltvFiles[0]);
int imgArea = sample.rows * sample.cols;
sample.release();

//Create the training matrix with numFiles rows and imgArea columns
cv::Mat trainingMat(numFiles, imgArea, CV_32FC1);

cv::Mat img;
int col = 0;
//go through LTV images first
for (int image = 0; image < ltvFiles.size(); image++) {
    //Open the next image
    img = cv::imread(ltvFiles[image], 0);
    //Start in the first column
    col = 0;

    //Iterate through each pixel in the opened image and place it in the next location in the current row
    for (int y = 0; y < img.rows; y++) {
        for (int x = 0; x < img.cols; x++) {
            trainingMat.at<float>(image, col++) = img.at<uchar>(y, x);
        }
    }
}

//Skipping code for MTV and HTV

//Show the result
cv::imshow("Training image", trainingMat);

I'm wondering if it has something to do with this line in the bottom for loop

trainingMat.at<float>(image, col++) = img.at<uchar>(y, x);

Is the uchar to float a problem?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by cartycrabber
close date 2017-04-10 16:51:07.897597

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-10 13:11:26 -0600

berak gravatar image

imshow() does some tricks under the hood, i.e float images are assumed to be in [0..1] range, and thus multiplied by 255 internally.

cv::imshow("Training image", trainingMat / 255);

will probably look ok in your case.

edit flag offensive delete link more

Comments

Worked exactly like you said, thanks!

cartycrabber gravatar imagecartycrabber ( 2017-04-10 16:51:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-10 12:43:29 -0600

Seen: 143 times

Last updated: Apr 10 '17