SqueezeBrains SDK 1.13
SurfaceDetect.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using sb_cs;
7using Tutorials_Common;
8
10{
42 {
43 static SbError execute()
44 {
45 SbProject prj = null;
46 SbImage img = null;
47 SbRoi roi = null;
48 SbRes res = null;
49 SbError err = SbError.SB_ERR_NONE;
50
52 Console.WriteLine("Sb Init");
53 err = Sb.Init("../../../sb.lic");
54 if (err != SbError.SB_ERR_NONE)
55 {
56 Console.WriteLine("Sb.Init failed with error " + err);
57 return err;
58 }
59
60
62 Console.WriteLine("Wait for license...");
63 err = Common.WaitLicense();
64 if (err != SbError.SB_ERR_NONE)
65 {
66 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
67 return err;
68 }
69
71 String project_file = "../../solution/surface_tutorial." + SbDefines.SbSolutionExt;
72 SbSolutionInfo solutionInfo = SbSolution.GetInfo(project_file);
73 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
74 {
75 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
76 Console.WriteLine("SbSolution.GetInfo failed");
77 goto FnExit;
78 }
79 else if(solutionInfo.projects.Length == 0)
80 {
81 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
82 Console.WriteLine("SbSolution.GetInfo no projects found");
83 goto FnExit;
84
85 }
86 Console.WriteLine("SbSolutionInfo.GetInfo, found " + solutionInfo.projects.Length + " projects");
87
89 prj = SbProject.Load(project_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
90 if (prj == null || prj.Error() != SbError.SB_ERR_NONE)
91 {
92 err = prj == null ? SbError.SB_ERR_INTERNAL : prj.Error();
93 Console.WriteLine("SbProject.Load failed with error " + err);
94 goto FnExit;
95 }
96
98 img = SbImage.Load("../../dataset/surface_001.png");
99 if (img == null || img.Error() != SbError.SB_ERR_NONE)
100 {
101 Console.WriteLine("SbImage.Load failed");
102 err = img == null ? SbError.SB_ERR_INTERNAL : img.Error();
103 goto FnExit;
104 }
105
107 roi = SbRoi.Create(img.Width(), img.Height());
108 if (roi == null || roi.Error() != SbError.SB_ERR_NONE)
109 {
110 Console.WriteLine("SbRoi.Create failed");
111 err = roi == null ? SbError.SB_ERR_INTERNAL : roi.Error();
112 goto FnExit;
113 }
114
116 err = roi.SetRect(255, new SbRect(0, 0, img.Width(), img.Height()), false);
117 if (err != SbError.SB_ERR_NONE)
118 {
119 Console.WriteLine("SbRoi.SetRect failed with error " + err);
120 goto FnExit;
121 }
122
123 // Set the blob analysis par
124 SbPar par = prj.GetPar();
125 par.blob_par.area.min = 10;
126 par.blob_par.merge_area.min = 20;
127 par.blob_par.blob_rle = 1;
128 par.blob_par.blob_contour = 1;
129 par.blob_par.merge_distance = 10;
130 par.surface_blob_analysis = 1;
131
132 err = prj.SetPar(par);
133 if (err != SbError.SB_ERR_NONE)
134 {
135 Console.WriteLine("SbProject.SetPar failed with error " + err);
136 goto FnExit;
137 }
138
140 err = prj.Detection(img, roi);
141 if (err != SbError.SB_ERR_NONE)
142 {
143 Console.WriteLine("SbProject.Detection failed with error " + err);
144 goto FnExit;
145 }
146
148 res = prj.GetRes(false);
149 if (res == null || prj.Error() != SbError.SB_ERR_NONE)
150 {
151 err = res == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
152 Console.WriteLine("SbProject.GetRes failed with error " + err);
153 goto FnExit;
154 }
155
156 // Print the results
157 Console.WriteLine(res.global.surface.blobs.Length + " defect blobs found:", res.global.surface.blobs.Length);
158 for (int j = 0; j < res.global.surface.blobs.Length; ++j)
159 Console.WriteLine(" ul=(" + res.global.surface.blobs[j].rect.x + "," + res.global.surface.blobs[j].rect.y + "), size=(" + res.global.surface.blobs[j].rect.width + "," + res.global.surface.blobs[j].rect.height + "), area=" + res.global.surface.blobs[j].area);
160
161FnExit:
163 if (prj != null)
164 prj.Dispose();
165 if (img != null)
166 img.Dispose();
167 if (roi != null)
168 roi.Dispose();
169 if (res != null)
170 res.Dispose();
171
172
173 Console.WriteLine("Release SqueezeBrains library");
174 Sb.Release();
175
176 Console.WriteLine("Press ENTER to terminate");
177 Console.ReadKey();
178
179 return err;
180 }
181
182 static void Main(string[] args)
183 {
184 execute();
185 }
186 }
187}
Tutorial 9 - Surface - How to elaborate images
int blob_rle
Set to a value != 0 if you want the rle of the shape each blob.
Definition: cs_blob.h:51
int merge_distance
blobs with a distance inferior or equal to the value are merged together.
Definition: cs_blob.h:68
int blob_contour
Set to a value != 0 if you want the contour of each blob.
Definition: cs_blob.h:52
SbRange merge_area
Range of area after merge, value in pixel.
Definition: cs_blob.h:69
SbRange area
Range of area, value in pixel.
Definition: cs_blob.h:47
SbError Error()
Returns the error code of the last operation. If no error SbError.SB_ERR_NONE is returned.
Defines class
Definition: cs_common.h:258
static const String SbSolutionExt
Solution file extension
Definition: cs_common.h:264
Sb Main Class
Definition: cs_sb.h:374
static SbError Release()
Releases all the resources allocated in the library
static SbError Init(String ^ license_file)
Init the SB Library. The function initializes all the functionalities of the library including the li...
SbImage class that wraps the sb_t_image structure. You must call the Dispose() method to free all the...
Definition: cs_image.h:66
int Height()
Height, in pixel, of the image.
static SbImage Load(String^ filename)
Loads an image from file.
int Width()
Width, in pixel, of the image.
Parameters Class that wraps the sb_t_par structure. You must call the Dispose() method to free all th...
Definition: cs_par.h:657
SbBlobPar blob_par
Blob analysis parameters. Used only for Surface projects
Definition: cs_par.h:687
int surface_blob_analysis
Enable the surface blob analysis
Definition: cs_par.h:717
Project Class You must call the Dispose() method to free all the resources of the returned instance.
Definition: cs_project.h:44
static SbProject Load(String^ solution_file, String^ project_uuid, SbProjectMode mode)
Loads an existing project from file.
SbRes GetRes(bool details)
Retrieves the results of the last processed image
SbError Detection(SbImage ^img, SbRoi ^roi)
The function elaborates the image inside the ROI. For Surface projects the function computes also the...
SbPar GetPar()
Retrieves the project parameters structure.
SbError SetPar(SbPar^ par)
Sets the project parameters.
int min
mininum value
Definition: cs_sb.h:138
Rectangle class that wraps the sb_t_rect structure
Definition: cs_sb.h:224
Class of the results of the image elaboration with the SbProject::Detection method....
Definition: cs_res.h:225
SbResModel global
Global results
Definition: cs_res.h:231
SbSurfaceRes surface
: Surface specific results.
Definition: cs_res.h:132
ROI Class that wraps the sb_t_roi structure. You must call the Dispose() method to free all the resou...
Definition: cs_roi.h:18
static SbRoi Create(int width, int height)
Creates a ROI.
SbError SetRect(int gl, SbRect^ rect, bool reset_roi)
Sets a rectangular ROI.
Solution class
Definition: cs_solution.h:44
static SbSolutionInfo GetInfo(String^ solution_file)
Returns the information contained in the solution_file.
Solution Info Class that wraps the sb_t_solution_info structure
Definition: cs_solution.h:17
array< SbProjectInfo^> projects
Array of the projects information of the solution. sb_t_solution_info.projects
Definition: cs_solution.h:23
array< SbBlob^> blobs
Array of the blobs found
Definition: cs_res.h:69
SbError
Enum error codes
Definition: cs_common.h:13
SbProjectMode
Project loading or saving mode that wraps the sb_t_project_mode enum
Definition: cs_project.h:18
SB Namespace
Definition: cs_common.h:3