7

所以我试图在我的 Java 应用程序中编码一些动画 gif 文件。我一直在使用一些在网上找到的类/算法,但似乎没有一个运行得足够好。

现在我正在使用这个量化类将图像的颜色减少到 256:http ://www.java2s.com/Code/Java/2D-Graphics-GUI/Anefficientcolorquantizationalgorithm.htm

问题是,它似乎不是很“聪明”。

如果我传入超过 256 种颜色的图像,它确实会减少颜色数量,但效果不是很好。(红色变成蓝色,等等 - 像这样的非常明显的错误)。

您可以推荐任何其他用于 Java 中颜色量化的算法/库吗?


注意:我知道在此算法中使用的 Neuquant:http ://www.java2s.com/Code/Java/2D-Graphics-GUI/AnimatedGifEncoder.htm

它非常慢并且会产生“eh”结果(帧之间的颜色闪烁)。

4

3 回答 3

12

你可以用谷歌搜索其他算法,如中位数切割、人口、k-means 等。

我最近也需要这个,但必须好看又快(我需要这个来实时视频捕捉)所以我设法做了这样的事情:

  1. 转换为 15 位 rgb

    如果您已经拥有 RGB,则可以跳过此步骤,但应用 shift/and 以匹配每通道 5 位。您也可以使用 5:6:5 方案

  2. 做一个直方图

    15 位 rgb 导致 32768 个条目,因此创建 2 个数组

    • his[32768]是像素数(直方图)
    • idx[32768]是索引 = 颜色值

    在计算颜色时,如果使用低位计数或高分辨率,请确保计数器不会溢出

  3. 重新排序数组,使零在his[]数组的末尾

    还计算非零条目his[]并调用它hists

  4. (index) 排序hist[],idx[]所以hist[]按降序排列;

  5. 创建 N 调色板

    取颜色idx[i]i={ 0,1,2,3,...,hists-1 }),看看你的调色板中是否没有相似的颜色。如果忽略此颜色(将其设置为找到的最接近的颜色),否则将其添加到调色板。如果你到达 N 颜色停止

  6. 创建颜色映射

    所以取每种颜色并在调色板中找到最接近的颜色(这可以在步骤 5 中部分完成)我称这个表为recolor[32][32][32]

  7. 重新着色图像

这是 C++ 源代码:

