C# 如何在emgucv c上实现反锐化掩蔽#

C# 如何在emgucv c上实现反锐化掩蔽#,c#,opencv,emgucv,C#,Opencv,Emgucv,我正在尝试使用c#在emgucv上实现反锐化掩蔽方法 我现在拥有的python代码是(): def unsharp\u掩码(图像,内核大小=(5,5),sigma=1.0,amount=1.0,threshold=0): “”“使用非锐化遮罩返回图像的锐化版本。”“” #有关取消锐化遮罩的详细信息,请参阅: # https://en.wikipedia.org/wiki/Unsharp_masking # https://homepages.inf.ed.ac.uk/rbf/HIPR2/unsh

我正在尝试使用c#在emgucv上实现反锐化掩蔽方法

我现在拥有的python代码是():

def unsharp\u掩码(图像,内核大小=(5,5),sigma=1.0,amount=1.0,threshold=0):
“”“使用非锐化遮罩返回图像的锐化版本。”“”
#有关取消锐化遮罩的详细信息,请参阅:
# https://en.wikipedia.org/wiki/Unsharp_masking
# https://homepages.inf.ed.ac.uk/rbf/HIPR2/unsharp.htm
模糊=cv.GaussianBlur(图像、核大小、西格玛)
锐化=浮动(数量+1)*图像-浮动(数量)*模糊
锐化=np.最大值(锐化,np.零(锐化形状))
锐化=np.最小值(锐化,255*np.个(锐化形状))
锐化=锐化的.round().aType(np.uint8)
如果阈值>0:
低对比度遮罩=np.绝对(图像模糊)<阈值
np.copyto(锐化,图像,其中=低对比度遮罩)
回程锐化
我现在使用的c#代码不能像上面的代码那样工作。有人知道如何使用c实现它吗

publicstaticvoidgetmat(图像srcimg,图像imgBlurred,参考数据表dst,int-nAmount=200)
{
浮动量=nAmount/100f;
使用(图像dst_temp=新图像(srcimg.Width,srcimg.Height))
{
对于(int v=0;v255)c=255;
温度数据[v,u,0]=(字节)c;
}
}
dst=dst_温度材料克隆();
}
}
公共静态图像(Mat src,ref Mat dst,int-nAmount=200,双西格玛=3,int-threshold=0)
{
浮动金额=纳蒙特/100.0F;
使用(Mat imgBlurred=新Mat())
{
高斯模糊(src,imgBlurred,新系统。图纸。尺寸(0,0),西格玛,西格玛);
使用(Mat mask_temp=new Mat())
{
CvInvoke.AbsDiff(src、imgBlurred、mask_temp);
使用(Mat LowControlMask=新Mat())
{
CvInvoke.Threshold(掩码温度,低对比度掩码,阈值,255,阈值类型.binarynv);
GetMat(src.ToImage(),imgBlurred.ToImage(),ref dst);
src.CopyTo(dst,低对比度掩码);
}
}
}
}
有一个python解决方案

以下是C#中的工作:

有一个python解决方案

以下是C#中的工作:

def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0):
    """Return a sharpened version of the image, using an unsharp mask."""
    # For details on unsharp masking, see:
    # https://en.wikipedia.org/wiki/Unsharp_masking
    # https://homepages.inf.ed.ac.uk/rbf/HIPR2/unsharp.htm
    blurred = cv.GaussianBlur(image, kernel_size, sigma)
    sharpened = float(amount + 1) * image - float(amount) * blurred
    sharpened = np.maximum(sharpened, np.zeros(sharpened.shape))
    sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape))
    sharpened = sharpened.round().astype(np.uint8)
    if threshold > 0:
        low_contrast_mask = np.absolute(image - blurred) < threshold
        np.copyto(sharpened, image, where=low_contrast_mask)
    return sharpened
public static void GetMat(Image<Gray, byte> srcimg, Image<Gray, byte> imgBlurred, ref Mat dst, int nAmount = 200)
{
    float amount = nAmount / 100f;
    using (Image<Gray, byte> dst_temp = new Image<Gray, byte>(srcimg.Width, srcimg.Height))
    {
        for (int v = 0; v < srcimg.Height; v++)
        {
            for (int u = 0; u < srcimg.Width; u++)
            {
                byte a = srcimg.Data[v, u, 0]; //Get Pixel Color | fast way
                byte b = imgBlurred.Data[v, u, 0];
                int c = (int)(a * (1 + amount) - (amount * b));
                if (c < 0) c = 0;
                if (c > 255) c = 255;
                dst_temp.Data[v, u, 0] = (byte)c;
            }
        }
        dst = dst_temp.Mat.Clone();
    }
}

public static void getSharpenImage(Mat src, ref Mat dst, int nAmount = 200, double sigma = 3, int threshold = 0)
{
    float amount = nAmount / 100.0F;
    using (Mat imgBlurred = new Mat())
    {
        CvInvoke.GaussianBlur(src, imgBlurred, new System.Drawing.Size(0, 0), sigma, sigma);
        using (Mat mask_temp = new Mat())
        {
            CvInvoke.AbsDiff(src, imgBlurred, mask_temp);
            using (Mat lowcontrastmask = new Mat())
            {
                CvInvoke.Threshold(mask_temp, lowcontrastmask, threshold, 255, ThresholdType.BinaryInv);
                GetMat(src.ToImage<Gray, byte>(), imgBlurred.ToImage<Gray, byte>(), ref dst);
                src.CopyTo(dst, lowcontrastmask);
            }
        }
    }
}
Mat blurredImage = new Mat();
Mat lapImage = new Mat();
CvInvoke.MedianBlur(grayImage, blurredImage, 1);
CvInvoke.Laplacian(blurredImage, lapImage, blurredImage.Depth);
blurredImage -= (0.9*lapImage);