-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (76 loc) · 3.55 KB
/
Program.cs
File metadata and controls
91 lines (76 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.IO;
using System.Linq;
using OpenCvSharp;
using OpenCvSharp.Features2D;
namespace MachineVisionPro
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("--- Machine Vision: Bag of Visual Words ---");
// 1. Localização automática da imagem (busca PNG ou png)
string[] possibleFiles = { "hog.PNG", "hog.png", "SURF.PNG", "SURF.png" };
string imagePath = "";
foreach (var fileName in possibleFiles)
{
string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
if (File.Exists(fullPath))
{
imagePath = fullPath;
break;
}
}
if (string.IsNullOrEmpty(imagePath))
{
Console.WriteLine("ERRO: Nenhuma imagem encontrada em: " + AppDomain.CurrentDomain.BaseDirectory);
return;
}
try
{
// 2. Leitura e Extração de Características
using var mat = Cv2.ImRead(imagePath, ImreadModes.Grayscale);
if (mat.Empty()) return;
using var sift = SIFT.Create();
KeyPoint[] keypoints;
using Mat descriptors = new Mat();
sift.DetectAndCompute(mat, null, out keypoints, descriptors);
Console.WriteLine($"Passo 1: {keypoints.Length} descritores SIFT extraídos.");
// 3. Criação do Dicionário Visual (K-Means)
int clusterCount = 20; // Tamanho do vocabulário visual
using Mat labels = new Mat();
using Mat centers = new Mat();
using Mat descriptorsFloat = new Mat();
descriptors.ConvertTo(descriptorsFloat, MatType.CV_32F);
// A SOLUÇÃO: Kmeans com 'm' minúsculo
Cv2.Kmeans(descriptorsFloat, clusterCount, labels,
new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.MaxIter, 10, 1.0),
3, KMeansFlags.PpCenters, centers);
Console.WriteLine($"Passo 2: Dicionário visual de {clusterCount} palavras gerado.");
// 4. Geração do Histograma (Vetor de Características)
float[] histogram = new float[clusterCount];
for (int i = 0; i < labels.Rows; i++)
{
int clusterIdx = labels.At<int>(i);
histogram[clusterIdx]++;
}
Console.WriteLine("Passo 3: Histograma calculado com sucesso.");
Console.WriteLine("Vetor (Primeiras 5 posições): " + string.Join(" | ", histogram.Take(5)));
// 5. Resultado Visual
using Mat output = new Mat();
Cv2.DrawKeypoints(mat, keypoints, output, Scalar.All(-1), DrawMatchesFlags.DrawRichKeypoints);
string outPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "resultado_bovw.png");
Cv2.ImWrite(outPath, output);
Console.WriteLine("\n==========================================");
Console.WriteLine("SUCESSO! Resultado visual salvo em:");
Console.WriteLine(outPath);
Console.WriteLine("==========================================\n");
}
catch (Exception ex)
{
Console.WriteLine($"Erro durante a execução: {ex.Message}");
}
}
}
}