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
|
//
// ImageUtils.swift
// iTexSnip
//
// Created by Navan Chauhan on 10/13/24.
//
import AppKit
import CoreImage
import Foundation
let IMAGE_MEAN: CGFloat = 0.9545467
let IMAGE_STD: CGFloat = 0.15394445
let FIXED_IMG_SIZE: CGFloat = 448
let IMG_CHANNELS: Int = 1
let MIN_HEIGHT: CGFloat = 12
let MIN_WIDTH: CGFloat = 30
func loadImage(from urlString: String) -> NSImage? {
guard let url = URL(string: urlString), let imageData = try? Data(contentsOf: url) else {
return nil
}
return NSImage(data: imageData)
}
func nsImageToCIImage(_ image: NSImage) -> CIImage? {
guard let data = image.tiffRepresentation,
let bitmapImage = NSBitmapImageRep(data: data),
let cgImage = bitmapImage.cgImage
else {
return nil
}
return CIImage(cgImage: cgImage)
}
func trimWhiteBorder(image: CIImage) -> CIImage? {
let context = CIContext()
guard let cgImage = context.createCGImage(image, from: image.extent) else {
return nil
}
let width = cgImage.width
let height = cgImage.height
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * width
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
var pixelData = [UInt8](repeating: 0, count: height * bytesPerRow)
guard
let contextRef = CGContext(
data: &pixelData,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: bytesPerRow,
space: colorSpace,
bitmapInfo: bitmapInfo
)
else {
return nil
}
contextRef.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
let whitePixel: [UInt8] = [255, 255, 255, 255]
var minX = width
var minY = height
var maxX: Int = 0
var maxY: Int = 0
for y in 0..<height {
for x in 0..<width {
let pixelIndex = (y * bytesPerRow) + (x * bytesPerPixel)
let pixel = Array(pixelData[pixelIndex..<(pixelIndex + 4)])
if pixel != whitePixel {
if x < minX { minX = x }
if x > maxX { maxX = x }
if y < minY { minY = y }
if y > maxY { maxY = y }
}
}
}
if minX == width || minY == height || maxX == 0 || maxY == 0 {
return image
}
let croppedRect = CGRect(
x: CGFloat(minX), y: CGFloat(minY), width: CGFloat(maxX - minX), height: CGFloat(maxY - minY))
return image.cropped(to: croppedRect)
}
func addWhiteBorder(to image: CIImage, maxSize: CGFloat) -> CIImage {
let clampedMaxSize = max(0, min(maxSize, CGFloat(UInt32.max)))
let randomPadding = (0..<4).map { _ in CGFloat(arc4random_uniform(UInt32(clampedMaxSize))) }
var xPadding = randomPadding[0] + randomPadding[2]
var yPadding = randomPadding[1] + randomPadding[3]
if xPadding + image.extent.width < MIN_WIDTH {
let compensateWidth = (MIN_WIDTH - (xPadding + image.extent.width)) * 0.5 + 1
xPadding += compensateWidth
}
if yPadding + image.extent.height < MIN_HEIGHT {
let compensateHeight = (MIN_HEIGHT - (yPadding + image.extent.height)) * 0.5 + 1
yPadding += compensateHeight
}
let padFilter = CIFilter(name: "CICrop")!
let paddedRect = CGRect(
x: image.extent.origin.x - randomPadding[0],
y: image.extent.origin.y - randomPadding[1],
width: image.extent.width + xPadding,
height: image.extent.height + yPadding)
padFilter.setValue(image, forKey: kCIInputImageKey)
padFilter.setValue(CIVector(cgRect: paddedRect), forKey: "inputRectangle")
return padFilter.outputImage ?? image
}
func padding(images: [CIImage], requiredSize: CGFloat) -> [CIImage] {
return images.map { image in
let widthPadding = requiredSize - image.extent.width
let heightPadding = requiredSize - image.extent.height
return addWhiteBorder(to: image, maxSize: max(widthPadding, heightPadding))
}
}
func resizeIfLarger(image: CIImage, maxSize: CGFloat) -> CIImage {
let width = image.extent.width
let height = image.extent.height
if width <= maxSize && height <= maxSize {
return image
}
let aspectRatio = width / height
var newWidth: CGFloat
var newHeight: CGFloat
if aspectRatio > 1 {
newWidth = maxSize
newHeight = maxSize / aspectRatio
} else {
newHeight = maxSize
newWidth = maxSize * aspectRatio
}
let scaleX = newWidth / width
let scaleY = newHeight / height
let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
return image.transformed(by: transform)
}
func inferenceTransform(images: [NSImage]) -> [CIImage] {
let ciImages = images.compactMap { nsImageToCIImage($0) }
// let trimmedImages = ciImages.compactMap { trimWhiteBorder(image: $0) }
// let resizedImages = trimmedImages.map { resizeIfLarger(image: $0, maxSize: FIXED_IMG_SIZE) }
// let paddedImages = padding(images: trimmedImages, requiredSize: FIXED_IMG_SIZE)
let resizedImages = ciImages.map { resizeIfLarger(image: $0, maxSize: FIXED_IMG_SIZE) }
let paddedImages = padding(images: resizedImages, requiredSize: FIXED_IMG_SIZE)
return paddedImages
}
func ciImageToFloatArray(_ image: CIImage, size: CGSize) -> [Float] {
let context = CIContext()
guard let cgImage = context.createCGImage(image, from: image.extent) else {
return []
}
let width = Int(size.width)
let height = Int(size.height)
var pixelData = [UInt8](repeating: 0, count: width * height) // Use UInt8 for grayscale
let colorSpace = CGColorSpaceCreateDeviceGray()
guard
let contextRef = CGContext(
data: &pixelData,
width: width,
height: height,
bitsPerComponent: 8,
bytesPerRow: width,
space: colorSpace,
bitmapInfo: CGImageAlphaInfo.none.rawValue
)
else {
return []
}
contextRef.draw(cgImage, in: CGRect(x: 0, y: 0, width: CGFloat(width), height: CGFloat(height)))
return pixelData.map { Float($0) / 255.0 }
}
|