OpenCV / C++ 中的优化帮助

发布于 2024-12-10 23:18:34 字数 1462 浏览 0 评论 0原文

我正在使用 OpenCV / Qt 创建一个简单的图像处理应用程序,并且正在寻找一种方法来优化我创建的代码(https://github.com/krslynx/ImageProcessingApplication)。目前,每次在应用程序中移动滑块时,都会调用相应的方法来更改图像,例如:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {
                          //setBrightness is the method called
    updateImage(ui->pixmap, setBrightness(this->image, ui->brightnessSlider->value());
}

updateImage 方法更新存储图像的 QLabel (ui->pixmap)。这会带来一个问题,因为 cv::Mat 图像 / this->image 是在每个 ActionEvent 中调用的原始图像,因此如果移动亮度滑块,则移动对比度滑块,由于滑块取决于 this->image 而不是设置的 QLabel 像素图,因此亮度变化不会持久。我通过使用以下代码“解决”了这个问题:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {

    cv::Mat result = this->image;

    result = setBrightness(result, ui->brightnessSlider->value());
    result = setContrast(result, ui->contrastSlider->value());
    result = setSharpness(result, ui->sharpnessSlider->value());
    result = setZoom(result, ui->zoomSlider->value());

    updateImage(ui->pixmap, result);
}

虽然此代码提供了我需要的确切视觉效果,但这意味着每个动作事件都需要在其中包含上述所有代码(并且我需要不断为每个新代码添加代码)我添加的功能),并且我正在重新处理我已经处理过的东西。当处理较大的图像时,所有滑块都已移动,有时可能需要 1.1 秒以上的时间来处理。

我尝试通过引用更改全局图像 this->image ,甚至为更改后的图像创建另一个全局图像,但是当我移动亮度/对比度/锐度/缩放滑块时遇到问题上/下,它的变化会太大。

任何指示将不胜感激!我对 C++ 相当陌生!

I am creating a simple image processing application using OpenCV / Qt and I am looking for a way to optimize the code that I have created (https://github.com/krslynx/ImageProcessingApplication). Currently, each time a slider is moved in the application the respective method is called which alters the image, for example:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {
                          //setBrightness is the method called
    updateImage(ui->pixmap, setBrightness(this->image, ui->brightnessSlider->value());
}

Where the updateImage method updates the QLabel (ui->pixmap) which stores the image. This presents a problem as the cv::Mat image / this->image is the raw image which is called in each ActionEvent thus if the Brightness Slider is moved, then the Contrast Slider is moved, the brightness changes won't be persisted due to the sliders depending on this->image rather than the set QLabel pixmap. I 'solved' this issue by using the following code:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {

    cv::Mat result = this->image;

    result = setBrightness(result, ui->brightnessSlider->value());
    result = setContrast(result, ui->contrastSlider->value());
    result = setSharpness(result, ui->sharpnessSlider->value());
    result = setZoom(result, ui->zoomSlider->value());

    updateImage(ui->pixmap, result);
}

While this code gives the exact visual effect I need, it means that each action event needs to have ALL of the above code within it (and I need to keep adding code for each new feature I add), and I am re-processing things that I've already processed. When dealing with a larger image, which has had all of the sliders moved it can sometimes take over 1.1 seconds to process.

I tried to instead change the global image this->image by reference, and even create another global for the altered image however I ran into problems where when I moved the brightness/contrast/sharpness/zoom slider up/down, it would change far too vigorously.

Any pointers would be greatly appreciated! I am fairly new to C++!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单身情人 2024-12-17 23:18:34

创建一个单独的函数,根据滑块的值更新图像。例如,有一个这样的函数,

void MainWiondow::generic_sliderMoved() {
    cv::Mat result = this->image;

    result = setBrightness(result, ui->brightnessSlider->value());
    result = setContrast(result, ui->contrastSlider->value());
    result = setSharpness(result, ui->sharpnessSlider->value());
    result = setZoom(result, ui->zoomSlider->value());

    updateImage(ui->pixmap, result);
}

您可以将每个滑块的动作事件设置为此函数。如果某些滑块需要更多功能,那么您可以创建一个不同的动作事件函数来调用该函数,如下所示:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {
    /* brightness slider specific code */

    generic_sliderMoved();
}

Make a separate function that updates the image based on the sliders' values. For example have a function like this

void MainWiondow::generic_sliderMoved() {
    cv::Mat result = this->image;

    result = setBrightness(result, ui->brightnessSlider->value());
    result = setContrast(result, ui->contrastSlider->value());
    result = setSharpness(result, ui->sharpnessSlider->value());
    result = setZoom(result, ui->zoomSlider->value());

    updateImage(ui->pixmap, result);
}

You could set each slider's action event to be this function. If some sliders need some more functionality beyond this then you can make a different action event function that calls that function like this:

/** ActionEvent for the brightness slider moving */
void MainWindow::on_brightnessSlider_sliderMoved() {
    /* brightness slider specific code */

    generic_sliderMoved();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文