Neuro/gui/NeuroUI/trainingdataloader.cpp

66 lines
1.6 KiB
C++
Raw Normal View History

#include "trainingdataloader.h"
2015-10-27 14:33:54 +00:00
#include <sstream>
#include <QImage>
#include <QColor>
TrainingDataLoader::TrainingDataLoader()
{
}
void TrainingDataLoader::addSamples(const QString &sourceFile, TrainingDataLoader::SampleId sampleId)
{
QImage sourceImage;
2015-10-27 14:33:54 +00:00
if (sourceImage.load(sourceFile) == false)
{
std::ostringstream errorString;
errorString << "error loading " << sourceFile.toStdString();
throw std::runtime_error(errorString.str());
}
2015-10-27 14:33:54 +00:00
QSize scanWindow(32, 32);
QPoint scanPosition(0, 0);
2015-10-27 14:33:54 +00:00
while (scanPosition.y() + scanWindow.height() < sourceImage.height())
{
2015-10-27 14:33:54 +00:00
scanPosition.setX(0);
while (scanPosition.x() + scanWindow.width() < sourceImage.width())
{
2015-10-27 14:33:54 +00:00
Sample sample;
sample.first = sampleId;
for (int y = 0; y < scanWindow.height(); ++y)
{
for (int x = 0; x < scanWindow.width(); ++x)
{
QRgb color = sourceImage.pixel(scanPosition.x() + x, scanPosition.y() + y);
sample.second[x + y * scanWindow.height()] = qGray(color) / 255.0;
}
}
m_samples.push_back(sample);
scanPosition.rx() += scanWindow.width();
}
2015-10-27 14:33:54 +00:00
scanPosition.ry() += scanWindow.height();
}
2015-10-27 14:33:54 +00:00
}
2015-10-27 14:33:54 +00:00
const TrainingDataLoader::Sample &TrainingDataLoader::getRandomSample() const
{
size_t sampleIndex = (std::rand() * m_samples.size()) / RAND_MAX;
auto it = m_samples.cbegin();
for (size_t index = 0; index < sampleIndex; ++index)
{
it++;
}
return *it;
}