0

我将图像大小减小到 8*8 以计算平均哈希值,以使用 C# 查找相似图像。我打算使用 Lanczos 算法来减小图像大小,因为它似乎给出了很好的结果(从互联网上读取,python 图像哈希算法也使用相同的方法)。你能指出我在哪里可以找到用 C# 实现的 Lanczos 算法吗?还有比 Lanczos 更好的方法。请在这里帮忙。

谢谢

4

2 回答 2

1

不知道算法,但是调整图像大小的方法相当简单:

    public static Bitmap ResizeImage(Image image, Int32 width, Int32 height)
    {
        Bitmap destImage = new Bitmap(width, height);
        using (Graphics graphics = Graphics.FromImage(destImage))
            graphics.DrawImage(image, new Rectangle(0, 0, width, height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
        return destImage;
    }

有了这个,你可以加载原始图像,调整它的大小,并将调整大小的图像保存到磁盘:

public void ResizeImageFromPath(String imagePath, Int32 width, Int32 height, String savePath)
{
    if (savePath == null)
        savePath = imagePath;
    Byte[] bytes = File.ReadAllBytes(imagePath);
    using (MemoryStream stream = new MemoryStream(bytes))
    using (Bitmap image = new Bitmap(stream))
    using (Bitmap resized = ResizeImage(image, newwidth, newheight))
        resized.Save(savePath, ImageFormat.Png);
}
于 2018-04-25T13:45:54.277 回答
0
   using (Bitmap bitmap = (Bitmap)Image.FromFile("file.jpg"))
    {
        using (Bitmap newBitmap = new Bitmap(bitmap))
        {
            newBitmap.SetResolution(8, 8);
            newBitmap.Save("file_64.jpg", ImageFormat.Jpeg);
        }
    }

您可以更改-FunctionImageFormat中的Save以获得另一个压缩率。

于 2018-04-25T11:44:45.577 回答