我正在将图像大小减少到8*8,以使用C#计算平均哈希值来查找相似的图像。我计划使用Lanczos算法来减小图像大小,因为它看起来效果很好(从互联网上读取,也使用python图像哈希算法)。你能告诉我在哪里可以找到用C#实现的Lanczos算法吗?还有比Lanczos更好的办法吗?请帮帮忙。
谢谢
发布于 2018-04-25 21:45:54
对算法一无所知,但调整图像大小的方法相当简单:
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-25 19:44:46
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);
}
}您可以在Save-Function中更改ImageFormat以获得另一个压缩率。
https://stackoverflow.com/questions/50019978
复制相似问题