BYTE db,*p;
AnsiString code;
int e,b,bits,adr;
int x0,x1,y0,y1,x,y,c;
DWORD ix,cc,cm,i0,i,mask;
union { DWORD dd; BYTE db[4]; } c0,c1;


    DWORD r,g,b; int a,aa,hists;
    DWORD his[32768];
    DWORD idx[32768];
    // 15bit histogram
    for (x=0;x<32768;x++) { his[x]=0; idx[x]=x; }
    for (y=0;y<ys;y++)
     for (x=0;x<xs;x++)
        {
        cc=pyx[y][x];
        cc=((cc>>3)&0x1F)|((cc>>6)&0x3E0)|((cc>>9)&0x7C00);
        if (his[cc]<0xFFFFFFFF) his[cc]++;
        }
    // remove zeroes
     for (x=0,y=0;y<32768;y++)
        {
        his[x]=his[y];
        idx[x]=idx[y];
        if (his[x]) x++;
        } hists=x;
    // sort by hist
    for (i=1;i;)
     for (i=0,x=0,y=1;y<hists;x++,y++)
      if (his[x]<his[y])
        {
        i=his[x]; his[x]=his[y]; his[y]=i;
        i=idx[x]; idx[x]=idx[y]; idx[y]=i; i=1;
        }
    // set lcolor color palete
    for (i0=0,x=0;x<hists;x++) // main colors
        {
        cc=idx[x];
        b= cc     &31;
        g=(cc>> 5)&31;
        r=(cc>>10)&31;
        c0.db[0]=b;
        c0.db[1]=g;
        c0.db[2]=r;
        c0.dd=(c0.dd<<3)&0x00F8F8F8;
        // skip if similar color already in lcolor[]
        for (a=0,i=0;i<i0;i++)
            {
            c1.dd=lcolor[i];
            aa=int(BYTE(c1.db[0]))-int(BYTE(c0.db[0])); if (aa<=0) aa=-aa; a =aa;
            aa=int(BYTE(c1.db[1]))-int(BYTE(c0.db[1])); if (aa<=0) aa=-aa; a+=aa;
            aa=int(BYTE(c1.db[2]))-int(BYTE(c0.db[2])); if (aa<=0) aa=-aa; a+=aa;
            if (a<=16) { a=1; break; } a=0; // *** treshold ***
            }
        if (a) recolor[r][g][b]=i;
        else{
            recolor[r][g][b]=i0;
            lcolor[i0]=c0.dd; i0++;
            if (i0>=DWORD(lcolors)) { x++; break; }
            }
        }   // i0 = new color table size
    for (;x<hists;x++)  // minor colors
        {
        cc=idx[x];
        b= cc     &31;
        g=(cc>> 5)&31;
        r=(cc>>10)&31;
        c0.db[0]=b;
        c0.db[1]=g;
        c0.db[2]=r;
        c0.dd=(c0.dd<<3)&0x00F8F8F8;
        // find closest color
        int dc=-1; DWORD ii=0;
        for (a=0,i=0;i<i0;i++)
            {
            c1.dd=lcolor[i];
            aa=int(BYTE(c1.db[0]))-int(BYTE(c0.db[0])); if (aa<=0) aa=-aa; a =aa;
            aa=int(BYTE(c1.db[1]))-int(BYTE(c0.db[1])); if (aa<=0) aa=-aa; a+=aa;
            aa=int(BYTE(c1.db[2]))-int(BYTE(c0.db[2])); if (aa<=0) aa=-aa; a+=aa;
            if ((dc<0)||(dc>a)) { dc=a; ii=i; }
            }
        recolor[r][g][b]=ii;
        }

所有者图像类包含以下内容:

// image data
Graphics::TBitmap *bmp,*bmp0,*bmp1; // actual and restore to 32bit frames,and 8bit input conversion frame
int xs,ys;                      // resolution
int *py;                        // interlace table
DWORD **pyx,**pyx0;             // ScanLine[] of bmp,bmp0
BYTE  **pyx1;

// colors (colors are computed from color_bits)
DWORD gcolor[256];              //hdr
DWORD lcolor[256];              //img
BYTE  recolor[32][32][32];      //encode reduce color table
int scolors,scolor_bits;        //hdr screen color depth
int gcolors,gcolor_bits;        //hdr global pallete
int lcolors,lcolor_bits;        //img/hdr local palette
  • 包含pyx[],bmp源 32 位图像
  • pyx1[],bmp1是用于编码的临时 8 位图像

这是重新着色的完成方式:

    // recolor to lcolors
    for (y=0;y<ys;y++)
     for (x=0;x<xs;x++)
        {
        int r,g,b;
        c0.dd=(pyx[y][x]>>3)&0x001F1F1F;
        b=c0.db[0];
        g=c0.db[1];
        r=c0.db[2];
        i=recolor[r][g][b];
//      pyx [y][x]=lcolor[i];   // 32 bit output (visual)
        pyx1[y][x]=i;           // 8  bit output (encoding)
        }

这里有一些输出示例:

这是 VCL/GDI 色彩还原、我的方法和原始图像之间的比较)

GDI 与此算法

上部是调色板绘制(原始图像包含中间图像的调色板)

这是真彩色照片:

原始照片

并减少到 256 种颜色:

减少颜色

这需要大约 185 毫秒来编码成 GIF(包括颜色减少)。我对结果非常满意,但正如您所见,图像并不相同。重新着色后的绿草簇有点不同(面积/强度较小?)

[笔记]

代码尚未优化,因此它应该是一种使其更快的方法。您可以通过以下方式提高编码速度:

  1. 降低最大编码字典大小
  2. 使用字典或三个结构的索引表来加快搜索速度
  3. 可以将直方图冒泡排序更改为更快的排序算法(但那部分代码远非关键)
  4. 要编码序列,您可以使用单个调色板(如果场景没有太多颜色变化)
  5. 如果您想要更快的速度,请创建静态调色板并使用抖动代替所有这些

