|
| 1 | +package opencv |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + // "fmt" |
| 6 | + "path" |
| 7 | + "runtime" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func GetFocusFromFile(file string) (err error, focus float64) { |
| 12 | + src := LoadImage(file) |
| 13 | + |
| 14 | + if src == nil { |
| 15 | + return errors.New("LoadImage fail"), focus |
| 16 | + } |
| 17 | + |
| 18 | + dst := CreateImage(src.Width(), src.Height(), src.Depth(), src.Channels()) |
| 19 | + |
| 20 | + Laplace(src, dst, 3) |
| 21 | + |
| 22 | + _, sigma := MeanStdDevWithMask(dst, nil) |
| 23 | + |
| 24 | + focus = sigma.Val()[0] * sigma.Val()[0] |
| 25 | + return err, focus |
| 26 | +} |
| 27 | + |
| 28 | +func TestBlurredImgs(t *testing.T) { |
| 29 | + |
| 30 | + // case different blur on image |
| 31 | + { |
| 32 | + _, currentfile, _, _ := runtime.Caller(0) |
| 33 | + |
| 34 | + fileBlurryFalse := path.Join(path.Dir(currentfile), "../images/blurry-false.png") |
| 35 | + err, focusBlurryFalse := GetFocusFromFile(fileBlurryFalse) |
| 36 | + if err != nil { |
| 37 | + t.Error("err get focus for blurry-false: ", err) |
| 38 | + } |
| 39 | + |
| 40 | + fileBlurryTrue := path.Join(path.Dir(currentfile), "../images/blurry-true.png") |
| 41 | + err, focusBlurryTrue := GetFocusFromFile(fileBlurryTrue) |
| 42 | + if err != nil { |
| 43 | + t.Error("err get focus for blurry-true: ", err) |
| 44 | + } |
| 45 | + |
| 46 | + if focusBlurryTrue > focusBlurryFalse { |
| 47 | + t.Error("err value focuses, blurry-true should be lesser blurry-false: ", err) |
| 48 | + } |
| 49 | + // fmt.Println("blyrry-false: ", focusBlurryFalse) |
| 50 | + // fmt.Println("blyrry-true: ", focusBlurryTrue) |
| 51 | + |
| 52 | + } |
| 53 | +} |
0 commit comments