Created
May 11, 2013 15:30
-
-
Save hellok/5560277 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace koh | |
{ | |
using AForge; | |
using AForge.Imaging; | |
using AForge.Imaging.Filters; | |
//using koh; | |
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.Serialization.Formatters.Binary; | |
internal class Program | |
{ | |
private static bool CheckArg(string arg) | |
{ | |
try | |
{ | |
FileInfo info = new FileInfo(arg); | |
return (info.Exists && (info.Length < 0x800000L)); | |
} | |
catch (Exception) | |
{ | |
return false; | |
} | |
} | |
private static List<byte> ConvertImage(Bitmap img) | |
{ | |
List<byte> list = new List<byte>(); | |
using (UnmanagedImage image = UnmanagedImage.FromManagedImage(img)) | |
{ | |
List<IntPoint> points = new List<IntPoint>(); | |
for (int i = 0; i < image.Width; i++) | |
{ | |
for (int j = 0; j < image.Height; j++) | |
{ | |
points.Add(new IntPoint(i, j)); | |
Console.Write(i+","+j+":"); | |
} | |
} | |
byte[] collection = image.Collect8bppPixelValues(points); | |
Console.Write(collection + ":"); | |
list.AddRange(collection); | |
} | |
return list; | |
} | |
private static Bitmap LoadImage(string arg) | |
{ | |
return AForge.Imaging.Image.FromFile(arg); | |
} | |
private static void Main(string[] args) | |
{ | |
/* if ((args.Length != 1) || !CheckArg(args[0])) | |
{ | |
Usage(); | |
} | |
else | |
{ | |
* */ | |
// string arg = args[0]; | |
string arg = "C:\\Users\\Administrator\\Desktop\\baltctf\\braaainz\\out.bmp"; | |
Bitmap img = ProcessImage(LoadImage(arg)); | |
List<byte> source = ConvertImage(img); | |
img.Save("out.jpg"); | |
BinaryFormatter formatter = new BinaryFormatter(); | |
FileStream serializationStream = File.OpenRead("pinky.brain"); | |
KohonenLayer layer = (KohonenLayer) formatter.Deserialize(serializationStream); | |
Console.WriteLine(layer.Neurons.Count); | |
Console.WriteLine(layer.PrevLayer.Count); | |
/* | |
for (int j = 0; j < layer.Neurons.Count; j++) | |
{ | |
Console.WriteLine(layer.Neurons[j]); | |
Console.WriteLine(((Neuron)layer.Neurons[j]).Data); | |
foreach (Link link in (((Neuron)layer.Neurons[j]).IngoingLinks)) | |
{ | |
Console.Write(link.Data); | |
} | |
foreach (Link link in (((Neuron)layer.Neurons[j]).OutgoingLinks)) | |
{ | |
Console.Write(link.Data); | |
} | |
} | |
*/ | |
Console.WriteLine("######################################"); | |
Console.WriteLine(layer.PrevLayer.Count); | |
for (int j = 0; j < layer.PrevLayer.Count; j++) | |
{ | |
//Console.Write(layer.PrevLayer[j]); | |
foreach (Link link in layer.PrevLayer[j].IngoingLinks) | |
{ | |
//Console.Write(link.Data); | |
// Console.WriteLine(link.Weight); | |
} | |
foreach (Link link in layer.PrevLayer[j].OutgoingLinks) | |
{ | |
//Console.Write(link.Data); | |
// Console.WriteLine(link.Weight); | |
} | |
} | |
double minValue = double.MinValue; | |
int num2 = 0;//让1号神经元的SUM最大。625个点指向10个神经元 | |
List<double> aa = source.Select<byte, double>(new Func<byte, double>(Convert.ToDouble)).ToList<double>(); | |
///////////////////////////////////////// | |
for (int i = 0; i < aa.Count; i++) | |
{ | |
foreach (Link link in layer.PrevLayer[i].OutgoingLinks) | |
{ | |
link.Data = aa[i]; | |
} | |
} | |
Console.WriteLine("layer.Neurons.Count:"+layer.Neurons.Count); | |
for (int j = 0; j < layer.Neurons.Count; j++) | |
{ | |
double num5 = ((Neuron)layer.Neurons[j]).Activate(); | |
Console.WriteLine(num5); | |
if (num5 > minValue) | |
{ | |
minValue = num5; | |
num2 = j; | |
} | |
} | |
Console.WriteLine("CQ:"+num2); | |
//source: 255---->aa:255.0 | |
Console.WriteLine(layer.Activate(aa)); | |
//} | |
} | |
private static Bitmap ProcessImage(Bitmap img) | |
{ | |
img = new ResizeBicubic(0x19, 0x19).Apply(img); | |
//http://www.aforgenet.com/framework/docs/html/1371dd12-7dbd-2a0e-a0d7-32d8c28d73eb.htm | |
img = new GrayscaleRMY().Apply(img); | |
//http://www.aforgenet.com/framework/docs/html/1100c0be-7a9f-490e-b49c-a2f3cc681a01.htm | |
img = new OtsuThreshold().Apply(img); | |
//http://www.aforgenet.com/framework/docs/html/b2bd54da-46c2-cb64-3577-0962d8f56554.htm | |
return img; | |
} | |
private static void Usage() | |
{ | |
Console.WriteLine("Provide path to image"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment