-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathslic.h
More file actions
101 lines (85 loc) · 2.83 KB
/
slic.h
File metadata and controls
101 lines (85 loc) · 2.83 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
#ifndef SLIC_H
#define SLIC_H
#include <CL/cl.h>
#include <opencv2/opencv.hpp>
// #include <opencv2/ximgproc.hpp>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <chrono>
#include <stdexcept>
#include <random>
// OpenCL kernel source code
extern const char* kernelSource1;
extern const char* kernelSource2;
extern const char* kernelSource3;
extern const char* kernelSource4;
extern const char* kernelSource5;
extern const char* kernelSource6;
extern const char* kernelSource7;
extern const char* kernelSource8;
extern const char* kernelSource9;
class SLICSuperpixels {
private:
// Member variables
int width;
int height;
int numSuperpixels;
int numPixels;
int regionSize;
size_t globalSize;
size_t globalSize2[2];
size_t globalSize3;
float compactness;
int iterations;
bool processTwoMaps;
// OpenCL variables
cl_context context;
cl_command_queue queue;
cl_program program;
cl_kernel initKernel, assignKernel, updateKernel, accumulateKernel, finalizeKernel;
// Kernels for single map case
cl_kernel averageKernel;
cl_kernel computeKernel;
cl_kernel assignAvgKernel;
// Kernels for two-map case
cl_kernel averageKernel2;
cl_kernel computeKernel2;
cl_kernel assignAvgKernel2;
// Device buffers
cl_mem d_clusters = nullptr;
cl_mem d_accum = nullptr;
cl_mem d_counts = nullptr;
// Buffers for single map case
cl_mem d_pixel_counts_per_superpixel = nullptr;
cl_mem d_value_sum_per_superpixel = nullptr;
cl_mem d_averages = nullptr;
// Buffers for two-map case
cl_mem d_value_sum_per_superpixel1 = nullptr;
cl_mem d_value_sum_per_superpixel2 = nullptr;
cl_mem d_pixel_counts_per_superpixel1 = nullptr;
cl_mem d_pixel_counts_per_superpixel2 = nullptr;
cl_mem d_averages1 = nullptr;
cl_mem d_averages2 = nullptr;
cl_device_id device;
// Helper function to load OpenCL kernel from string
std::string loadKernelSource();
// Initialize OpenCL
void initOpenCL();
// Clean up OpenCL resources
void CleanupOpenCL();
public:
SLICSuperpixels(cl_context context, cl_command_queue queue, cl_device_id device, float compactness = 13.0f, int numSuperpixels = 1000, int iterations = 1);
~SLICSuperpixels();
// Allocate all needed buffers except d_image
void allocateBuffers(int width_, int height_, bool processTwoMaps_);
// Set all kernel arguments, using external d_image
void setKernelArgs(cl_mem d_image, cl_mem d_labels, cl_mem d_opticalFlowMap1, cl_mem d_opticalFlowMap2, cl_mem d_avgImage1, cl_mem d_avgImage2);
// Release device buffers
void releaseBuffers();
// Run kernels and process image, including superpixel averaging and assignment
void processImage();
void processTwoImages();
};
#endif // SLIC_H