-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx4WatershedSegmentation.scala
69 lines (55 loc) · 1.74 KB
/
Ex4WatershedSegmentation.scala
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
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter05
import java.io.File
import opencv_cookbook.OpenCVUtils._
import org.bytedeco.opencv.global.opencv_core._
import org.bytedeco.opencv.global.opencv_imgcodecs._
import org.bytedeco.opencv.global.opencv_imgproc._
import org.bytedeco.opencv.opencv_core._
/**
* Example from section "Segmenting images using watersheds".
*/
object Ex4WatershedSegmentation extends App {
// Read input image
val image = loadAndShowOrExit(new File("data/group.jpg"), IMREAD_COLOR)
val binary = loadAndShowOrExit(new File("data/binary.bmp"), IMREAD_GRAYSCALE)
// Eliminate noise and smaller objects, repeat erosion 6 times
val fg = new Mat()
erode(binary, fg,
new Mat() /* 3x3 square */ ,
new Point(-1, -1),
6 /* iterations */ ,
BORDER_CONSTANT,
morphologyDefaultBorderValue)
show(fg, "Foreground")
// Identify image pixels pixels objects
val bg = new Mat()
dilate(binary, bg,
new Mat() /* 3x3 square */ ,
new Point(-1, -1),
6 /* iterations */ ,
BORDER_CONSTANT,
morphologyDefaultBorderValue)
show(bg, "Dilated")
threshold(bg, bg,
1 /* threshold */ ,
128 /* max value */ ,
THRESH_BINARY_INV)
show(bg, "Background")
// Create marker image
val markers = new Mat(binary.size(), CV_8U, new Scalar(0d))
add(fg, bg, markers)
show(markers, "Markers")
val segmenter = new WatershedSegmenter()
segmenter.setMarkers(markers)
val segmentMarkers = segmenter.process(image)
show(segmentMarkers, "segmentMarkers")
val segmentation = segmenter.segmentation
show(segmentation, "Segmentation")
val watershed = segmenter.watersheds
show(watershed, "Watersheds")
}