图像压缩算法 - 按颜色将图像分解为正方形
我正在尝试开发一个移动应用程序,我想知道将图像转换为文本文件的最简单方法,然后能够稍后在内存中重新创建所述文本。相关图像将包含不超过 16 种左右的颜色,因此效果很好。
基本上,暴力破解这个解决方案需要我将每个人的像素颜色数据保存到一个文件中。但是,这会产生一个巨大的文件。 我知道有更好的方法 - 例如,如果图像的很大一部分由相同颜色组成,则将该区域分解为更小的正方形和矩形,并将它们的坐标和大小保存到文件中。
这里是一个例子。图像应该只是黑/白。大颜色框代表输出文本文件中的理论“数据点”。这些盒子会真实地说明它们的来源、尺寸以及它们应该是什么颜色。
例如,顶盒的原点为0,0,大小为359,48,它代表黑色。 保存在文本文件中,数据将为 0,0,359,48,0。
这会是什么样的算法?
注意:我使用的 SDK 无法从 X、Y 坐标返回像素的颜色。但是,我可以将外部信息从文本文件加载到程序中并以这种方式对其进行操作。我需要导出到文本文件的数据来自不同的实用程序,该实用程序能够从 X、Y 坐标获取像素的颜色。
编辑:添加了一张图片 EDIT2:添加约束
I'm trying to develop a mobile application, and I'm wondering the easiest way to convert an image into a text file, and then be able to recreate it later in memory said text. The image(s) in question will contain no more than 16 or so colors, so it would work out fine.
Basically, brute-forcing this solution would require me saving each individual's pixel color data into a file. However, this would result in a HUGE file. I know there's a better way - like, if there's a huge portion of the image that consists of the same color, breaking up the area into smaller squares and rectangles and saving their coordinates and size to file.
Here's an example. The image is supposed to be just black/white. The big color boxes represent theoretical 'data points' in the outputted text file. These boxes would really state their origin, size, and what color they should be.
E.g., top box has an origin of 0,0, a size of 359,48, and it represents the color black.
Saved in a text file, the data would be 0,0,359,48,0.
What kind of algorithm would this be?
NOTE: The SDK that I am using cannot return a pixel's color from an X,Y coordinate. However, I can load external information into the program from a text file and manipulate it that way. This data that I need to export to a text file will be from a different utility that will have the capability to get a pixel's color from X,Y coordinates.
EDIT: Added a picture
EDIT2: Added constraints
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否详细说明为什么要将图像(或其部分)保存为纯文本?不能用二进制表示代替吗?此外,如果图像通常具有大量连续的相同颜色的像素,您可能需要使用所谓的游程长度编码 (RLE)。或者,可以使用 Lempel-Ziv-something 压缩算法之一(LZ77、LZ78、LZW)。
Could you elaborate on why you want to save an image (or its parts) as plain text? Can't you use a binary representation instead? Also, if images typically have lots of contiguous runs of pixels of same color, you may want to use the so-called run-length encoding (RLE). Alternatively, one of Lempel-Ziv-something compression algorithms could be used (LZ77, LZ78, LZW).
将图像压缩为压缩格式(例如 JPEG、PNG、GIF 等),然后将其另存为 .txt 文件或其他文件。要重新创建图像,只需使用适合您特定需求的任何库函数将文件读入您的程序即可。
如果 .txt 文件必须具有某种文本含义,那么您可能会遇到麻烦。
Compress the image into a compressed format (e.g. JPEG, PNG, GIF, etc) and then save it as a .txt file or whatever. To recreate the image, just read in the file into your program using whatever library function suits your particular needs.
If it's necessary that the .txt file have some textual meaning, then you may be in some trouble.
在cs中,有一种类似空间索引的算法可以递归地将平面细分为4个图块。如果单元具有相同的大小,则它看起来像四叉树。如果希望将平面细分为(颜色)图案,您可以使用此平铺想法来动态更改单元格的大小。一个好的起点是 z 曲线或希尔伯特曲线。
In cs there is an algorithm like spatial index to recursivley subdivide a plane into 4 tiles. If the cell has the same size it looks like a quadtree. If want you to subdivide a plane into pattern (of colors) you can use this tiling idea to dynamically change the size of the cell. A good start to look at is a z-curve or a hilbert curve.