public int LoadTexture(string file) { var img = (Bitmap)Bitmap.FromFile(file); using (img) { var rect = new Rectangle(new Point(), img.Size); var data = img.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb); int texId = GL.GenTexture(); //generate a new texture GL.BindTexture(TextureTarget.Texture2D, texId); // bind the texture (to start working withthe texture) GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, img.Size.Width, img.Size.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); img.UnlockBits(data); //set filters - this is important GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); return(texId); } }
private static void CreateBoW() { var numberOfWords = 36; foreach (var file in Directory.EnumerateFiles(@"C:\Temp\TLLCamerasTestData\37_Training", "*.jpg")) { var trainingImage = (Bitmap)Bitmap.FromFile(file); trainingImages.Add(file, trainingImage); } foreach (var file in Directory.EnumerateFiles(@"C:\Temp\TLLCamerasTestData\37_Testing", "*.jpg")) { var testImage = (Bitmap)Bitmap.FromFile(file); testingImages.Add(file, testImage); } // We will use SURF, so we can use a standard clustering // algorithm that is based on Euclidean distances. A good // algorithm for clustering codewords is the Binary Split // variant of the K-Means algorithm. // Create a Binary-Split clustering algorithm BinarySplit binarySplit = new BinarySplit(numberOfWords); // Create bag-of-words (BoW) with the given algorithm BagOfVisualWords surfBow = new BagOfVisualWords(binarySplit); // Compute the BoW codebook using training images only IBagOfWords <Bitmap> bow = surfBow.Learn(trainingImages.Values.ToArray()); // now that we've created the bow we need to use it to create a representation of each training and test image foreach (var trainingImage in trainingImages.Keys) { var asBitmap = trainingImages[trainingImage] as Bitmap; var featureVector = (bow as ITransform <Bitmap, double[]>).Transform(asBitmap); var featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); trainingFeatures.Add(trainingImage, featureVector); } foreach (var testingImage in testingImages.Keys) { var asBitmap = testingImages[testingImage] as Bitmap; var featureVector = (bow as ITransform <Bitmap, double[]>).Transform(asBitmap); var featureString = featureVector.ToString(DefaultArrayFormatProvider.InvariantCulture); testingFeatures.Add(testingImage, featureVector); } }
/// <summary> /// Download our tile /// </summary> public Task DownloadTile() { if (DownloadClient == null) { DownloadClient = new WebClient[5]; for (int a = 0; a < DownloadClient.Length; a++) { DownloadClient[a] = new WebClient(); DownloadClient[a].DownloadDataCompleted += new DownloadDataCompletedEventHandler(MM_MapTile_DownloadDataCompleted); DownloadClient[a].UseDefaultCredentials = false; DownloadClient[a].Proxy = new WebProxy(WebRequest.DefaultWebProxy.GetProxy(new Uri(GetImageURL()))); DownloadClient[a].Proxy.Credentials = CredentialCache.DefaultCredentials; DownloadClient[a].Credentials = CredentialCache.DefaultCredentials; } } //Now, check to see whether our tile is cached if (!String.IsNullOrEmpty(MM_Repository.OverallDisplay.MapTileCache)) { if (!Directory.Exists(MM_Repository.OverallDisplay.MapTileCache)) { Directory.CreateDirectory(MM_Repository.OverallDisplay.MapTileCache); } string filename = GetFileName(); string filePath = Path.GetDirectoryName(filename); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } if (File.Exists(filename)) { this.Tile = ((Bitmap)Bitmap.FromFile(filename)); } } return(Task.Run(() => { //First, make sure our downloader is set up if (this.Tile == null) { foreach (WebClient wClient in DownloadClient) { if (wClient.IsBusy == false) { Saving = true; wClient.DownloadDataAsync(new Uri(GetImageURL()), this); return; } } } })); }
private static void WriteBitmap(Bitmap textureMap, string texPath, int textureSize, ref int countX, ref int countY) { var file = $"{SharpCraft.Instance.GameFolderDir}\\SharpCraft_Data\\assets\\textures\\{texPath}.png"; Bitmap tex = (File.Exists(file) ? new Bitmap(Bitmap.FromFile(file), textureSize, textureSize) : null) ?? new Bitmap(TextureManager.TEXTURE_MISSING, textureSize, textureSize); using (tex) { using (Graphics g = Graphics.FromImage(textureMap)) { g.DrawImage(tex, countX, countY); countX += textureSize; if (countX + textureSize > textureMap.Width) { countX = 0; countY += textureSize; } } } }