应用高次、有理代数式为AI生成亚对称图像
原创:daode1212(daode3056)
本文定义不完全对称的图像叫亚对称图像,因为全对称的太过机械,不符合人工的特点,本人基于二元高次的有理式,生成时引入N个随机数分A,B两个组,再通过指针对画布所有像素高速扫描生成三个类别的图像。这些奇异的图像、图案可广泛应用于纺织、工艺、陶瓷、铁艺、瓷砖等行业的图像自动生成,也为AI自动生成图像添加了新元素、新算法,截图如下:
C# 源代码如下:
// Asymmetric solution space (RGB, black and white, root curve).n344:
// By Daode3056, 2024-12-24
unsafe void button344_Click(object sender, EventArgs e)
{int ordeNum = 6; // The highest power of the polynomialint K = 40; // Refinement coefficientint width = 16 * K, height = 16 * K;Bitmap img = new Bitmap(width, height);float dlt = 1f / K; // Differential stepvar RD = new Random();string pStr = "";// Generate random arrays: =================================== double[] A = new double[ordeNum]; double[] B = new double[ordeNum];for (int i = 0; i < ordeNum; i++){A[i] = 21 * RD.NextDouble();pStr += string.Format("{0:0.00}", A[i]) + "|";}pStr += "\r\n";for (int i = 0; i < ordeNum; i++){B[i] = 21 * RD.NextDouble();pStr += string.Format("{0:0.00}", B[i]) + "|";}// Define bitmap data, pointer object: =====================================BitmapData data = img.LockBits(new Rectangle(0, 0, width, height),ImageLockMode.ReadWrite,System.Drawing.Imaging.PixelFormat.Format24bppRgb);var ptr = (byte*)data.Scan0.ToPointer();// Binary high-order polynomial calculation function: =====================================Func<double, double, double> ploy = (double x, double y) =>{double xx = x * x; double yy = y * y;double fx = 1; double fy = 1;double retV = 0;//for (int i = 0; i < ordeNum; i++)//{// fx *= (xx - A[i])*(y - A[i])/(x - A[i]);// fy *= (yy - B[i])*(y - B[i])/(x - B[i]);//}for (int i = 0; i < ordeNum; i++){fx *= (y - A[i]) * (x - B[i]) * (xx - A[i]);fy *= (y - B[i]) * (x - A[i]) * (yy - B[i]);}if (fx != 0 && fy != 0){retV = x * fy / fx - y * y * fx / fy;}return Math.Abs(retV);};// Pointer operation, generate bitmap: ========================int idx = RD.Next(3);for (int i = 0; i < data.Height - 60; i++){for (int j = 0; j < data.Width; j++){float x = (i - width / 2 + 30) / (float)K;float y = (j - height / 2) / (float)K;//====================double z0 = Math.Log(ploy(x, y));if (idx == 0){// RGB image:if (z0 < 3){ptr[i * data.Stride + j * 3] = (byte)(127 - 127 * Math.Cos(z0 / 1.23));ptr[i * data.Stride + j * 3 + 1] = (byte)(127 + 127 * Math.Cos(z0 / 2.19));ptr[i * data.Stride + j * 3 + 2] = (byte)(127 + 127 * Math.Cos(z0 / 3.31));}}if (idx == 1){// Black and white image:if (z0 < 5){ptr[i * data.Stride + j * 3] = (byte)(127 + 127 * Math.Cos(z0) > 127 ? 255 : 0);ptr[i * data.Stride + j * 3 + 1] = (byte)(127 + 127 * Math.Cos(z0) > 127 ? 255 : 0);ptr[i * data.Stride + j * 3 + 2] = (byte)(127 + 127 * Math.Cos(z0) > 127 ? 255 : 0);}}if (idx == 2){// Equation root finding: ====================double z1 = ploy(x - dlt, y);double z2 = ploy(x + dlt, y);double z3 = ploy(x, y - dlt);double z4 = ploy(x, y + dlt);//=========================if (z1 * z2 < 100 || z3 * z4 < 100){ptr[i * data.Stride + j * 3] = (byte)(127 - 127 * Math.Cos(z0 / 137) > 127 ? 255 : 0);ptr[i * data.Stride + j * 3 + 1] = (byte)(127 - 127 * Math.Cos(z0 / 137) > 127 ? 255 : 0);ptr[i * data.Stride + j * 3 + 2] = (byte)(127 - 127 * Math.Cos(z0 / 137) > 127 ? 255 : 0);}else{ptr[i * data.Stride + j * 3] = (byte)255;ptr[i * data.Stride + j * 3 + 1] = (byte)255;ptr[i * data.Stride + j * 3 + 2] = (byte)255;}}}}img.UnlockBits(data);// Bitmap drawing tool:Graphics gs = Graphics.FromImage(img);Pen pen0 = new Pen(Color.FromArgb(0, 0, 60), 2);Brush bh = new SolidBrush(Color.White);gs.DrawString("["+idx+"]"+pStr, new Font("SimHei", 11), bh, 15 * K - pStr.Length * 5, height - 50);pictureBox1.Image = img;img.Save("button34_"+ string.Format("{0:X}",RD.Next(0xFFFFFF)) +".png");
}