//------------------------------------------------------------------------- #endregion #region private Method //------------------------------------------------------------------------- /// <summary> /// 这段代码?切分矩阵 /// </summary> /// <param name="partialsum"></param> /// <param name="lookaheadsum"></param> /// <param name="vbox"></param> /// <param name="color"></param> /// <param name="total"></param> /// <returns></returns> private static EICBox[] __DoCut(Dictionary <int, int?> partialsum, Dictionary <int, int?> lookaheadsum, EICBox vbox, string color, int total) { int i, left, right, d2; EICBox vbox1, vbox2; //从一个维度的最小值到最大值,rgb。 for (i = vbox[color][0]; i <= vbox[color][1]; i++) { //代表里面的命中的像素以及大于整体的一半信息。 if (partialsum[i] > total / 2) { vbox1 = vbox.copy(); vbox2 = vbox.copy(); left = i - vbox[color][0]; right = vbox[color][1] - i; if (left <= right) { d2 = Math.Min(vbox[color][1] - 1, ~~(i + right / 2)); } else { d2 = Math.Max(vbox[color][0], ~~(i - 1 - left / 2)); } while (!partialsum[d2].HasValue) { d2++; } //肯定有内容 var count2 = lookaheadsum[d2]; while ( (!count2.HasValue || count2.Value == 0) && partialsum.ContainsKey(d2 - 1) && partialsum[d2 - 1].HasValue && partialsum[d2 - 1].Value > 0) { count2 = lookaheadsum[--d2]; } // set dimensions vbox1[color] = new int[] { vbox1[color][0], d2 }; vbox2[color] = new int[] { d2 + 1, vbox2[color][1] }; return(new EICBox[] { vbox1, vbox2 }); } } return(null); }
public EICCMapObject(EICBox vbox, int?[] color) { this.vbox = vbox; this.color = color; }
//------------------------------------------------------------------------- /// <summary> /// 压入矩阵 /// </summary> /// <param name="vbox"></param> public void push(EICBox vbox) { vboxes.push(new EICCMapObject(vbox, vbox.avg())); }
//------------------------------------------------------------------------- private static object[] __MedianCutApply(int[] histo, EICBox vbox) { if (vbox.count() == 0) { return(null); } int rw = vbox.r2 - vbox.r1 + 1, gw = vbox.g2 - vbox.g1 + 1, bw = vbox.b2 - vbox.b1 + 1, //最大颜色,按照最大的变进行切分 maxw = Math.Max(Math.Max(rw, gw), bw); // only one pixel, no split if (vbox.count() == 1) { return(new EICBox[] { vbox.copy(), null }); } /* Find the partial sum arrays along the selected axis. */ int total = 0; Dictionary <int, int?> partialsum = new Dictionary <int, int?>(); Dictionary <int, int?> lookaheadsum = new Dictionary <int, int?>(); int i, j, k, sum, index; if (maxw == rw) { for (i = vbox.r1; i <= vbox.r2; i++) { sum = 0; for (j = vbox.g1; j <= vbox.g2; j++) { for (k = vbox.b1; k <= vbox.b2; k++) { index = GetColorIndex(i, j, k); sum += index < histo.Length ? histo[index] : 0; } } total += sum; partialsum[i] = total; } } else if (maxw == gw) { for (i = vbox.g1; i <= vbox.g2; i++) { sum = 0; for (j = vbox.r1; j <= vbox.r2; j++) { for (k = vbox.b1; k <= vbox.b2; k++) { index = GetColorIndex(j, i, k); sum += index < histo.Length ? histo[index] : 0; } } total += sum; partialsum[i] = total; } } else { /* maxw == bw */ for (i = vbox.b1; i <= vbox.b2; i++) { sum = 0; for (j = vbox.r1; j <= vbox.r2; j++) { for (k = vbox.g1; k <= vbox.g2; k++) { index = GetColorIndex(j, k, i); sum += index < histo.Length ? histo[index] : 0; } } total += sum; partialsum[i] = total; } } foreach (var v in partialsum) { //计算剩余的部分 lookaheadsum[v.Key] = total - v.Value; } // determine the cut planes return (maxw == rw?__DoCut(partialsum, lookaheadsum, vbox, "r", total) : maxw == gw?__DoCut(partialsum, lookaheadsum, vbox, "g", total) : __DoCut(partialsum, lookaheadsum, vbox, "b", total)); }