From 39775b5e982d239f99c2e6f904965d405083caf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Fri, 22 Jul 2022 11:36:10 +0200 Subject: [PATCH] Scale float images to 0,1 range on load --- rawimage.cpp | 16 ++++++++++++++++ rawimage.h | 1 + 2 files changed, 17 insertions(+) diff --git a/rawimage.cpp b/rawimage.cpp index 1413835..3aa0292 100644 --- a/rawimage.cpp +++ b/rawimage.cpp @@ -67,6 +67,7 @@ RawImage::RawImage(cv::Mat &img) { m_img = img; m_stats = false; + scaleToUnit(); } RawImage::RawImage(const RawImage &d) @@ -378,3 +379,18 @@ bool RawImage::pixel(int x, int y, QVector3D &rgb) const } return true; } + +void RawImage::scaleToUnit() +{ + if(CV_MAT_DEPTH(m_img.type()) == CV_32F) + { + double min, max; + cv::minMaxIdx(m_img, &min, &max); + if(min < 0 || max > 1) + { + float scale = 1.0 / (max - min); + float zero = min * scale; + m_img = m_img * scale - zero; + } + } +} diff --git a/rawimage.h b/rawimage.h index aece193..0f58a71 100644 --- a/rawimage.h +++ b/rawimage.h @@ -84,6 +84,7 @@ public: float thumbAspect() const; const cv::Mat& mat() const; bool pixel(int x, int y, QVector3D &rgb) const; + void scaleToUnit(); }; #endif // RAWIMAGE_H