libdart_openmvg/src/frame.cxx

128 lines
3.1 KiB
C++

#include "frame.h"
#include <iostream>
FILE *make_buffer(const uint8_t *data, const size_t data_len)
{
FILE *file = fmemopen((void *)data, data_len, "r+");
if (file == NULL)
{
perror("Error opening file");
return NULL;
}
// Seek to the beginning of the file
rewind(file);
// Write the data to the file
fwrite(data, sizeof(uint8_t), data_len, file);
return file;
}
int read_buffer(FILE *file, uint8_t **buffer, size_t *length)
{
if (file != NULL)
{
// Get the file size
fseek(file, 0, SEEK_END);
(*length) = ftell(file);
fseek(file, 0, SEEK_SET);
// Allocate memory
*buffer = (uint8_t *)malloc((*length) * sizeof(char));
if (buffer == NULL)
{
printf("Error: Unable to allocate memory.\n");
return 1;
}
// Read file
size_t newLen = fread(*buffer, sizeof(char), *length, file);
if (newLen == 0)
{
if (feof(file))
{
printf("Error: End of file reached.\n");
return 2;
}
printf("Error: Reading file.\n");
return 3;
}
return 4;
}
return 5;
}
Frame *new_frame_from_data(const uint8_t *data, const size_t data_len, int w, int h, int depth)
{
FILE *buf = make_buffer(data, data_len);
return new_frame_from_handle(buf, w, h, depth);
}
Frame *new_frame_from_handle(FILE *stream, int w, int h, int depth)
{
Frame *f = (Frame *)malloc(sizeof(Frame));
f->stream = stream;
f->w = w;
f->h = h;
f->depth = depth;
return f;
}
#ifdef __cplusplus
openMVG::image::Image<u_char> DartOpenMvg::imageFromFrame(const CFrame *frame)
{
rewind(frame->stream);
std::vector<unsigned char> imageData;
if (!openMVG::image::ReadPngStream(frame->stream, &imageData, (int *)&(frame->w), (int *)&(frame->h), (int *)&(frame->depth)))
{
throw std::runtime_error("Could not read stream");
};
int w = frame->w, h = frame->h;
Eigen::Matrix<u_char, Eigen::Dynamic, Eigen::Dynamic, 1> eigenMatrix(frame->w, frame->h);
for (int i = 0; i < w * h; ++i)
{
eigenMatrix(i / h, i % h) = imageData[i];
}
return openMVG::image::Image<uint8_t>(eigenMatrix);
}
void DartOpenMvg::Frame::calculateFeatures()
{
using namespace openMVG::image;
using namespace openMVG::features;
auto desc = SIFT_Anatomy_Image_describer(SIFT_Anatomy_Image_describer::Params());
auto image = imageFromFrame(cFrame);
mRegions = desc.Describe(image);
}
openMVG::image::Image<u_char> DartOpenMvg::Frame::getMvgImage() {
return imageFromFrame(this->cFrame);
}
void DartOpenMvg::Frames::computeMatches()
{
using namespace openMVG::matching;
using namespace openMVG::matching_image_collection;
using namespace openMVG::sfm;
using namespace openMVG::image;
Matcher *matcher = nullptr;
Regions_Provider provider = Regions_Provider();
matcher = new Matcher_Regions(0.8f, CASCADE_HASHING_L2);
openMVG::Pair_Set pairs = openMVG::Pair_Set();
Image im1 = this->at(0).getMvgImage();
Image im2 = this->at(1).getMvgImage();
auto pair = openMVG::Pair(0, 1);
pairs.insert(pair);
PairWiseMatches map_punitive_matches;
// matcher->Match(provider, pairs, map_punitive_matches, NULL);
#endif // __cplusplus