简体   繁体   English

如何从图像加载特定的补丁/矩形?

[英]How to load a specific patch/rectangle from an image?

We have an application that show a large image file (satellite image) from local network resource. 我们有一个应用程序,可以显示来自本地网络资源的大图像文件(卫星图像)。 To speed up the image rendering, we divide the image to smaller patches (eg 6x6 cm) and the app tiles them appropriately. 为了加快图像渲染速度,我们将图像划分为较小的补丁(例如6x6厘米),然后应用将它们适当地平铺。 But each time the satellite image updated, the dividing pre-process should be done, which is a time consuming work. 但是,每次卫星图像更新时,都应进行分割预处理,这是一项耗时的工作。

I wonder how can we load the patches from the original file? 我想知道如何从原始文件加载补丁?

PS 1: I find the LeadTools library , but we need an open source solution. PS 1:我找到了LeadTools库 ,但是我们需要一个开源解决方案。

PS 2: The app is in .NET C# PS 2:该应用程序在.NET C#中

Edit 1: The format is not a point for us, but currently it's JPG. 编辑1:格式不是我们的重点,但目前是JPG。 changing the format to a another could be consider, but BMP format is hardly acceptable, because of it large volume. 可以考虑将格式更改为另一种格式,但是BMP格式由于其体积大而难以接受。

I'm not 100% sure what you're after but if you're looking for a way to go from string imagePath, Rectangle desiredPortion to a System.Drawing.Image object then perhaps something like this: 我不是100%知道要做什么,但是如果您正在寻找一种方法,可以从string imagePath, Rectangle desiredPortionSystem.Drawing.Image对象,那么可能是这样的:

public System.Drawing.Image LoadImagePiece(string imagePath, Rectangle desiredPortion)
{
   using (Image img = Image.FromFile(path))
   {
       Bitmap result = new Bitmap(desiredPortion.Width, desiredPortion.Height, PixelFormat.Format24bppRgb);
       using (Graphics g = Graphics.FromImage((Image)result))
       {
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.DrawImage(img, 0, 0, desiredPortion, GraphicsUnit.Pixel);
       }
       return result;
   }
}

Note that for performance reasons you may want to consider building multiple output images at once rather than calling this multiple times - perhaps passing it an array of rectangles and getting back an array of images or similar. 请注意,出于性能原因,您可能需要考虑一次构建多个输出图像,而不是多次调用-可能将其传递一个矩形数组并返回一个图像数组或类似图像。

If that's not what you're after can you clarify what you're actually looking for? 如果不是您所追求的,那么您可以弄清楚您实际上在寻找什么吗?

I wote a beautifull attempt of answer to your question, but my browser ate it... :( 我很想回答您的问题,但是我的浏览器却吃了它……:(

Basically what I tried to say was: 基本上我想说的是:

1.- Since Jpeg (and most compression formats) uses a secuential compression, you'll always need to decode all the bits that are before the ones that you need. 1.-由于Jpeg(和大多数压缩格式)使用保密压缩,因此,您始终需要解码所需比特之前的所有比特。
2.- The solution I propose need to be done with each format you need to support. 2.-我建议的解决方案需要使用您需要支持的每种格式来完成。
3.- There are a lot of open source jpeg decoders that you could modify. 3.-您可以修改很多开源的jpeg解码器。 Jpeg decoders need to decode blocks of bits (of variable size) that convert into pixel blocks of size 8x8. Jpeg解码器需要对(可变大小的)位块进行解码,然后将其转换为大小为8x8的像素块。 What you could do is modify the code to save in memory only the blocks you need and discard all the others as soon as they aren't needed any more (basically as soon as they are decoded). 您可以做的就是修改代码,以仅将所需的块保存在内存中,并在不再需要其他所有块时(基本上是在解码后立即丢弃)将其丢弃。 With those memory-saved blocks, create the image you need. 使用那些内存保存的块,创建所需的图像。
4.- Since Jpeg works with blocks of 8x8, your work could be easier if you work with patches of sizes multiples of 8 pixels. 4.-由于Jpeg可处理8x8的块,因此,如果您使用8像素倍数大小的补丁,则工作会更容易。
5.- The modification done to the jpeg decoder could be used to substitute the preprocessing of the images you are doing if you save the patch and discard the blocks as soon as you complete them. 5.-如果保存补丁并在完成块后立即丢弃它们,则可以使用对jpeg解码器所做的修改来替代对正在处理的图像的预处理。 It would be really fast and less memory consuming. 这将是真正的快速和更少的内存消耗。

I know it needs a lot of work and there are a lot of details to be taken in consideration (specially if you work with color images), but if you need performance I belive you will always end fighting or playing (as you want to see it) with the bytes. 我知道它需要大量的工作,并且要考虑很多细节(特别是如果您使用彩色图像),但是如果您需要性能,我相信您将永远结束战斗或玩游戏(如您所愿)它)与字节。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM