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 root_dir: src/mmp_extractor
dependencies: dependencies:
- common - common
- pvt.cppan.demo.intel.opencv.highgui: "*"
model: model:
type: lib type: lib

View file

@ -79,7 +79,7 @@ public:
auto end() const { return data.end(); } auto end() const { return data.end(); }
// left/right // left/right
mat mirror() mat mirror() const
{ {
int cols = width; int cols = width;
int rows = height; int rows = height;
@ -97,7 +97,7 @@ public:
} }
// up/down // up/down
mat flip() mat flip() const
{ {
int cols = width; int cols = width;
int rows = height; int rows = height;
@ -114,10 +114,40 @@ public:
return m; return m;
} }
#ifdef HAVE_OPENCV_IMGCODECS
operator cv::Mat() const
{
return flip().toCvMat();
}
#endif
private: private:
std::vector<T> data; std::vector<T> data;
int width; int width;
int height; 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) 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 #define NOMINMAX
#include <opencv2/highgui.hpp>
#include "mmp.h" #include "mmp.h"
#include <primitives/filesystem.h>
#include <algorithm> #include <algorithm>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <iostream>
#include <sstream> #include <sstream>
#include <primitives/filesystem.h>
void water_segment::load(const buffer &b) void water_segment::load(const buffer &b)
{ {
wg.load(b); wg.load(b);
@ -355,9 +357,10 @@ void mmp::writeColorMap()
void mmp::writeSplitColormap() const void mmp::writeSplitColormap() const
{ {
std::unordered_set<uint32_t> colors; std::set<uint32_t> colors;
for (auto &pixel : colormap) for (auto &pixel : colormap)
colors.insert(pixel); colors.insert(pixel);
int i = 0;
for (auto &color : colors) for (auto &color : colors)
{ {
auto m = colormap; auto m = colormap;
@ -369,8 +372,9 @@ void mmp::writeSplitColormap() const
ss.fill('0'); ss.fill('0');
ss.width(8); ss.width(8);
ss << std::hex << std::uppercase << color; ss << std::hex << std::uppercase << color;
fn += ".colormap." + ss.str() + ".bmp"; fn += ".colormap." + ss.str() + ".png";
write_mat_bmp(fn, m); 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <opencv2/highgui.hpp>
#include "mmp.h" #include "mmp.h"
#include <primitives/filesystem.h> #include <primitives/filesystem.h>