0

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++!

1 Answer 1

2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.