I am a beginner in c++ (mainly worked with Python) and I do not yet know how to properly do things. I want to process some color images as signals over time and, in order to do that, I want them to be in a double matrix.
A grayscale image would be 1d vector, from top left corner to bottom right, the color image would be a 2d vector, the second dimension being the 3 colors. That is, I want to flatten the image to a long vector, which would contain size 3 vectors with the rgb information.
I open the image using dlib like so:
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
using namespace dlib;
array2d<rgb_pixel> img;
load_image(img, image_name);
Which gives me a dlib array2d containing pixel structs. Now, I want to change that to a flattened image. I figured that, since the images dimensions might change, I would use a
std::vector<std::vector<double>>
as my matrix.
The naive way to convert it would be the following:
#include <vector>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
std::vector<std::vector<double>> image_to_frame(array2d<rgb_pixel> const &image)
{
const int total_num_of_px = image.nc() * image.nr();
std::vector<std::vector<double>> frame = std::vector<std::vector<double>>(total_num_of_px);
for (int i = 0; i < image.nr(); i++)
{
for (int j = 0; j < image.nc(); j++)
{
frame[(i+1)*j] = std::vector<double>(3);
frame[(i + 1)*j][0] = (double)image[i][j].red;
frame[(i + 1)*j][1] = (double)image[i][j].green;
frame[(i + 1)*j][2] = (double)image[i][j].blue;
}
}
return frame;
}
But this takes 8 seconds for an 1280x720 image. Which seems to me to be a bit long. Is there a better way to do this? A more efficient way of converting the array2d to vector matrix?
Or is there a more efficient data structure than the vector matrix? Or should I not be using dlib and open the image in another way to be easier to convert?
In Python I can open the image directly as a numpy array then do a reshape, which is very fast. Is there some equivalent to this in c++ that I am not aware of?
vectorwhat you cannot achieve witharray2d<>? - Jodocus