-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdemo.cpp
More file actions
174 lines (145 loc) · 5.93 KB
/
Copy pathdemo.cpp
File metadata and controls
174 lines (145 loc) · 5.93 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2014-2015 Isis Innovation Limited and the authors of gSLICr
#include <time.h>
#include <stdio.h>
#include <stdexcept>
#include <cstring>
#include "gSLICr_Lib/gSLICr.h"
#include "NVTimer.h"
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define color_space_XYZ 0
#define color_space_LAB 1
#define color_space_RGB 2
using namespace std;
using namespace cv;
extern "C"
void load_image_from_Mat_to_UChar4(const Mat& inimg, gSLICr::UChar4Image* outimg)
{
gSLICr::Vector4u* outimg_ptr = outimg->GetData(MEMORYDEVICE_CPU);
for (int y = 0; y < outimg->noDims.y;y++)
for (int x = 0; x < outimg->noDims.x; x++)
{
int idx = x + y * outimg->noDims.x;
outimg_ptr[idx].b = inimg.at<Vec3b>(y, x)[0];
outimg_ptr[idx].g = inimg.at<Vec3b>(y, x)[1];
outimg_ptr[idx].r = inimg.at<Vec3b>(y, x)[2];
}
}
extern "C"
void load_image_from_UChar4_to_Mat(const gSLICr::UChar4Image* inimg, Mat& outimg)
{
const gSLICr::Vector4u* inimg_ptr = inimg->GetData(MEMORYDEVICE_CPU);
for (int y = 0; y < inimg->noDims.y; y++)
for (int x = 0; x < inimg->noDims.x; x++)
{
int idx = x + y * inimg->noDims.x;
outimg.at<Vec3b>(y, x)[0] = inimg_ptr[idx].b;
outimg.at<Vec3b>(y, x)[1] = inimg_ptr[idx].g;
outimg.at<Vec3b>(y, x)[2] = inimg_ptr[idx].r;
}
}
static gSLICr::engines::core_engine* build_engine(int img_size_x,
int img_size_y,
int n_segs,
int spixel_size,
float coh_weight,
int n_iters,
int segment_color_space,
bool segment_by_size,
bool enforce_connectivity){
gSLICr::objects::settings my_settings;
my_settings.img_size.x = img_size_x;
my_settings.img_size.y = img_size_y;
my_settings.no_segs = n_segs;
my_settings.spixel_size = spixel_size;
my_settings.coh_weight = coh_weight;
my_settings.no_iters = n_iters;
if(segment_color_space == color_space_XYZ)
my_settings.color_space = gSLICr::XYZ;
else if(segment_color_space == color_space_LAB)
my_settings.color_space = gSLICr::CIELAB;
else if(segment_color_space == color_space_RGB)
my_settings.color_space = gSLICr::RGB;
else
throw std::invalid_argument("Assigned color space is not valid");
if(segment_by_size)
my_settings.seg_method = gSLICr::GIVEN_SIZE;
else
my_settings.seg_method = gSLICr::GIVEN_NUM;
my_settings.do_enforce_connectivity = enforce_connectivity;
return new gSLICr::engines::core_engine(my_settings);
}
extern "C"
void CUDA_gSLICr(unsigned char* image,
int img_size_x,
int img_size_y,
int n_segs,
int spixel_size,
float coh_weight,
int n_iters,
int color_space,
int segment_color_space,
bool segment_by_size,
bool enforce_connectivity,
const char* out_name){
gSLICr::engines::core_engine* gSLICr_engine = build_engine(
img_size_x, img_size_y, n_segs, spixel_size, coh_weight, n_iters,
segment_color_space, segment_by_size, enforce_connectivity);
Size s(img_size_x, img_size_y);
gSLICr::UChar4Image* in_img = new gSLICr::UChar4Image(gSLICr::Vector2i(img_size_x, img_size_y), true, true);
gSLICr::UChar4Image* out_img = new gSLICr::UChar4Image(gSLICr::Vector2i(img_size_x, img_size_y), true, true);
Mat boundry_draw_frame; boundry_draw_frame.create(s, CV_8UC3);
StopWatchInterface *my_timer; sdkCreateTimer(&my_timer);
Mat frame(s, CV_8UC3, (void *) image);
load_image_from_Mat_to_UChar4(frame, in_img);
sdkResetTimer(&my_timer); sdkStartTimer(&my_timer);
gSLICr_engine->Process_Frame(in_img);
sdkStopTimer(&my_timer);
cout<<"\rsegmentation in:["<<sdkGetTimerValue(&my_timer)<<"]ms"<<flush;
char __out_name__[100];
gSLICr_engine->Draw_Segmentation_Result(out_img);
load_image_from_UChar4_to_Mat(out_img, boundry_draw_frame);
sprintf(__out_name__, "seg_%s.jpg", out_name);
imwrite(__out_name__, boundry_draw_frame);
sprintf(__out_name__, "seg_%s.pgm", out_name);
gSLICr_engine->Write_Seg_Res_To_PGM(__out_name__);
delete in_img;
delete out_img;
delete gSLICr_engine;
}
// Run segmentation and write the per-pixel superpixel label map into the
// caller-provided int32 buffer of length img_size_x * img_size_y.
// No files are produced; the caller can convert the label map into contours
// (e.g. with cv2.findContours) for further per-superpixel processing.
extern "C"
void CUDA_gSLICr_segment(unsigned char* image,
int img_size_x,
int img_size_y,
int n_segs,
int spixel_size,
float coh_weight,
int n_iters,
int segment_color_space,
bool segment_by_size,
bool enforce_connectivity,
int* out_labels){
gSLICr::engines::core_engine* gSLICr_engine = build_engine(
img_size_x, img_size_y, n_segs, spixel_size, coh_weight, n_iters,
segment_color_space, segment_by_size, enforce_connectivity);
Size s(img_size_x, img_size_y);
gSLICr::UChar4Image* in_img = new gSLICr::UChar4Image(gSLICr::Vector2i(img_size_x, img_size_y), true, true);
Mat frame(s, CV_8UC3, (void *) image);
load_image_from_Mat_to_UChar4(frame, in_img);
gSLICr_engine->Process_Frame(in_img);
const gSLICr::IntImage* idx_img = gSLICr_engine->Get_Seg_Res();
const int* labels = idx_img->GetData(MEMORYDEVICE_CPU);
std::memcpy(out_labels, labels, sizeof(int) * img_size_x * img_size_y);
delete in_img;
delete gSLICr_engine;
}
int main(){
return 0;
}