Compare commits

...

4 Commits

Author SHA1 Message Date
61767661e6 Add QDebug 2023-06-26 22:48:45 +02:00
6cefc7bd18 change slider layout 2023-06-26 22:25:11 +02:00
96097b614d Use qt as a graphical display 2023-06-26 22:19:05 +02:00
dff0311eac Setup cmake and conan 2023-06-26 22:18:42 +02:00
4 changed files with 185 additions and 116 deletions

View File

@@ -1,19 +1,18 @@
cmake_minimum_required(VERSION 3.18) cmake_minimum_required(VERSION 3.18)
project(TV_Denoising_CUDA) project(TV_Denoising_CUDA)
# Set tool chain set(CMAKE_BUILD_TYPE "Release")
set(VCPKG_DIR "E:/programming/vcpkg")
set(VCPKG_INSTALLED_DIR "${VCPKG_DIR}/installed")
set(CMAKE_TOOLCHAIN_FILE "${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
# Find GTK3
find_package(PkgConfig REQUIRED) list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
# Find the Qt5 package
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# Find OpenCV # Find OpenCV
set(OpenCV_DIR "${VCPKG_INSTALLED_DIR}/x64-windows/share/opencv2") set(OpenCV_DIR "C:/Users/Culis/.conan2/p/b/openc720995090ff52/b/")
find_package(OpenCV REQUIRED) find_package(OpenCV REQUIRED)
# Find CUDA # Find CUDA
enable_language(CUDA) enable_language(CUDA)
@@ -29,12 +28,21 @@ set_source_files_properties(${CUDA_SOURCE_FILES} PROPERTIES CUDA_SOURCE_PROPERTY
file(GLOB CPP_SOURCE_FILES "src/*.cpp") file(GLOB CPP_SOURCE_FILES "src/*.cpp")
# Set the include directories # Set the include directories
include_directories(${OpenCV_DIR} ${CUDA_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS} "include") include_directories(${OpenCV_DIR} ${CUDA_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} "include")
# Create the executable # Create the executable
add_executable(TV_Denoising_CUDA ${CPP_SOURCE_FILES} ${CUDA_SOURCE_FILES}) add_executable(TV_Denoising_CUDA ${CPP_SOURCE_FILES} ${CUDA_SOURCE_FILES})
# Link CUDA libraries # Link CUDA libraries
target_link_libraries(TV_Denoising_CUDA ${CUDA_LIBRARIES} ${GTK3_LIBRARIES}) target_link_libraries(TV_Denoising_CUDA ${CUDA_LIBRARIES} ${OpenCV_LIBS} Qt5::Widgets)
target_compile_options(TV_Denoising_CUDA PUBLIC ${GTK3_CFLAGS_OTHER}) set_property(TARGET TV_Denoising_CUDA PROPERTY ENVIRONMENT "PATH=${CMAKE_CURRENT_BINARY_DIR}/Debug;${CMAKE_CURRENT_BINARY_DIR}/Release")
set_target_properties(TV_Denoising_CUDA PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" WIN32_EXECUTABLE TRUE)
# Copy necessary DLLs and runtime files to the release directory
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/"
FILES_MATCHING PATTERN "*.dll"
)

9
CMakeUserPresets.json Normal file
View File

@@ -0,0 +1,9 @@
{
"version": 4,
"vendor": {
"conan": {}
},
"include": [
"E:\\programming\\C++\\tv-image-denoising\\build\\CMakePresets.json"
]
}

11
conanfile.txt Normal file
View File

@@ -0,0 +1,11 @@
[requires]
opencv/4.5.5
qt/5.15.8
[options]
qt/5.15.8:qttools=True
qt/5.15.8:shared=True
[generators]
CMakeDeps
CMakeToolchain

View File

@@ -15,24 +15,35 @@ Description:
- <iterations> is the number of iterations for TV denoising (optional, default: 10). - <iterations> is the number of iterations for TV denoising (optional, default: 10).
*/ */
#include <iostream> #include <iostream>
#include <gtk/gtk.h> #include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QSlider>
#include <QFileDialog>
#include <QMessageBox>
#include <QImageReader>
#include <QImage>
#include <opencv2/opencv.hpp> #include <opencv2/opencv.hpp>
#include "tv_denoising.hpp" #include "tv_denoising.hpp"
// Global variables for GUI elements // Global variables for GUI elements
GtkLabel* originalImageLabel; QLabel *originalImageLabel;
GtkLabel* resultImageLabel; QLabel *resultImageLabel;
GtkEntry* inputImagePathEntry; QLineEdit *inputImagePathEdit;
GtkEntry* outputImagePathEntry; QLineEdit *outputImagePathEdit;
GtkScale* lambdaScale; QSlider *lambdaSlider;
GtkScale* iterationsScale; QSlider *iterationsSlider;
static void updateDenoisedImage(GtkWidget* widget, gpointer data) void updateDenoisedImage()
{ {
std::string inputImagePath = gtk_entry_get_text(inputImagePathEntry); std::string inputImagePath = inputImagePathEdit->text().toStdString();
std::string outputImagePath = gtk_entry_get_text(outputImagePathEntry); std::string outputImagePath = outputImagePathEdit->text().toStdString();
float lambda = gtk_range_get_value(GTK_RANGE(lambdaScale)) / 100.0; float lambda = lambdaSlider->value() / 100.0;
int iterations = gtk_range_get_value(GTK_RANGE(iterationsScale)); int iterations = iterationsSlider->value();
// Read the input image // Read the input image
cv::Mat image = cv::imread(inputImagePath, cv::IMREAD_GRAYSCALE); cv::Mat image = cv::imread(inputImagePath, cv::IMREAD_GRAYSCALE);
@@ -40,10 +51,7 @@ static void updateDenoisedImage(GtkWidget* widget, gpointer data)
// Check if the image was successfully loaded // Check if the image was successfully loaded
if (image.empty()) if (image.empty())
{ {
GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, QMessageBox::critical(nullptr, "Error", "Failed to read the input image.");
"Failed to read the input image.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return; return;
} }
@@ -51,126 +59,159 @@ static void updateDenoisedImage(GtkWidget* widget, gpointer data)
TVDenoising(image, lambda, iterations); TVDenoising(image, lambda, iterations);
// Display the denoised image // Display the denoised image
GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(image.data, GDK_COLORSPACE_RGB, FALSE, 8, image.cols, image.rows, QImage resultImage(image.data, image.cols, image.rows, QImage::Format_Grayscale8);
image.step, NULL, NULL); resultImageLabel->setPixmap(QPixmap::fromImage(resultImage));
gtk_image_set_from_pixbuf(GTK_IMAGE(resultImageLabel), pixbuf);
g_object_unref(pixbuf);
// Save the denoised image // Save the denoised image
cv::imwrite(outputImagePath, image); cv::imwrite(outputImagePath, image);
} }
static void openInputImage(GtkWidget* widget, gpointer data) void openInputImage()
{ {
GtkWidget* dialog = gtk_file_chooser_dialog_new("Select Input Image", GTK_WINDOW(data), GTK_FILE_CHOOSER_ACTION_OPEN, QString filePath = QFileDialog::getOpenFileName(nullptr, "Select Input Image");
"Cancel", GTK_RESPONSE_CANCEL, "Open", GTK_RESPONSE_ACCEPT, NULL); if (!filePath.isEmpty())
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) inputImagePathEdit->setText(filePath);
{
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gtk_entry_set_text(inputImagePathEntry, filename);
g_free(filename);
}
gtk_widget_destroy(dialog);
} }
static void openOutputImage(GtkWidget* widget, gpointer data) void openOutputImage()
{ {
GtkWidget* dialog = gtk_file_chooser_dialog_new("Select Output Image", GTK_WINDOW(data), GTK_FILE_CHOOSER_ACTION_SAVE, QString filePath = QFileDialog::getSaveFileName(nullptr, "Select Output Image");
"Cancel", GTK_RESPONSE_CANCEL, "Save", GTK_RESPONSE_ACCEPT, NULL); if (!filePath.isEmpty())
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) outputImagePathEdit->setText(filePath);
{
char* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gtk_entry_set_text(outputImagePathEntry, filename);
g_free(filename);
}
gtk_widget_destroy(dialog);
} }
int main(int argc, char** argv) int main(int argc, char **argv)
{ {
// Initialize GTK QApplication app(argc, argv);
gtk_init(&argc, &argv);
// Create the main window // Create the main window
GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL); QWidget window;
gtk_window_set_title(GTK_WINDOW(window), "TV Image Denoising"); window.setWindowTitle("TV Image Denoising");
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); window.resize(800, 600);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
// Create the main vertical box layout // Create the layout
GtkWidget* vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); QGridLayout *layout = new QGridLayout(&window);
gtk_container_add(GTK_CONTAINER(window), vbox);
// Create the original image label // Create the original image label
originalImageLabel = GTK_LABEL(gtk_label_new("Original Image")); QLabel *originalImageLabel = new QLabel("Original Image");
gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(originalImageLabel), FALSE, FALSE, 0); layout->addWidget(originalImageLabel, 0, 0, 1, 2); // Row 0, spanning 1 row, 2 columns
// Create the original image display widget
QLabel *originalImageDisplay = new QLabel();
originalImageDisplay->setAlignment(Qt::AlignCenter);
layout->addWidget(originalImageDisplay, 1, 0, 1, 2); // Row 1, spanning 1 row, 2 columns
// Create the result image label // Create the result image label
resultImageLabel = GTK_LABEL(gtk_label_new("Result Image")); QLabel *resultImageLabel = new QLabel("Result Image");
gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(resultImageLabel), FALSE, FALSE, 0); layout->addWidget(resultImageLabel, 0, 2, 1, 2); // Row 0, spanning 1 row, 2 columns
// Create the result image display widget
QLabel *resultImageDisplay = new QLabel();
resultImageDisplay->setAlignment(Qt::AlignCenter);
layout->addWidget(resultImageDisplay, 1, 2, 1, 2); // Row 1, spanning 1 row, 2 columns
// Create the input image path input field // Create the input image path input field
GtkWidget* inputImageHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); QHBoxLayout *inputLayout = new QHBoxLayout;
gtk_box_pack_start(GTK_BOX(vbox), inputImageHBox, FALSE, FALSE, 0); QLabel *inputLabel = new QLabel("Input Image:");
QLineEdit *inputImagePathEdit = new QLineEdit;
GtkWidget* inputImageLabel = gtk_label_new("Input Image:"); QPushButton *inputBrowseButton = new QPushButton("Browse");
gtk_box_pack_start(GTK_BOX(inputImageHBox), inputImageLabel, FALSE, FALSE, 0); QObject::connect(inputBrowseButton, &QPushButton::clicked, openInputImage);
inputLayout->addWidget(inputLabel);
inputImagePathEntry = GTK_ENTRY(gtk_entry_new()); inputLayout->addWidget(inputImagePathEdit);
gtk_box_pack_start(GTK_BOX(inputImageHBox), GTK_WIDGET(inputImagePathEntry), TRUE, TRUE, 0); inputLayout->addWidget(inputBrowseButton);
layout->addLayout(inputLayout, 2, 0, 1, 4); // Row 2, spanning 1 row, 4 columns
GtkWidget* inputBrowseButton = gtk_button_new_with_label("Browse");
g_signal_connect(inputBrowseButton, "clicked", G_CALLBACK(openInputImage), window);
gtk_box_pack_start(GTK_BOX(inputImageHBox), inputBrowseButton, FALSE, FALSE, 0);
// Create the output image path input field // Create the output image path input field
GtkWidget* outputImageHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); QHBoxLayout *outputLayout = new QHBoxLayout;
gtk_box_pack_start(GTK_BOX(vbox), outputImageHBox, FALSE, FALSE, 0); QLabel *outputLabel = new QLabel("Output Image:");
QLineEdit *outputImagePathEdit = new QLineEdit;
QPushButton *outputBrowseButton = new QPushButton("Browse");
QObject::connect(outputBrowseButton, &QPushButton::clicked, openOutputImage);
outputLayout->addWidget(outputLabel);
outputLayout->addWidget(outputImagePathEdit);
outputLayout->addWidget(outputBrowseButton);
layout->addLayout(outputLayout, 3, 0, 1, 4); // Row 3, spanning 1 row, 4 columns
GtkWidget* outputImageLabel = gtk_label_new("Output Image:"); // Create the lambda slider
gtk_box_pack_start(GTK_BOX(outputImageHBox), outputImageLabel, FALSE, FALSE, 0); QLabel *lambdaLabel = new QLabel("Lambda:");
QSlider *lambdaSlider = new QSlider(Qt::Horizontal);
lambdaSlider->setObjectName("LambdaSlider"); // Set object name for styling
lambdaSlider->setMinimum(0);
lambdaSlider->setMaximum(100);
outputImagePathEntry = GTK_ENTRY(gtk_entry_new()); // Create the iterations slider
gtk_box_pack_start(GTK_BOX(outputImageHBox), GTK_WIDGET(outputImagePathEntry), TRUE, TRUE, 0); QLabel *iterationsLabel = new QLabel("Iterations:");
QSlider *iterationsSlider = new QSlider(Qt::Horizontal);
iterationsSlider->setObjectName("IterationsSlider"); // Set object name for styling
iterationsSlider->setMinimum(1);
iterationsSlider->setMaximum(100);
GtkWidget* outputBrowseButton = gtk_button_new_with_label("Browse"); // Create labels to display slider values
g_signal_connect(outputBrowseButton, "clicked", G_CALLBACK(openOutputImage), window); QLabel *lambdaValueLabel = new QLabel();
gtk_box_pack_start(GTK_BOX(outputImageHBox), outputBrowseButton, FALSE, FALSE, 0); QLabel *iterationsValueLabel = new QLabel();
// Create the lambda scale // Set stylesheet for dark design
GtkWidget* lambdaHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5); QString styleSheet = "QLabel { color: white; }" // Set label text color to white
gtk_box_pack_start(GTK_BOX(vbox), lambdaHBox, FALSE, FALSE, 0); "QSlider::groove:horizontal { background-color: #555555; height: 6px; }" // Set groove color and height
"QSlider::handle:horizontal { background-color: #ffffff; width: 10px; margin: -6px 0; }"; // Set handle color and size
GtkWidget* lambdaLabel = gtk_label_new("Lambda:"); lambdaSlider->setStyleSheet(styleSheet);
gtk_box_pack_start(GTK_BOX(lambdaHBox), lambdaLabel, FALSE, FALSE, 0); iterationsSlider->setStyleSheet(styleSheet);
lambdaScale = GTK_SCALE(gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, 100, 1)); // Set size policies for sliders
gtk_scale_set_value_pos(lambdaScale, GTK_POS_RIGHT); lambdaSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
gtk_scale_set_digits(lambdaScale, 2); iterationsSlider->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
gtk_box_pack_start(GTK_BOX(lambdaHBox), GTK_WIDGET(lambdaScale), TRUE, TRUE, 0);
// Create the iterations scale
GtkWidget* iterationsHBox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_start(GTK_BOX(vbox), iterationsHBox, FALSE, FALSE, 0);
GtkWidget* iterationsLabel = gtk_label_new("Iterations:");
gtk_box_pack_start(GTK_BOX(iterationsHBox), iterationsLabel, FALSE, FALSE, 0);
iterationsScale = GTK_SCALE(gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 1, 100, 1));
gtk_scale_set_value_pos(iterationsScale, GTK_POS_RIGHT);
gtk_scale_set_digits(iterationsScale, 0);
gtk_box_pack_start(GTK_BOX(iterationsHBox), GTK_WIDGET(iterationsScale), TRUE, TRUE, 0);
// Create the denoise button // Create the denoise button
GtkWidget* denoiseButton = gtk_button_new_with_label("Denoise"); QPushButton *denoiseButton = new QPushButton("Denoise");
g_signal_connect(denoiseButton, "clicked", G_CALLBACK(updateDenoisedImage), NULL); QObject::connect(denoiseButton, &QPushButton::clicked, updateDenoisedImage);
gtk_box_pack_start(GTK_BOX(vbox), denoiseButton, FALSE, FALSE, 0);
// Show all widgets // Add the lambda slider and its label
gtk_widget_show_all(window); layout->addWidget(lambdaLabel, 4, 0); // Row 4, column 0
layout->addWidget(lambdaSlider, 4, 1); // Row 4, column 1
layout->addWidget(lambdaValueLabel, 5, 0); // Row 5, column 0
// Run the GTK main loop // Add the iterations slider and its label
gtk_main(); layout->addWidget(iterationsLabel, 4, 2); // Row 4, column 2
layout->addWidget(iterationsSlider, 4, 3); // Row 4, column 3
layout->addWidget(iterationsValueLabel, 5, 2); // Row 5, column 2
return 0; // Add the denoise button
layout->addWidget(denoiseButton, 6, 0, 1, 4); // Row 6, spanning 1 row, 4 columns
QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
qDebug() << "Supported image formats:" << supportedFormats;
// Load and display the input image
QString inputImagePath = inputImagePathEdit->text();
QImage inputImage(inputImagePath);
if (inputImage.isNull())
{
qDebug() << "Failed to load input image";
}
else
{
qDebug() << "Input image loaded successfully";
}
QPixmap inputPixmap = QPixmap::fromImage(inputImage);
originalImageDisplay->setPixmap(inputPixmap.scaled(originalImageDisplay->size(), Qt::KeepAspectRatio));
// Load and display the output image
QString outputImagePath = outputImagePathEdit->text();
QImage outputImage(outputImagePath);
if (outputImage.isNull())
{
qDebug() << "Failed to load input image";
}
else
{
qDebug() << "Input image loaded successfully";
}
QPixmap outputPixmap = QPixmap::fromImage(outputImage);
resultImageDisplay->setPixmap(outputPixmap.scaled(resultImageDisplay->size(), Qt::KeepAspectRatio));
// Show the main window
window.show();
return app.exec();
} }