SqueezeBrains SDK 1.18
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 SbPar par = null;
50 SbError err = SbError.SB_ERR_NONE;
51
53 Console.WriteLine("Sb Init");
54 err = Sb.Init("../../../sb.lic");
55 if (err != SbError.SB_ERR_NONE)
56 {
57 Console.WriteLine("Sb.Init failed with error " + err);
58 return err;
59 }
60
61
63 Console.WriteLine("Wait for license...");
64 err = Common.WaitLicense();
65 if (err != SbError.SB_ERR_NONE)
66 {
67 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
68 return err;
69 }
70
72 String project_file = "../../solution/surface_tutorial." + SbDefines.SbSolutionExt;
73 SbSolutionInfo solutionInfo = SbSolution.GetInfo(project_file);
74 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
75 {
76 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
77 Console.WriteLine("SbSolution.GetInfo failed");
78 goto FnExit;
79 }
80 else if(solutionInfo.projects.Length == 0)
81 {
82 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
83 Console.WriteLine("SbSolution.GetInfo no projects found");
84 goto FnExit;
85
86 }
87 Console.WriteLine("SbSolutionInfo.GetInfo, found " + solutionInfo.projects.Length + " projects");
88
90 prj = SbProject.Load(project_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
91 if (prj == null || prj.Error() != SbError.SB_ERR_NONE)
92 {
93 err = prj == null ? SbError.SB_ERR_INTERNAL : prj.Error();
94 Console.WriteLine("SbProject.Load failed with error " + err);
95 goto FnExit;
96 }
97
99 img = SbImage.Load("../../dataset/surface_001.png");
100 if (img == null || img.Error() != SbError.SB_ERR_NONE)
101 {
102 Console.WriteLine("SbImage.Load failed");
103 err = img == null ? SbError.SB_ERR_INTERNAL : img.Error();
104 goto FnExit;
105 }
106
108 roi = SbRoi.Create(img.Width(), img.Height());
109 if (roi == null || roi.Error() != SbError.SB_ERR_NONE)
110 {
111 Console.WriteLine("SbRoi.Create failed");
112 err = roi == null ? SbError.SB_ERR_INTERNAL : roi.Error();
113 goto FnExit;
114 }
115
117 err = roi.SetRect(255, new SbRect(0, 0, img.Width(), img.Height()), false);
118 if (err != SbError.SB_ERR_NONE)
119 {
120 Console.WriteLine("SbRoi.SetRect failed with error " + err);
121 goto FnExit;
122 }
123
124 // Set the blob analysis par
125 par = prj.GetPar();
126 par.blob_par.area.min = 10;
127 par.blob_par.merge_area.min = 20;
128 par.blob_par.blob_rle = 1;
129 par.blob_par.blob_contour = 1;
130 par.blob_par.merge_distance = 10;
131 par.surface_blob_analysis = 1;
132
133 err = prj.SetPar(par);
134 if (err != SbError.SB_ERR_NONE)
135 {
136 Console.WriteLine("SbProject.SetPar failed with error " + err);
137 goto FnExit;
138 }
139
141 err = prj.Detection(img, roi);
142 if (err != SbError.SB_ERR_NONE)
143 {
144 Console.WriteLine("SbProject.Detection failed with error " + err);
145 goto FnExit;
146 }
147
149 res = prj.GetRes(false);
150 if (res == null || prj.Error() != SbError.SB_ERR_NONE)
151 {
152 err = res == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
153 Console.WriteLine("SbProject.GetRes failed with error " + err);
154 goto FnExit;
155 }
156
157 // Print the results
158 Console.WriteLine(res.global.surface.blobs.Length + " defect blobs found:", res.global.surface.blobs.Length);
159 for (int j = 0; j < res.global.surface.blobs.Length; ++j)
160 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);
161
162FnExit:
164 if (prj != null)
165 prj.Dispose();
166 if (img != null)
167 img.Dispose();
168 if (roi != null)
169 roi.Dispose();
170 if (par != null)
171 par.Dispose();
172 if (res != null)
173 res.Dispose();
174
175
176 Console.WriteLine("Release SqueezeBrains library");
177 Sb.Release();
178
179 Console.WriteLine("Press ENTER to terminate");
180 Console.ReadKey();
181
182 return err;
183 }
184
185 static void Main(string[] args)
186 {
187 execute();
188 }
189 }
190}
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:309
static const String SbSolutionExt
Solution file extension
Definition: cs_common.h:315
Sb Main Class
Definition: cs_sb.h:401
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:751
SbBlobPar blob_par
Blob analysis parameters. Used only for Surface projects
Definition: cs_par.h:781
int surface_blob_analysis
Enable the surface blob analysis
Definition: cs_par.h:811
Project Class You must call the Dispose() method to free all the resources of the returned instance.
Definition: cs_project.h:51
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:128
Rectangle class that wraps the sb_t_rect structure
Definition: cs_sb.h:214
Class of the results of the image elaboration with the SbProject::Detection method....
Definition: cs_res.h:229
SbResModel global
Global results
Definition: cs_res.h:235
SbSurfaceRes surface
: Surface specific results.
Definition: cs_res.h:136
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:73
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