Add image processing using opencv

This commit is contained in:
Luis Ángel San Martín
2024-08-31 12:41:44 +02:00
parent 47c7c24e78
commit 57d39fabbf
6 changed files with 185 additions and 27 deletions

View File

@ -0,0 +1,33 @@
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
SOURCES += \
$$PWD/resize_image.cpp
HEADERS += \
$$PWD/resize_image.h
# include opencv, and link against it (core and imgproc)
# windows
win32 {
INCLUDEPATH += $$PWD/opencv/build/include
DEPENDPATH += $$PWD/opencv/build/include
}
# release
win32:CONFIG(release, debug|release) {
LIBS += -L$$PWD/opencv/build/x64/vc16/lib \
-lopencv_world490
}
# debug
win32:CONFIG(debug, debug|release) {
LIBS += -L$$PWD/opencv/build/x64/vc16/lib \
-lopencv_world490d
}
#win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../opencv/build/x64/vc15/lib/ -lopencv_world341
#else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../opencv/build/x64/vc15/lib/ -lopencv_world341d
#INCLUDEPATH += $$PWD/../../../../opencv/build/include
#DEPENDPATH += $$PWD/../../../../opencv/build/include

View File

@ -0,0 +1,75 @@
#include "resize_image.h"
// include opencv
#include <opencv2/opencv.hpp>
QPixmap scalePixmapBicubic(const QPixmap &pixmap, int width, int height);
QPixmap scalePixmapLanczos(const QPixmap &pixmap, int width, int height);
QPixmap scalePixmapArea(const QPixmap &pixmap, int width, int height);
QPixmap scalePixmapOpenCV(const QPixmap &pixmap, int width, int height, cv::InterpolationFlags flags);
QPixmap smartScalePixmap(const QPixmap &pixmap, int width, int height)
{
const int w = pixmap.width();
const int h = pixmap.height();
if ((w == width && h == height) || pixmap.isNull()) {
return pixmap;
}
if (w <= width && h <= height) { // upscaling
return scalePixmapLanczos(pixmap, width, height);
}
return pixmap;
}
QPixmap scalePixmap(const QPixmap &pixmap, int width, int height, ScaleMethod method)
{
const int w = pixmap.width();
const int h = pixmap.height();
if (w == width && h == height) {
return pixmap;
}
switch (method) {
case ScaleMethod::QtFast:
return pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::FastTransformation);
case ScaleMethod::QtSmooth:
return pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
case ScaleMethod::Bicubic:
return scalePixmapBicubic(pixmap, width, height);
case ScaleMethod::Lanczos:
return scalePixmapLanczos(pixmap, width, height);
case ScaleMethod::Area:
return scalePixmapArea(pixmap, width, height);
}
}
QPixmap scalePixmapBicubic(const QPixmap &pixmap, int width, int height)
{
return scalePixmapOpenCV(pixmap, width, height, cv::INTER_CUBIC);
}
QPixmap scalePixmapLanczos(const QPixmap &pixmap, int width, int height)
{
return scalePixmapOpenCV(pixmap, width, height, cv::INTER_LANCZOS4);
}
QPixmap scalePixmapArea(const QPixmap &pixmap, int width, int height)
{
return scalePixmapOpenCV(pixmap, width, height, cv::INTER_AREA);
}
QPixmap scalePixmapOpenCV(const QPixmap &pixmap, int width, int height, cv::InterpolationFlags flags)
{
QImage qimage = pixmap.toImage();
cv::Mat mat = cv::Mat(qimage.height(), qimage.width(), CV_8UC4, (void *)qimage.bits(), qimage.bytesPerLine());
cv::Mat resizedMat;
cv::resize(mat, resizedMat, cv::Size(width, height), 0, 0, flags);
QImage resizedQImage((uchar *)resizedMat.data, resizedMat.cols, resizedMat.rows, resizedMat.step, QImage::Format_ARGB32);
QPixmap resizedPixmap = QPixmap::fromImage(resizedQImage);
return resizedPixmap;
}

View File

@ -0,0 +1,20 @@
#ifndef RESIZE_IMAGE_H
#define RESIZE_IMAGE_H
#include <cmath>
#include <QPixmap>
#include <QImage>
enum class ScaleMethod {
QtFast,
QtSmooth, // Bilinear
Bicubic,
Lanczos,
Area
};
QPixmap smartScalePixmap(const QPixmap &pixmap, int width, int height);
QPixmap scalePixmap(const QPixmap &pixmap, int width, int height, ScaleMethod method = ScaleMethod::QtSmooth);
#endif // RESIZE_IMAGE_H