-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_support.go
More file actions
114 lines (94 loc) · 2.73 KB
/
image_support.go
File metadata and controls
114 lines (94 loc) · 2.73 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
package face
import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"os"
"path/filepath"
"strings"
"gocv.io/x/gocv"
)
// SupportedImageFormats lists all supported image formats
var SupportedImageFormats = []string{
".jpg", ".jpeg", // JPEG
".png", // PNG
".bmp", // Bitmap
".tif", ".tiff", // TIFF
".webp", // WebP
".gif", // GIF
}
// IsSupportedImageFormat checks if the file extension is supported
func IsSupportedImageFormat(filename string) bool {
ext := strings.ToLower(filepath.Ext(filename))
for _, supportedExt := range SupportedImageFormats {
if ext == supportedExt {
return true
}
}
return false
}
// LoadImage loads an image from file path
// Supports: JPG, PNG, BMP, TIFF, WebP, GIF
func LoadImage(filepath string) (gocv.Mat, error) {
if !IsSupportedImageFormat(filepath) {
return gocv.Mat{}, fmt.Errorf("unsupported image format: %s", filepath)
}
img := gocv.IMRead(filepath, gocv.IMReadColor)
if img.Empty() {
return gocv.Mat{}, fmt.Errorf("failed to load image: %s", filepath)
}
return img, nil
}
// LoadImageFromBytes loads an image from byte slice
func LoadImageFromBytes(data []byte) (gocv.Mat, error) {
img, err := gocv.IMDecode(data, gocv.IMReadColor)
if err != nil {
return gocv.Mat{}, fmt.Errorf("failed to decode image: %v", err)
}
if img.Empty() {
return gocv.Mat{}, fmt.Errorf("decoded image is empty")
}
return img, nil
}
// LoadImageFromStdImage converts standard Go image.Image to gocv.Mat
func LoadImageFromStdImage(img image.Image) (gocv.Mat, error) {
bounds := img.Bounds()
width := bounds.Dx()
height := bounds.Dy()
mat := gocv.NewMatWithSize(height, width, gocv.MatTypeCV8UC3)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
r, g, b, _ := img.At(x, y).RGBA()
// Convert from 16-bit to 8-bit
mat.SetUCharAt(y, x*3+2, uint8(r>>8)) // R
mat.SetUCharAt(y, x*3+1, uint8(g>>8)) // G
mat.SetUCharAt(y, x*3, uint8(b>>8)) // B
}
}
return mat, nil
}
// SaveImage saves a Mat to file
func SaveImage(filepath string, img gocv.Mat) error {
if !IsSupportedImageFormat(filepath) {
return fmt.Errorf("unsupported image format: %s", filepath)
}
success := gocv.IMWrite(filepath, img)
if !success {
return fmt.Errorf("failed to save image: %s", filepath)
}
return nil
}
// GetImageInfo returns information about an image file
func GetImageInfo(filepath string) (width, height, channels int, err error) {
if _, err := os.Stat(filepath); os.IsNotExist(err) {
return 0, 0, 0, fmt.Errorf("file does not exist: %s", filepath)
}
img := gocv.IMRead(filepath, gocv.IMReadColor)
if img.Empty() {
return 0, 0, 0, fmt.Errorf("failed to read image: %s", filepath)
}
defer img.Close()
return img.Cols(), img.Rows(), img.Channels(), nil
}