Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++/GIMP/OpenCV
#1
Using the following mask for bunch of images


filters > enhance > unsharp mask with values of

radius = 6.8, amount = 2.69, threshold = 0

Want to make the same with Open CV functions
Please advise how to proceed

Thanks in advance Smile
Reply
#2
  • Filters > Enhance > Unsharp mask is a "classic" Gimp 2.8 plugin.
  • `Filters enhance > Sharpen (unsharp mask) is the new GEGL-based version
They have the same parameters so which one are we talking about?

The GEGL one uses GEGL-isms and is probably not easy to convert to C++. The "classic" version uses convolution kernels so once you know how to convert the inputs to the adequate kernels you should be in business.

A completely different option is to try with sharpening options in OpenCV and see which one gives equivalent results (there is an unsharpMask operation in OpenCV).
Reply
#3
(08-01-2023, 07:46 AM)Ofnuts Wrote:
  • Filters > Enhance > Unsharp mask is a "classic" Gimp 2.8 plugin.
  • `Filters enhance > Sharpen (unsharp mask) is the new GEGL-based version
They have the same parameters so which one are we talking about?

The GEGL one uses GEGL-isms and is probably not easy to convert to C++. The "classic" version uses convolution kernels so once you know how to convert the inputs to the adequate kernels you should be in business.

A completely different option is to try with sharpening options in OpenCV and see which one gives equivalent results (there is an unsharpMask operation in OpenCV).

Can you please share unsharpMask with OpenCV sample?
Reply
#4
(08-01-2023, 07:48 PM)PowerThreads Wrote:
(08-01-2023, 07:46 AM)Ofnuts Wrote:
  • Filters > Enhance > Unsharp mask is a "classic" Gimp 2.8 plugin.
  • `Filters enhance > Sharpen (unsharp mask) is the new GEGL-based version
They have the same parameters so which one are we talking about?

The GEGL one uses GEGL-isms and is probably not easy to convert to C++. The "classic" version uses convolution kernels so once you know how to convert the inputs to the adequate kernels you should be in business.

A completely different option is to try with sharpening options in OpenCV and see which one gives equivalent results (there is an unsharpMask operation in OpenCV).

Can you please share unsharpMask with OpenCV sample?

https://stackoverflow.com/questions/6870...ith-opencv
Reply
#5
Can you please clarify which chunk stands for UnsharpMask?
Using peace of code below, but it's not the same even close
Code:
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <cstring>

cv::Mat not_so_smart_sharpen(
        const cv::Mat& bgr,
        double sigma,
        double amount,
        double canny_low_threshold_weight,
        double canny_high_threshold_weight,
        int edge_weight)
{
    cv::Mat enhanced_bgr, lab, enhanced_lab, channel[3], blurred, difference, bw, kernel, edges;

    // convert to Lab
    cv::cvtColor(bgr, lab, cv::ColorConversionCodes::COLOR_BGR2Lab);
    // perform the enhancement on the brightness component
    cv::split(lab, channel);
    cv::Mat& brightness = channel[0];
    // smoothing for unsharp masking
    cv::GaussianBlur(brightness, blurred, cv::Size(0, 0), sigma);
    difference = brightness - blurred;
    // calculate an edge map. I'll use Otsu threshold as the basis
    double thresh = cv::threshold(brightness, bw, 0, 255, cv::ThresholdTypes::THRESH_BINARY | cv::ThresholdTypes::THRESH_OTSU);
    cv::Canny(brightness, edges, thresh * canny_low_threshold_weight, thresh * canny_high_threshold_weight);
    // control edge thickness. use edge_weight=0 to use Canny edges unaltered
    cv::dilate(edges, edges, kernel, cv::Point(-1, -1), edge_weight);
    // unsharp masking on the edges
    cv::add(brightness, difference * amount, brightness, edges);
    // use the enhanced brightness channel
    cv::merge(channel, 3, enhanced_lab);
    // convert to BGR
    cv::cvtColor(enhanced_lab, enhanced_bgr, cv::ColorConversionCodes::COLOR_Lab2BGR);

//  cv::imshow("edges", edges);
//  cv::imshow("difference", difference * amount);
//  cv::imshow("original", bgr);
//  cv::imshow("enhanced", enhanced_bgr);
//  cv::waitKey(0);

    return enhanced_bgr;
}

int main(int argc, char *argv[])
{
    double sigma = std::stod(argv[1]);
    double amount = std::stod(argv[2]);
    double low = std::stod(argv[3]);
    double high = std::stod(argv[4]);
    int w = std::stoi(argv[5]);

    cv::Mat bgr = cv::imread("flower.jpg");

    cv::Mat enhanced = not_so_smart_sharpen(bgr, sigma, amount, low, high, w);

    cv::imshow("original", bgr);
    cv::imshow("enhanced", enhanced);
    cv::waitKey(0);

    return 0;
}

Other piece of code:
Code:
int main()
{
cv::Mat bgr = cv::imread("/home/supernova/Desktop/1tess2.jpg");
cv::Mat image;
cv::GaussianBlur(bgr, image, cv::Size(0, 0), 3);
cv::imshow("GaussianBlur", image);
cv::waitKey(100);
cv::addWeighted(bgr, 1.2, image, 0, 0.2, image);
cv::imshow("enhanced", image);
cv::waitKey(0);
cv::imwrite("/home/supernova/Desktop/1tessNew.jpg", image);
}

it does something, which is not relevant to what unsharpMask does, maybe I need to set some specific values?
if so, please advise what values I need to set
Reply
#6
Maybe there is a way to make command line function call with gimp?
something like gimp img.jpg unsharp 6.8, 2.69, 0
Reply


Forum Jump: