OpenCV and Unsharp Masking Like Adobe Photoshop

前端 未结 3 1986
挽巷
挽巷 2021-02-06 19:11

I am trying to implement unsharp masking like it\'s done in Adobe Photoshop. I gathered a lot of information on the interent but I\'m not sure if I\'m missing something. Here\'s

3条回答
  •  名媛妹妹
    2021-02-06 19:28

    Here's the code what I have done. I am using this code to implement Unsharp Mask and it is working well for me. Hope it is useful for you.

    void USM(cv::Mat &O, int d, int amp, int threshold)
    {
        cv::Mat GB;
        cv::Mat O_GB;
        cv::subtract(O, GB, O_GB);
    
        cv::Mat invGB = cv::Scalar(255) - GB;
    
        cv::add(O, invGB, invGB);
    
        invGB = cv::Scalar(255) - invGB;
    
        for (int i = 0; i < O.rows; i++)
        {
            for (int j = 0; j < O.cols; j++)
            {
                unsigned char o_rgb = O.at(i, j);
                unsigned char d_rgb = O_GB.at(i, j);
                unsigned char inv_rgb = invGB.at(i, j);
    
                int newVal = o_rgb;
                if (d_rgb >= threshold)
                {
                    newVal = o_rgb + (d_rgb - inv_rgb) * amp;
                    if (newVal < 0)      newVal = 0;
                    if (newVal > 255)    newVal = 255;
                }
    
                O.at(i, j) = unsigned char(newVal);
            }
        }
    
    }
    

提交回复
热议问题