正在查看 1 个帖子:1-1 (共 1 个帖子)
- 作者帖子
千江月游客Dezoomify下载、拼合图像时,常常由于网络不稳定,导致下载的图像中有缺失的“瓦片”,表现为图像中随机位置存在黑色区块。
写了段代码,可以对任意一幅图像进行检查。
/// <summary> /// 图像残损检查:如果图像中任何一个256*256的区块是纯黑色,则返回false /// </summary> /// <param name="imagePath"></param> /// <returns></returns> public static bool IsBadImage(string imagePath) { try { using (Bitmap bitmap = new Bitmap(imagePath)) { int width = bitmap.Width; int height = bitmap.Height;
for (int x = 0; x < width; x += 256) { for (int y = 0; y < height; y += 256) { int blockWidth = Math.Min(256, width - x); int blockHeight = Math.Min(256, height - y); if (!IsBlockPureBlack(bitmap, x, y, blockWidth, blockHeight)) { return false; } } } } } catch (Exception ex) { Console.WriteLine($"Error processing image: {ex.Message}"); return false; } return true; } static bool IsBlockPureBlack(Bitmap bitmap, int startX, int startY, int blockWidth, int blockHeight) { for (int x = startX; x < startX + blockWidth && x < bitmap.Width; x++) { for (int y = startY; y < startY + blockHeight && y < bitmap.Height; y++) { Color pixelColor = bitmap.GetPixel(x, y); if (pixelColor.R != 0 || pixelColor.G != 0 || pixelColor.B != 0) { return false; } } } return true; }
调用IsBadImage(图像路径文本),如果返回false,则表明图像存在残损区块。
对编程不熟悉的朋友,借助DeepSeek等工具,可以较为容易地基于以上代码,封装、生成自己的检测工具。
抛转引玉,也欢迎编程大神们指导、完善。
- 作者帖子
正在查看 1 个帖子:1-1 (共 1 个帖子)
正在查看 1 个帖子:1-1 (共 1 个帖子)