Save to png via opencv.

This commit is contained in:
lzwdgc 2018-08-02 14:14:38 +03:00
parent bdf5e9ff37
commit 9c3034e7ef
4 changed files with 43 additions and 7 deletions

View file

@ -91,6 +91,7 @@ projects:
root_dir: src/mmp_extractor
dependencies:
- common
- pvt.cppan.demo.intel.opencv.highgui: "*"
model:
type: lib

View file

@ -79,7 +79,7 @@ public:
auto end() const { return data.end(); }
// left/right
mat mirror()
mat mirror() const
{
int cols = width;
int rows = height;
@ -97,7 +97,7 @@ public:
}
// up/down
mat flip()
mat flip() const
{
int cols = width;
int rows = height;
@ -114,10 +114,40 @@ public:
return m;
}
#ifdef HAVE_OPENCV_IMGCODECS
operator cv::Mat() const
{
return flip().toCvMat();
}
#endif
private:
std::vector<T> data;
int width;
int height;
#ifdef HAVE_OPENCV_IMGCODECS
cv::Mat toCvMat() const
{
if constexpr (std::is_same_v<T, uint32_t>)
{
int cols = width;
int rows = height;
cv::Mat m(height, width, CV_8UC3);
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
auto &o = operator()(row * cols + col);
m.ptr<uint8_t>(row)[3 * col + 2] = (o >> 16) & 0xFF;
m.ptr<uint8_t>(row)[3 * col + 1] = (o >> 8) & 0xFF;
m.ptr<uint8_t>(row)[3 * col + 0] = (o >> 0) & 0xFF;
}
}
return m;
}
}
#endif
};
inline void write_mat_bmp(const path &filename, int width, int height, int bits, const uint8_t *b, size_t s)

View file

@ -18,15 +18,17 @@
#define NOMINMAX
#include <opencv2/highgui.hpp>
#include "mmp.h"
#include <primitives/filesystem.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <primitives/filesystem.h>
void water_segment::load(const buffer &b)
{
wg.load(b);
@ -355,9 +357,10 @@ void mmp::writeColorMap()
void mmp::writeSplitColormap() const
{
std::unordered_set<uint32_t> colors;
std::set<uint32_t> colors;
for (auto &pixel : colormap)
colors.insert(pixel);
int i = 0;
for (auto &color : colors)
{
auto m = colormap;
@ -369,8 +372,9 @@ void mmp::writeSplitColormap() const
ss.fill('0');
ss.width(8);
ss << std::hex << std::uppercase << color;
fn += ".colormap." + ss.str() + ".bmp";
write_mat_bmp(fn, m);
fn += ".colormap." + ss.str() + ".png";
std::cout << "\r[" << ++i << "/" << colors.size() << "] Processing color " << ss.str();
cv::imwrite(fn.u8string(), cv::Mat(m));
}
}

View file

@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <opencv2/highgui.hpp>
#include "mmp.h"
#include <primitives/filesystem.h>