这是 RT 捕获视频的示例(源为 50fps,因此我降低了分辨率以匹配速度):

捕获示例

于 2015-05-15T17:17:34.580 回答
2

你可以使用Gif89Encoder

这个用于编码 GIF 的 Java 类库与任何其他免费的 Java GIF 编码器相比,它涵盖了更多扩展的 GIF89a 功能集,包括动画和嵌入的文本注释。

http://imagej.nih.gov/ij/

用于 Java 的动画 GIF 库

我已经为 Java 使用了动画 GIF 库,效果很好

于 2015-03-25T01:42:45.330 回答
2

在这里...我写了这个,它的工作速度比 Octree 快一点,并且似乎在大多数图像上产生了更好的结果(而且编码更容易哈哈)。它基本上像八叉树一样工作,但相反......它创建一个初始颜色列表,然后根据需要通过有序位(随后降低位#)分割具有最多唯一颜色的列表,直到它有尽可能多的列出所需的颜色。然后它返回一个包含每个列表的平均颜色的数组......

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;


namespace SeelWorks.Libraries.Imaging.Quantization {

    public static class BitSplitQuantizer {

        public static Color[] CreatePalette(IEnumerable<Color> sourceColors, int maxColors = 256) {
            var collections = new List<Collection>();
            collections.Add(new Collection());
            foreach(var _ in sourceColors) collections[0].Add(_);
            var offset = 1;
            while(collections.Count < maxColors) {
                if(offset > collections.Count) {
                    break;
                } else {
                    collections = collections.OrderBy(_ => _.Colors.Count).ToList();
                    var split = collections[collections.Count - offset].Split();
                    if((split.Count == 1) || ((collections.Count + split.Count - 1) > maxColors)) {
                        offset++;
                    } else {
                        offset = 1;
                        collections.RemoveAt(collections.Count - 1);
                        collections.AddRange(split);
                    }
                }
            }
            return collections.Select(_ => _.GetAverageColor()).ToArray();
        }


        private class Collection {
            public Dictionary<Color, int> Colors = new Dictionary<Color, int>();
            public int Level = -1;

            public void Add(Color color) {
                if(!Colors.ContainsKey(color)) Colors.Add(color, 0);
                Colors[color]++;
            }

            public List<Collection> Split() {
                var colors = Colors.OrderBy(_ => _.Value).Select(_ => _.Key).ToList();
                var level = (7 - Level - 1);
                var indexes = new int[8] { -1, -1, -1, -1, -1, -1, -1, -1 };
                var ret = new List<Collection>();
                foreach(var _ in colors) {
                    var index_ = ((((_.R >> level) & 1) << 2) | (((_.G >> level) & 1) << 1) | ((_.B >> level) & 1));
                    if(indexes[index_] == -1) {
                        ret.Add(new Collection());
                        indexes[index_] = (ret.Count - 1);
                        ret[ret.Count - 1].Level = (Level + 1);
                    }
                    ret[indexes[index_]].Colors[_] = Colors[_];
                }
                return ret;
            }

            public Color GetAverageColor() {
                var r = 0.0;
                var g = 0.0;
                var b = 0.0;
                var t = 0.0;
                foreach(var _ in Colors) {
                    r += (_.Key.R * _.Value);
                    g += (_.Key.G * _.Value);
                    b += (_.Key.B * _.Value);
                    t += _.Value;
                }
                return Color.FromArgb((int)Math.Round(r / t), (int)Math.Round(g / t), (int)Math.Round(b / t));
            }
        }

    }

}

原图:
原来的

八叉树量化(0.145s):
八叉树量化

BitSplit 量化 (0.100s):
BitSplit 量化

原图:
原始图像

八叉树量化(0.233s):
八叉树量化

BitSplit 量化 (0.213s):
BitSplit 量化

于 2017-04-16T17:34:34.573 回答