MauiKit Image Tools

convertimage.hpp
1/*****************************************************************************
2 * convertimage.hpp
3 *
4 * Created: 5/28/2020 2020 by mguludag
5 *
6 * Copyright 2020 mguludag. All rights reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *****************************************************************************/
27#pragma once
28#include <opencv2/core.hpp>
29#include <opencv2/imgcodecs.hpp>
30#include <opencv2/imgproc.hpp>
31#include <QImage>
32#include <QObject>
33#include "opencvlib_export.h"
34#include <QDebug>
35
36inline unsigned char float2uchar_bounded(const float &in)
37{
38 return (in < 0.0f) ? 0 : ((in > 255.0f) ? 255 : static_cast<unsigned char>(in));
39}
40
41class OPENCVLIB_EXPORT ConvertImage
42{
43public:
44 ConvertImage();
45 static cv::Mat qimageToMatRef(QImage &img, int format)
46 {
47 return cv::Mat(img.height(),
48 img.width(),
49 format,
50 img.bits(),
51 static_cast<size_t>(img.bytesPerLine()));
52 }
53 static cv::Mat qimageToMat(QImage img, int format)
54 {
55 return cv::Mat(img.height(),
56 img.width(),
57 format,
58 img.bits(),
59 static_cast<size_t>(img.bytesPerLine()));
60 }
61 static QImage matToQimageRef(cv::Mat &mat, QImage::Format format)
62 {
63 // return QImage((uchar*) mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format).rgbSwapped();
64 return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format);
65 }
66 static QImage matToQimage(cv::Mat mat, QImage::Format format)
67 {
68
69 return QImage((uchar*) mat.data, mat.cols, mat.rows,static_cast<int>(mat.step), format);
70
71 // return QImage(mat.data, mat.cols, mat.rows, static_cast<int>(mat.step), format);
72 }
73
74 static cv::Mat readImage(const QString &fileUrl)
75 {
76 cv::Mat res2;
77 cv::Mat res = cv::imread(fileUrl.toStdString(), 0);
78 res.convertTo(res,CV_32FC1,1.0/255.0);
79 // res.cvtColor(res, res, cv::COLOR_BGR2GRAY);
80 // cv::threshold(res, res2,127,255, cv::THRESH_BINARY|cv::THRESH_OTSU);
81 return res;
82 }
83
84 static cv::Mat QImageToMat(QImage & image)
85 {
86 cv::Mat out;
87 switch(image.format()) {
89 {
90 cv::Mat empty;
91 empty.copyTo(out);
92 break;
93 }
95 {
96 qDebug() << "Image format is rgb32" << image.format();
97 cv::Mat view(image.height(),
98 image.width(),
99 CV_8UC4,
100 image.bits(),
101 static_cast<size_t>(image.bytesPerLine()));
102 view.copyTo(out);
103 break;
104 }
106 {
107 qDebug() << "Image format is rgb32" << image.format();
108
109 cv::Mat view(image.height(),image.width(),CV_8UC3,(void *)image.constBits(),image.bytesPerLine());
110 cv::cvtColor(view, out, cv::COLOR_RGB2BGR);
111 break;
112 }
113 default:
114 {
115 qDebug() << "Image format is rgb32" << image.format();
116
117 QImage conv = image.convertToFormat(QImage::Format_ARGB32);
118 cv::Mat view(conv.height(),conv.width(),CV_8UC4,(void *)conv.constBits(),conv.bytesPerLine());
119 view.copyTo(out);
120 break;
121 }
122 }
123 return out;
124 }
125};
uchar * bits()
qsizetype bytesPerLine() const const
const uchar * constBits() const const
QImage convertToFormat(Format format, Qt::ImageConversionFlags flags) &&
Format format() const const
int height() const const
int width() const const
std::string toStdString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:57:09 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.