删除图像周围的黑色边框

时间:2023-02-10 00:17:55

I have a few JPG Images. Some of them may have a black border on one or more sides, and I'd like to remove them. The black border may not go around the actual image - some may only have the border at the bottom (with the actual image at the top), while some could be centered (which means black borders on two sides, but not connected). Worse, the images are JPG Compressed, so they may not be exactly 0,0,0 black anymore.

我有一些JPG图片。它们中的一些可能有一个或多个边的黑色边界,我想删除它们。黑边可能不会绕过实际的图像,有些可能只有底部的边界(在顶部有实际的图像),而有些可能是居中(这意味着两边的黑色边界,但没有连接)。更糟糕的是,这些图片被压缩了,所以它们可能不再是0、0、0。

In a Paint Program, I would "simply" use the Magic Wand tool with a low tolerance, but I need to do it in C# on ASP.net, and I don't know what the best way of doing this is.

在一个绘图程序中,我会“简单地”使用一个低容错的魔棒工具,但是我需要在ASP.net上使用c#,我不知道最好的方法是什么。

Should I "scan" each line and then each column (two nested for-loops) to find black areas? Sounds somewhat stupid to do, performance and CPU-Load-wise. Or does GDI+ have some magic wand tool build in already?

我应该“扫描”每一行,然后每一列(两个嵌套的for循环)以找到黑色区域吗?听起来有点愚蠢,性能和cpu负载方面。或者GDI+是否已经有了一些魔棒工具?

The images are not that big (474x474 pixels maximum) and cached afterwards, but I need to keep the server load as low as possible.

图像不是很大(最多474x474像素),之后会缓存,但是我需要尽可能地降低服务器的负载。

Any hints what the least stupid way of doing it would be?

有什么不愚蠢的做法吗?

1 个解决方案

#1


4  

It seems like for each edge you could do something like this:

似乎对于每条边你都可以这样做:

for each edge:
    for (i = 0; ; i++) {
         compute average pixel value along edge row/column + i
         if (average value > threshold)
              break;
    }
    crop image

#1


4  

It seems like for each edge you could do something like this:

似乎对于每条边你都可以这样做:

for each edge:
    for (i = 0; ; i++) {
         compute average pixel value along edge row/column + i
         if (average value > threshold)
              break;
    }
    crop image