-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx3LoadAndSave.scala
91 lines (70 loc) · 2.55 KB
/
Ex3LoadAndSave.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter01
import opencv_cookbook.OpenCVUtils.toBufferedImage
import org.bytedeco.javacv.CanvasFrame
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._
import javax.swing.WindowConstants
/**
* Example of reading, saving, displaying, and drawing on an image.
*/
object Ex3LoadAndSave extends App {
// read the input image as a gray-scale image
val image = imread("data/puppy.bmp", IMREAD_COLOR)
if (image.empty()) {
// error handling
// no image has been created...
// possibly display an error message
// and quit the application
println("Error reading image...")
System.exit(0)
}
println("This image is " + image.rows + " x " + image.cols)
// Create image window named "My Image".
val canvas = new CanvasFrame("My Image", 1)
// Request closing of the application when the image window is closed
canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
// Show image on window
canvas.showImage(toBufferedImage(image))
// we create another empty image
val result = new Mat(); // we create another empty image
// positive for horizontal
// 0 for vertical,
// negative for both
flip(image, result, 1)
// the output window
val canvas2 = new CanvasFrame("Output Image", 1)
// Request closing of the application when the image window is closed
canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
// Show image on window
canvas2.showImage(toBufferedImage(result))
// save result
imwrite("output.bmp", result)
// create another image window
val canvas3 = new CanvasFrame("Drawing on an Image", 1)
val image3 = image.clone()
circle(image3, // destination image
new Point(155, 110), // center coordinate
65, // radius
new Scalar(0), // color (here black)
3, // thickness
8, // 8-connected line
0) // shift
putText(image3, // destination image
"This is a dog.", // text
new Point(40, 200), // text position
FONT_HERSHEY_PLAIN, // font type
2.0, // font scale
new Scalar(255), // text color (here white)
2, // text thickness
8, // Line type.
false) //When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
canvas3.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
canvas3.showImage(toBufferedImage(image3))
}