-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimage_converter.go
153 lines (125 loc) · 4.11 KB
/
image_converter.go
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
package mlpack
/*
#cgo CFLAGS: -I./capi -Wall
#cgo LDFLAGS: -L. -lmlpack_go_image_converter
#include <capi/image_converter.h>
#include <stdlib.h>
*/
import "C"
import "gonum.org/v1/gonum/mat"
type ImageConverterOptionalParam struct {
Channels int
Dataset *mat.Dense
Height int
Quality int
Save bool
Verbose bool
Width int
}
func ImageConverterOptions() *ImageConverterOptionalParam {
return &ImageConverterOptionalParam{
Channels: 0,
Dataset: nil,
Height: 0,
Quality: 90,
Save: false,
Verbose: false,
Width: 0,
}
}
/*
This utility takes an image or an array of images and loads them to a matrix.
You can optionally specify the height "Height" width "Width" and channel
"Channels" of the images that needs to be loaded; otherwise, these parameters
will be automatically detected from the image.
There are other options too, that can be specified such as "Quality".
You can also provide a dataset and save them as images using "Dataset" and
"Save" as an parameter.
An example to load an image :
// Initialize optional parameters for ImageConverter().
param := mlpack.ImageConverterOptions()
param.Height = 256
param.Width = 256
param.Channels = 3
Y := mlpack.ImageConverter(X, param)
An example to save an image is :
// Initialize optional parameters for ImageConverter().
param := mlpack.ImageConverterOptions()
param.Height = 256
param.Width = 256
param.Channels = 3
param.Dataset = Y
param.Save = true
_ := mlpack.ImageConverter(X, param)
Input parameters:
- input ([]string): Image filenames which have to be loaded/saved.
- Channels (int): Number of channels in the image. Default value 0.
- Dataset (mat.Dense): Input matrix to save as images.
- Height (int): Height of the images. Default value 0.
- Quality (int): Compression of the image if saved as jpg (0-100).
Default value 90.
- Save (bool): Save a dataset as images.
- Verbose (bool): Display informational messages and the full list of
parameters and timers at the end of execution.
- Width (int): Width of the image. Default value 0.
Output parameters:
- output (mat.Dense): Matrix to save images data to, Onlyneeded if you
are specifying 'save' option.
*/
func ImageConverter(input []string, param *ImageConverterOptionalParam) (*mat.Dense) {
params := getParams("image_converter")
timers := getTimers()
disableBacktrace()
disableVerbose()
// Detect if the parameter was passed; set if so.
setParamVecString(params, "input", input)
setPassed(params, "input")
// Detect if the parameter was passed; set if so.
if param.Channels != 0 {
setParamInt(params, "channels", param.Channels)
setPassed(params, "channels")
}
// Detect if the parameter was passed; set if so.
if param.Dataset != nil {
gonumToArmaMat(params, "dataset", param.Dataset, false)
setPassed(params, "dataset")
}
// Detect if the parameter was passed; set if so.
if param.Height != 0 {
setParamInt(params, "height", param.Height)
setPassed(params, "height")
}
// Detect if the parameter was passed; set if so.
if param.Quality != 90 {
setParamInt(params, "quality", param.Quality)
setPassed(params, "quality")
}
// Detect if the parameter was passed; set if so.
if param.Save != false {
setParamBool(params, "save", param.Save)
setPassed(params, "save")
}
// Detect if the parameter was passed; set if so.
if param.Verbose != false {
setParamBool(params, "verbose", param.Verbose)
setPassed(params, "verbose")
enableVerbose()
}
// Detect if the parameter was passed; set if so.
if param.Width != 0 {
setParamInt(params, "width", param.Width)
setPassed(params, "width")
}
// Mark all output options as passed.
setPassed(params, "output")
// Call the mlpack program.
C.mlpackImageConverter(params.mem, timers.mem)
// Initialize result variable and get output.
var outputPtr mlpackArma
output := outputPtr.armaToGonumMat(params, "output")
// Clean memory.
cleanParams(params)
cleanTimers(timers)
// Return output(s).
return output
}