Hello.
I am trying to create a train Model but i am getting this error:
(-215:Assertion failed) responses.type() == CV_32F || responses.type() == CV_32S in function ‘setData’
#include <opencv2/highgui.hpp>
#include <string>
#include <opencv2/opencv.hpp>
#include "cv_idx.hpp"
std::string LABELS = "static/t10k-labels.idx1-ubyte";
std::string IMAGES = "static/t10k-images.idx3-ubyte";
std::string CHECK_LABELS = "static/train-labels.idx1-ubyte";
std::string CHECK_IMAGES = "static/train-images.idx3-ubyte";
/**
* Notes:
* [ML Intro](https://docs.opencv.org/master/dc/dd6/ml_intro.html)
* [Example SVM](https://docs.opencv.org/4.5.3/d1/d73/tutorial_introduction_to_svm.html)
*/
int main(void)
{
// Cargar las etiquetas
CV_idx idx_labels(LABELS);
// Carga las imágenes
CV_idx images{IMAGES};
// Pasarlos a espacio CV
cv::Mat labelsMat((int)idx_labels.number_of_elements, 1, CV_8UC1, static_cast<uint8_t*>(idx_labels));
auto imagesMat = static_cast<std::vector<cv::Mat> >(images);
// Train the Stochastic Gradient Descent SVM
cv::Mat trainModelData;
trainModelData.convertTo(trainModelData, CV_32F);
for(auto image: imagesMat) {
cv::Mat normal;
image.convertTo(normal, CV_32F, 1.0/255.0);
cv::Mat image_row((int)normal.total(), 1, CV_32F);
if(!normal.isContinuous()){assert(0);}
image_row.data = normal.data;
trainModelData.push_back(image_row);
}
cv::Ptr<cv::ml::SVMSGD> svmsgd = cv::ml::SVMSGD::create();
svmsgd->train(trainModelData, cv::ml::ROW_SAMPLE, labelsMat);
// Print the images
/*CV_idx check_labels{LABELS};
CV_idx check_images{IMAGES};
cv::Mat check_labelsMat((int)idx_labels.number_of_elements, 1, CV_8UC1, static_cast<uint8_t*>(check_labels));
auto check_imagesMat = static_cast<std::vector<cv::Mat> >(check_images);
namedWindow("Display digit", cv::WINDOW_AUTOSIZE);
for(auto image: check_imagesMat) {
cv::Mat normal;
image.convertTo(normal, CV_32F, 1/255.0);
normal.reshape(1, normal.total());
std::cout << svmsgd->predict(normal, responses) << std::endl;
imshow("Display digit", image);
cv::waitKey(0);
}*/
}
i already checked that the images are correct and can be displayed with imshow
and i have a row for every image in the dataset ¿someone can help me to understand why this happen?
Best regards