/
image.go
268 lines (214 loc) · 6.21 KB
/
image.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package cv
/*
#cgo pkg-config: opencv
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/imgproc/types_c.h>
*/
import "C"
import (
"errors"
//"fmt"
goimage "image"
"image/color"
"unsafe"
)
/**************************************************
* Implementation of the IplImage structure
**************************************************/
type Image struct {
// Private attributes
iplImage *C.IplImage
ptr unsafe.Pointer
colorModel color.Model
imtype MatType
size Size
// Exported attributes
Initialized bool
}
func (img *Image) ScalarAt(pos ...int) Scalar {
n := len(pos)
var s C.CvScalar
if n == 1 {
s = C.cvGet1D(img.ptr, C.int(pos[0]))
} else if n == 2 {
s = C.cvGet2D(img.ptr, C.int(pos[0]), C.int(pos[1]))
} else if n == 3 {
s = C.cvGet3D(img.ptr, C.int(pos[0]), C.int(pos[1]), C.int(pos[2]))
}
return Scalar{float64(s.val[0]), float64(s.val[1]), float64(s.val[2]), float64(s.val[3])}
}
func (img *Image) Release() {
if img.iplImage != nil {
C.cvReleaseImage(&img.iplImage)
img.ptr = nil
img.iplImage = nil
}
img.Initialized = false
}
func (img *Image) Size() Size {
return img.size
}
func (img *Image) Type() MatType {
return img.imtype
}
/******* Image initialization methods *******/
func (img *Image) InitializeAs(other *Image) *Image {
if img.Initialized && img.Size() == other.Size() && img.Type() == other.Type() {
// Do nothing if they are of the same size and type
return img
}
img.Release()
tmp := CreateImage(other.Size(), other.imtype)
*img = *tmp
return img
}
func CreateImage(size Size, imtype MatType) (image *Image) {
var iplImage *C.IplImage
if imtype.Depth == 8 && imtype.ElemType == Unsigned {
iplImage = C.cvCreateImage(C.CvSize{C.int(size.Width), C.int(size.Height)}, C.IPL_DEPTH_8U, C.int(imtype.NumChannels))
}
image, _ = imageFromIplImage(iplImage)
return
}
func (img *Image) Initialize(size Size, imtype MatType) {
if img.Initialized && img.Size() == size && img.imtype == imtype {
return
}
if img.Initialized {
img.Release()
}
*img = *CreateImage(size, imtype)
}
func NewImage() (image *Image) {
image = new(Image)
image.Initialized = false
image.size = Size{0, 0}
return
}
func (image *Image) checkInitialized() {
if !image.Initialized {
panic("Tried to process non-Initialized image")
}
}
/******* Resize *******/
type InterpolationType int
const (
INTER_NN = C.CV_INTER_NN
INTER_LINEAR = C.CV_INTER_LINEAR
INTER_AREA = C.CV_INTER_AREA
INTER_CUBIC = C.CV_INTER_CUBIC
INTER_LANCZOS4 = C.CV_INTER_LANCZOS4
)
func (img *Image) ResizeTo(dest *Image, size Size, interp InterpolationType) {
if !dest.Initialized || dest.Size() != size || dest.imtype != img.imtype {
dest.Release()
*dest = *CreateImage(size, img.imtype)
}
C.cvResize(img.ptr, dest.ptr, C.int(interp))
}
func (img *Image) Resize(size Size, interp InterpolationType) (res *Image) {
res = new(Image)
img.ResizeTo(res, size, interp)
return
}
/******* Rotate *******/
func (img *Image) RotateTo(dest *Image, angle float64) {
dest.InitializeAs(img)
center := C.CvPoint2D32f{C.float(img.Size().Width / 2), C.float(img.Size().Height / 2)}
var rot *C.CvMat = C.cvCreateMat(2, 3, C.CV_32F)
defer C.cvReleaseMat(&rot)
C.cv2DRotationMatrix(center, C.double(angle), C.double(1.0), rot)
C.cvWarpAffine(img.ptr, dest.ptr, rot, C.CV_INTER_LINEAR+C.CV_WARP_FILL_OUTLIERS, C.cvScalarAll(C.double(0.0)))
}
func (img *Image) Rotate(angle float64) (res *Image) {
res = new(Image)
img.RotateTo(res, angle)
return
}
/******* Blur *******/
func (img *Image) GaussianBlurTo(dest *Image, radius int) {
dest.InitializeAs(img)
C.cvSmooth(img.ptr, dest.ptr, C.CV_GAUSSIAN, C.int(radius), C.int(radius), C.double(0.0), C.double(0.0))
}
func (img *Image) GaussianBlur(radius int) (res *Image) {
res = new(Image)
img.GaussianBlurTo(res, radius)
return
}
/******* Copy / Clone *******/
func (img *Image) CopyTo(dest *Image) {
dest.InitializeAs(img)
C.cvCopy(img.ptr, dest.ptr, nil)
}
func (img *Image) Clone() (res *Image) {
res, _ = imageFromIplImage(C.cvCloneImage(img.iplImage))
return
}
/******* LUT *******/
type LUT [][256]uint8
func (img *Image) LUTTo(res *Image, lut *LUT) {
res.InitializeAs(img)
cvlut := CreateImage(Size{256, 1}, CV_8UC1)
defer cvlut.Release()
for i := 0; i < 256; i++ {
C.cvSetReal2D(cvlut.ptr, C.int(0), C.int(i), C.double((*lut)[0][i]))
}
C.cvLUT(img.ptr, res.ptr, cvlut.ptr)
}
/******* Split *******/
type Channels []Image
func (channels *Channels) Release() {
for _, channel := range *channels {
channel.Release()
}
}
func (img *Image) SplitTo(channels *Channels) error {
img.checkInitialized()
n := img.imtype.NumChannels
if n != len(*channels) {
return errors.New("Trying to split image to wrong number of channels")
}
if n == 1 {
C.cvSplit(img.ptr, (*channels)[0].ptr, nil, nil, nil)
} else if n == 2 {
C.cvSplit(img.ptr, (*channels)[0].ptr, (*channels)[1].ptr, nil, nil)
} else if n == 3 {
C.cvSplit(img.ptr, (*channels)[0].ptr, (*channels)[1].ptr, (*channels)[2].ptr, nil)
} else if n == 4 {
C.cvSplit(img.ptr, (*channels)[0].ptr, (*channels)[1].ptr, (*channels)[2].ptr, (*channels)[3].ptr)
}
return nil
}
func (img *Image) Split() (channels Channels) {
img.checkInitialized()
channels = make(Channels, img.imtype.NumChannels)
for i := 0; i < img.imtype.NumChannels; i++ {
channels[i].Initialize(img.Size(), MatType{img.imtype.Depth, img.imtype.ElemType, 1})
}
img.SplitTo(&channels)
return
}
/******* CvtColor *******/
/************************************************
* Implementation of Go's Image interface
************************************************/
func (image *Image) ColorModel() color.Model {
return image.colorModel
}
func (image *Image) At(x, y int) (res color.Color) {
scalar := image.ScalarAt(y, x)
if image.imtype.NumChannels == 1 {
res = color.Gray{uint8(scalar[0])}
} else {
// While OpenCV represents images as BGR, we ensure that any interaction with images results in an RGB image
// (for example in cv.LoadImage)
res = color.NRGBA{uint8(scalar[0]), uint8(scalar[1]), uint8(scalar[2]), 255}
}
return
}
func (image *Image) Bounds() goimage.Rectangle {
p0 := goimage.Point{0, 0}
p1 := goimage.Point{image.Size().Width, image.Size().Height}
return goimage.Rectangle{p0, p1}
}