I've trained my LDA on several thousand images and was keen to save it so that I could load it in the main program that would be doing predictions (rather than running it fresh each time). I knew that with an SVM I can use a command like this to save it:

SVM.save("tag_svm.yml");

So I tried a similar thing for LDA, and that also seemed to work:

cv::LDA lda(2); lda.compute(trainData, trainLabels); cv::Mat projected = lda.project(trainData); lda.save("tag_lda.yml");

Then, in my prediction program, I load the LDA and try to project the new data:

cv::LDA lda(2); lda.load("tag_lda.yml"); cv::Mat projected = lda.project(testData);

The loading part worked fine, however when I try to project the new data, I get this error:

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2).) in subspaceProject error: (-5) Wrong shapes for given matrices. Was size(src) = (256,30), size(W) = (256,2). in function subspaceProject

But that doesn't make sense to me: the data I trained the LDA on had 256 columns, it was grayscale pixel values. I did exactly the same processing for my test images as I did for my training images. If I don't bother about loading the LDA and just have the prediction stage in the same program it works perfectly (same code for processing the test images too!), so I can't understand why this error keeps occurring.

To clarify, I've been running std::cout on trainData and to confirm it has 256 cols and 30 rows but still get the error. This is driving me crazy because if I deliberately make testData an incorrectly sized matrix (not the 256 cols it's expecting) than I get the same answer!

The main section of code