docs: add comments to rotation preprocessing
This commit is contained in:
@@ -743,23 +743,31 @@ func preprocessRotation(img image.Image) ([]float32, error) {
|
|||||||
bounds := img.Bounds()
|
bounds := img.Bounds()
|
||||||
w, h := bounds.Dx(), bounds.Dy()
|
w, h := bounds.Dx(), bounds.Dy()
|
||||||
|
|
||||||
|
// 原版要求正方形图片,这里我们也先裁剪成正方形
|
||||||
|
// 取较短边作为正方形边长
|
||||||
size := w
|
size := w
|
||||||
if h < w {
|
if h < w {
|
||||||
size = h
|
size = h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 中心裁剪成正方形
|
||||||
cropX := (w - size) / 2
|
cropX := (w - size) / 2
|
||||||
cropY := (h - size) / 2
|
cropY := (h - size) / 2
|
||||||
cropped := imaging.Crop(img, image.Rect(cropX, cropY, cropX+size, cropY+size))
|
cropped := imaging.Crop(img, image.Rect(cropX, cropY, cropX+size, cropY+size))
|
||||||
|
|
||||||
|
// 原版: new_size = int(h / sqrt(2)),去掉边角
|
||||||
sqrt2 := math.Sqrt(2.0)
|
sqrt2 := math.Sqrt(2.0)
|
||||||
newSize := int(float64(size) / sqrt2)
|
newSize := int(float64(size) / sqrt2)
|
||||||
offset := (size - newSize) / 2
|
offset := (size - newSize) / 2
|
||||||
|
|
||||||
|
// 再次中心裁剪(去掉边角区域)
|
||||||
centerCropped := imaging.Crop(cropped, image.Rect(offset, offset, offset+newSize, offset+newSize))
|
centerCropped := imaging.Crop(cropped, image.Rect(offset, offset, offset+newSize, offset+newSize))
|
||||||
|
|
||||||
|
// resize 到 224x224
|
||||||
resized := imaging.Resize(centerCropped, 224, 224, imaging.Lanczos)
|
resized := imaging.Resize(centerCropped, 224, 224, imaging.Lanczos)
|
||||||
rgb := imaging.Clone(resized)
|
rgb := imaging.Clone(resized)
|
||||||
|
|
||||||
|
// ImageNet 标准化
|
||||||
mean := [3]float32{0.485, 0.456, 0.406}
|
mean := [3]float32{0.485, 0.456, 0.406}
|
||||||
std := [3]float32{0.229, 0.224, 0.225}
|
std := [3]float32{0.229, 0.224, 0.225}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user