Skip to content

Commit

Permalink
Fix threshold on sub-images (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
leizor authored Jul 27, 2024
1 parent d5d9042 commit 461615a
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 5 deletions.
Binary file added res/threshold/otsuThreshBinCropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/threshold/otsuThreshBinInvCropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/threshold/otsuThreshToZeroCropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/threshold/otsuThreshToZeroInvCropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/threshold/otsuThreshTruncCropped.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions threshold/threshold.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package threshold

import (
"errors"
"github.com/ernyoke/imger/histogram"
"github.com/ernyoke/imger/utils"
"image"
"image/color"

"github.com/ernyoke/imger/histogram"
"github.com/ernyoke/imger/utils"
)

// Method is an enum type for global threshold methods
Expand Down Expand Up @@ -176,17 +177,19 @@ func OtsuThreshold(img *image.Gray, method Method) (*image.Gray, error) {
func threshold(img *image.Gray, setPixel func(*image.Gray, int, int)) *image.Gray {
size := img.Bounds().Size()
gray := image.NewGray(img.Bounds())
offset := img.Bounds().Min
utils.ParallelForEachPixel(size, func(x, y int) {
setPixel(gray, x, y)
setPixel(gray, x+offset.X, y+offset.Y)
})
return gray
}

func threshold16(img *image.Gray16, setPixel16 func(*image.Gray16, int, int)) *image.Gray16 {
size := img.Bounds().Size()
gray := image.NewGray16(img.Bounds())
offset := img.Bounds().Min
utils.ParallelForEachPixel(size, func(x, y int) {
setPixel16(gray, x, y)
setPixel16(gray, x+offset.X, y+offset.Y)
})
return gray
}
Expand Down
26 changes: 25 additions & 1 deletion threshold/threshold_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package threshold

import (
"github.com/ernyoke/imger/imgio"
"fmt"
"image"
"testing"

"github.com/ernyoke/imger/imgio"
)

// -----------------------------Acceptance tests------------------------------------
Expand Down Expand Up @@ -107,4 +109,26 @@ func Test_Acceptance_OtsuThreshold(t *testing.T) {
tearDownTestCase(t, thresh, "../res/threshold/otsuThreshBin.jpg")
}

func Test_Acceptance_OtsuThreshold_Cropped(t *testing.T) {
thresholdMethods := map[string]Method{
"Bin": ThreshBinary,
"BinInv": ThreshBinaryInv,
"Trunc": ThreshTrunc,
"ToZero": ThreshToZero,
"ToZeroInv": ThreshToZeroInv,
}

for name, meth := range thresholdMethods {
t.Run(name, func(t *testing.T) {
gray := setupTestCaseOtsu(t)
cropped := gray.SubImage(image.Rect(100, 100, gray.Bounds().Max.X-100, gray.Bounds().Max.Y-100))
thresh, err := OtsuThreshold(cropped.(*image.Gray), meth)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tearDownTestCase(t, thresh, fmt.Sprintf("../res/threshold/otsuThresh%sCropped.jpg", name))
})
}
}

//---------------------------------------------------------------------------------

0 comments on commit 461615a

Please sign in to comment.