public int ComputeMoments(ColorImage input, Color bgr, int S, ref double m00, ref double m01, ref double m10, ref double m11, ref double m02, ref double m20) { double Ival; m00 = m01 = m10 = m11 = m02 = m20 = 0; for (int y = 1; y <= S; y++) { for (int x = 1; x <= S; x++) { Ival = 0.0; if ((x < input.Width) && (y < input.Height)) { Color c = input.GetColor(x, y); Ival = MomentI(c, bgr) * 255.0; } m00 += Ival; m01 += y * Ival; m10 += x * Ival; m11 += y * x * Ival; m02 += y * y * Ival; m20 += x * x * Ival; } } return(1); }
public static void BlendImages(ref ColorImage lower, ColorImage upper, Color c, int xc, int yc) { int xOffset = xc - upper.Width; int yOffset = yc - upper.Height; for (int y = 0; y < upper.Height; y++) { for (int x = 0; x < upper.Width; x++) { int lowerX = x + xOffset + 15; int lowerY = y + yOffset + 15; if ((lowerX > 0) && (lowerX < lower.Width) && (lowerY > 0) && (lowerY < lower.Height)) { int upperR = upper.GetColor(x, y).R; double alpha = upperR / 255.0; double oneMinusAlpha = (1.0 - alpha); Color lowerC = lower.GetColor(lowerX, lowerY); int r = (int)((alpha * c.R) + (lowerC.R * oneMinusAlpha)); int g = (int)((alpha * c.G) + (lowerC.G * oneMinusAlpha)); int b = (int)((alpha * c.B) + (lowerC.B * oneMinusAlpha)); lower.SetColor(lowerX, lowerY, Color.FromArgb(r, g, b)); } } } }
double Moment00(ColorImage input, Color bgr, StreamWriter writer) { double m = 0.0; int lineCount = 0; DateTime start = DateTime.Now; for (int i = 0; i < input.Height; i++) { for (int j = 0; j < input.Width; j++) { Color c = input.GetColor(j, i); m += MomentI(c, bgr); //String* s1 = String::Concat(" c=", c.R.ToString(), String::Concat(",", c.G.ToString(), ",", c.B.ToString())); //writer->WriteLine(String::Concat(lineCount.ToString(), s1, ": m=", m.ToString("000.00"))); lineCount++; } } //TimeSpan duration = DateTime.Now - start; //if (duration.TotalMilliseconds > 20) // System.Console.WriteLine("Moment00 completed in " + duration.TotalMilliseconds + "ms"); return(m); }
public Stroke GenerateStroke(ColorImage input, int x, int y, int s, int factor, int level) { ArrayList list = new ArrayList(); double m00, m01, m10, m11, m02, m20; double a, b, c; double tempval; double dw, dxc, dyc; int xc, yc; double theta; float w, l; m00 = m01 = m10 = m11 = m02 = m20 = 0; System.Drawing.Color firstColor = input.GetColor(x, y); //System::Drawing::Color::FromArgb((int)color[0], (int)color[1], (int)color[2]); ColorImage cropImage = input.Crop(x, y, s, s); ComputeMoments(cropImage, firstColor, s, ref m00, ref m01, ref m10, ref m11, ref m02, ref m20); dxc = m10 / m00; dyc = m01 / m00; a = (m20 / m00) - (double)((dxc) * (dxc)); b = 2 * (m11 / m00 - (double)((dxc) * (dyc))); c = (m02 / m00) - (double)((dyc) * (dyc)); theta = System.Math.Atan2(b, (a - c)) / 2; tempval = System.Math.Sqrt(b * b + (a - c) * (a - c)); dw = System.Math.Sqrt(6 * (a + c - tempval)); w = (float)(System.Math.Sqrt(6 * (a + c - tempval))); l = (float)(System.Math.Sqrt(6 * (a + c + tempval))); xc = (int)(x + System.Math.Round(dxc - s / 2)); yc = (int)(y + System.Math.Round(dyc - s / 2)); Stroke stroke = new Stroke(); stroke.xc = factor * xc; stroke.yc = factor * yc; stroke.w = factor * w; stroke.l = factor * l; stroke.theta = (float)theta; stroke.red = firstColor.R; stroke.green = firstColor.G; stroke.blue = firstColor.B; stroke.level = level; return(stroke); }
public ColorImage GenerateStrokeArea(Bitmap input, int s) { ColorImage piece; double m; ArrayList list = new ArrayList(); double pixVal; DateTime start = DateTime.Now; //Bitmap img = new Bitmap(input.Width, input.Height); ColorImage cImg = new ColorImage(input.Width, input.Height); StreamWriter writer = null; //new StreamWriter("new.txt"); ColorImage inMem = new ColorImage(new UnsafeBitmap(input)); for (int i = 0; i < input.Height; i++) { for (int j = 0; j < input.Width; j++) { piece = inMem.Crop(j, i, s, s); System.Drawing.Color topColor = inMem.GetColor(j, i); m = Moment00(piece, topColor, writer); pixVal = (255.0 * m / (piece.Width * piece.Height)); cImg.SetColor(j, i, Color.FromArgb((int)pixVal, (int)pixVal, (int)pixVal)); } } //img.Save("area.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); TimeSpan duration = DateTime.Now - start; System.Console.WriteLine("Stroke area computed in " + duration.TotalMilliseconds + "ms"); return(cImg); }
public static ColorImage Rotate(ColorImage input, double thetaInRadians) { double q = thetaInRadians; int max = System.Math.Max(input.Width, input.Height); ColorImage output = new ColorImage(max + max, max + max); double cosX = System.Math.Cos(q); double sinX = System.Math.Sin(q); int origXCenter = input.Width / 2; int origYCenter = input.Height / 2; int xOffset = (int)(output.Width / 2); int yOffset = (int)(output.Height / 2); int xCenter = (int)((origXCenter * cosX) - (origYCenter * sinX)); int yCenter = (int)((origXCenter * sinX) + (origYCenter * cosX)); int xc = 0, yc = 0; for (int y = 0; y < input.Height; y++) { for (int x = 0; x < input.Width; x++) { xc = x - origXCenter; yc = y - origYCenter; int xPrime = (int)((xc * cosX) - (yc * sinX)) + xOffset; int yPrime = (int)((xc * sinX) + (yc * cosX)) + yOffset; if ((xPrime > 0) && (xPrime < output.singledata.Length) && (yPrime > 0) && (yPrime < output.singledata.Length)) { output.SetColor(xPrime, yPrime, input.GetColor(x, y)); } } } return(output); }
public static ColorImage ScaleHeight(ColorImage input, int oh) { ColorImage output = new ColorImage(input.Width, oh); int x, y, v; int res_r, acc_r; int res_g, acc_g; int res_b, acc_b; int p, q; int ih = input.Height; int area = (ih * oh); for (x = 0; x < input.Width; x++) { q = ih; p = oh; acc_r = 0; acc_g = 0; acc_b = 0; y = v = 0; while (y < oh) { if (v + 1 < ih) { /* * res_r = p * input.data[v][x].R + (oh - p) * input.data[v][x].R; * res_g = p * input.data[v][x].G + (oh - p) * input.data[v][x].G; * res_b = p * input.data[v][x].B + (oh - p) * input.data[v][x].B; */ Color cxv = input.GetColor(x, v); res_r = p * cxv.R + (oh - p) * cxv.R; res_g = p * cxv.G + (oh - p) * cxv.G; res_b = p * cxv.B + (oh - p) * cxv.B; } else { /* * res_r = oh * input.data[v][x].R; * res_g = oh * input.data[v][x].G; * res_b = oh * input.data[v][x].B; */ Color cxv = input.GetColor(x, v); res_r = oh * cxv.R; res_g = oh * cxv.G; res_b = oh * cxv.B; } if (p < q) { acc_r += res_r * p; acc_g += res_g * p; acc_b += res_b * p; q -= p; p = oh; v++; } else { acc_r += res_r * q; acc_g += res_g * q; acc_b += res_b * q; //output.data[y][x]= Color.FromArgb((byte)(acc_r / area), (byte)(acc_g / area), (byte)(acc_b / area)); output.SetColor(x, y, Color.FromArgb((byte)(acc_r / area), (byte)(acc_g / area), (byte)(acc_b / area))); acc_r = 0; acc_g = 0; acc_b = 0; p -= q; q = ih; y++; } } } output.Width = input.Width; output.Height = oh; return(output); }
public static ColorImage ScaleWidth(ColorImage input, int ow) { ColorImage output = new ColorImage(ow, input.Height); int u, x, y; int res_r, res_g, res_b, acc_r, acc_g, acc_b; int p, q; int iw = input.Width; int area = (ow * iw); for (y = 0; y < input.Height; y++) { q = iw; p = ow; acc_r = 0; acc_g = 0; acc_b = 0; x = u = 0; while (x < ow) { if (u + 1 < iw) { /* * res_r = p * input.data[y][u].R + (ow - p) * input.data[y][u+1].R; * res_g = p * input.data[y][u].G + (ow - p) * input.data[y][u+1].G; * res_b = p * input.data[y][u].B + (ow - p) * input.data[y][u+1].B; */ Color cuy = input.GetColor(u, y); Color cuPlusOney = input.GetColor(u + 1, y); res_r = p * cuy.R + (ow - p) * cuPlusOney.R; res_g = p * cuy.G + (ow - p) * cuPlusOney.G; res_b = p * cuy.B + (ow - p) * cuPlusOney.B; } else { /* * res_r = ow * input.data[y][u].R; * res_g = ow * input.data[y][u].G; * res_b = ow * input.data[y][u].B; */ Color cuy = input.GetColor(u, y); res_r = ow * cuy.R; res_g = ow * cuy.G; res_b = ow * cuy.B; } if (p < q) { acc_r += res_r * p; acc_g += res_g * p; acc_b += res_b * p; q -= p; p = ow; u++; } else { acc_r += res_r * q; acc_g += res_g * q; acc_b += res_b * q; //output.data[y][x] = Color.FromArgb((byte)(acc_r / area), (byte)(acc_g / area), (byte)(acc_b / area)); output.SetColor(x, y, Color.FromArgb((byte)(acc_r / area), (byte)(acc_g / area), (byte)(acc_b / area))); acc_r = 0; acc_g = 0; acc_b = 0; p -= q; q = iw; x++; } } } output.Width = ow; output.Height = input.Height; return(output); }
public static int Blend(ref ColorImage inp, ColorImage stroke, Color rgb, int xc, int yc) { int xi, yi; /* lower left corner in input image */ int xa, ya; /* corresponding corner in alpha image */ int wa, ha; /* area in alpha image to be blended */ wa = stroke.Width; xa = 0; xi = xc - wa / 2; if (xi < 0) { wa += xi; xa -= xi; xi = 0; } if (xi > inp.Width) { return(0); } if (wa <= 0) { return(0); } if (xi + wa >= inp.Width) { wa = inp.Width - xi; } ha = stroke.Height; ya = 0; yi = yc - ha / 2; if (yi < 0) { ha += yi; ya -= yi; yi = 0; } if (yi > inp.Height) { return(0); } if (ha <= 0) { return(0); } if (yi + ha >= inp.Height) { ha = inp.Height - yi; } //la = stroke->row*stroke->type; //li = in->row*in->type; //input = in->buffer + li*yi + xi*in->type; //alpha = stroke->buffer + la*ya + xa*stroke->type; for (int iny = yi, strokey = ya; strokey < ha; iny++, strokey++) { for (int inx = xi, strokex = xa; strokex < wa; inx++, strokex++) { int upperR = stroke.GetColor(strokex, strokey).R; double alpha = upperR / 255.0; double oneMinusAlpha = (1.0 - alpha); Color lowerC = inp.GetColor(inx, iny); int r = (int)((alpha * rgb.R) + (lowerC.R * oneMinusAlpha)); int g = (int)((alpha * rgb.G) + (lowerC.G * oneMinusAlpha)); int b = (int)((alpha * rgb.B) + (lowerC.B * oneMinusAlpha)); inp.SetColor(inx, iny, Color.FromArgb(r, g, b)); } } return(1); }
public Bitmap Dither(ColorImage inImage) { int width = inImage.Width; int height = inImage.Height; int [,] input = new int [height, width]; Bitmap img = new Bitmap(width, height); int w, h; float error, val; float a = (float)((s - 1) / System.Math.Pow(255.0, p)); int [,] inputPrime = new int [height, width]; // Convert into collection of Color objects for (int y = 0; y < inImage.Height; y++) { for (int x = 0; x < inImage.Width; x++) { int r = inImage.GetColor(x, y).R; input[y, x] = r; } } System.Random rand = new System.Random(); w = width; h = height; /* allocates working space */ float [] cur = new float [w]; float [] nxt = new float [w]; /* inicializa o buffer de proxima linha */ for (int x = 0; x < w; x++) { nxt[x] = System.Math.Max(1.0f / (a * (float)(System.Math.Pow(input[0, x], p)) + 1), 0.5f); } int yClone = 0; for (int y = 0; y < h - 1; y++, yClone = y) { /* next line becomes current line */ nxt.CopyTo(cur, 0); /* copies next line to local buffer */ nxt[0] = System.Math.Max(1.0f / (a * (float)(System.Math.Pow(input[y + 1, 0], p)) + 1), 0.5f); for (int x = 1; x < w; x++) { nxt[x] = 1.0f / (a * (float)(System.Math.Pow(input[y + 1, x], p)) + 1); } /* spread error */ int xClone = 0; for (int x = 0; x < w - 1; x++, xClone = x) { val = cur[x] > 1.0f ? 1.0f : 0.0f; error = cur[x] - val; inputPrime[y, x] = val > 0.0 ? 0 : 255; switch (rand.Next() % 4) { case 0: nxt[x + 1] += error / 16.0f; if (x > 0) { nxt[x - 1] += 3 * error / 16.0f; } nxt[x] += 5 * error / 16.0f; cur[x + 1] += 7 * error / 16.0f; break; case 1: nxt[x + 1] += 7 * error / 16.0f; if (x > 0) { nxt[x - 1] += error / 16.0f; } nxt[x] += 3 * error / 16.0f; cur[x + 1] += 5 * error / 16.0f; break; case 2: nxt[x + 1] += 5 * error / 16.0f; if (x > 0) { nxt[x - 1] += 7 * error / 16.0f; } nxt[x] += error / 16.0f; cur[x + 1] += 3 * error / 16.0f; break; case 3: nxt[x + 1] += 3 * error / 16.0f; if (x > 0) { nxt[x - 1] += 5 * error / 16.0f; } nxt[x] += 7 * error / 16.0f; cur[x + 1] += error / 16.0f; break; } } inputPrime[y, xClone] = cur[xClone] > 1.0 ? 0 : 255; } /* clip whole last line */ for (int x = 0; x < w; x++) { inputPrime[yClone, x] = nxt[x] > 1.0 ? 0 : 255; } // Generate bitmap for (int y2 = 0; y2 < height; y2++) { for (int x2 = 0; x2 < width; x2++) { img.SetPixel(x2, y2, System.Drawing.Color.FromArgb(inputPrime[y2, x2], inputPrime[y2, x2], inputPrime[y2, x2])); } } return(img); }