2026-07-22 16:01:28 +08:00
|
|
|
package spike
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
|
|
func TestLetterboxBGRToNCHWPlacesPixelInScaledImage(t *testing.T) {
|
|
|
|
|
// One bright BGR pixel in a 2:1 frame should be resized into the centred
|
|
|
|
|
// 640x320 image area. The remaining top/bottom area must stay black.
|
|
|
|
|
frame := []byte{0, 0, 0, 255, 0, 0}
|
|
|
|
|
|
|
|
|
|
input, err := LetterboxBGRToNCHW(frame, 2, 1, 640)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("LetterboxBGRToNCHW returned an error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const plane = 640 * 640
|
|
|
|
|
if got := len(input); got != 3*plane {
|
|
|
|
|
t.Fatalf("input length = %d, want %d", got, 3*plane)
|
|
|
|
|
}
|
2026-07-22 16:18:16 +08:00
|
|
|
padding := float32(114.0 / 255.0)
|
|
|
|
|
if got := input[2*plane+159*640+320]; got != padding {
|
|
|
|
|
t.Fatalf("top padding blue channel = %v, want %v", got, padding)
|
2026-07-22 16:01:28 +08:00
|
|
|
}
|
|
|
|
|
if got := input[0*plane+320*640+320]; got != 0 {
|
|
|
|
|
t.Fatalf("red channel = %v, want 0", got)
|
|
|
|
|
}
|
|
|
|
|
if got := input[1*plane+320*640+320]; got != 0 {
|
|
|
|
|
t.Fatalf("green channel = %v, want 0", got)
|
|
|
|
|
}
|
2026-07-22 16:18:16 +08:00
|
|
|
if got := input[2*plane+320*640+320]; got != float32(128.0/255.0) {
|
|
|
|
|
t.Fatalf("blue channel = %v, want cv2 INTER_LINEAR value 128/255", got)
|
2026-07-22 16:01:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLetterboxBGRToNCHWRejectsIncorrectFrameSize(t *testing.T) {
|
|
|
|
|
_, err := LetterboxBGRToNCHW([]byte{0}, 2, 1, 640)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("LetterboxBGRToNCHW accepted a truncated BGR frame")
|
|
|
|
|
}
|
|
|
|
|
}
|