-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorphology.zig
More file actions
75 lines (63 loc) · 3.01 KB
/
morphology.zig
File metadata and controls
75 lines (63 loc) · 3.01 KB
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
const std = @import("std");
const cv = @import("zopencv");
pub fn main() !void {
std.debug.print("=== Morphology Operations Example ===\n", .{});
std.debug.print("Demonstrates erosion, dilation, opening, closing, gradient, tophat, blackhat\n\n", .{});
const image_path = "testdata/images/lena.jpg";
var img = cv.imgcodecs.imread(image_path, .color) catch {
std.debug.print("❌ Failed to load: {s}\n", .{image_path});
return;
};
defer img.deinit();
std.debug.print("✅ Loaded: {s}\n", .{image_path});
var gray = try cv.Mat.init();
defer gray.deinit();
cv.imgproc.cvtColor(img, &gray, .bgr2gray);
// Create structuring element
const anchor = cv.Point{ .x = -1, .y = -1 };
var kernel = try cv.imgproc.getStructuringElement(.rect, cv.Size{ .width = 5, .height = 5 }, anchor);
defer kernel.deinit();
// Erosion
var eroded = try cv.Mat.init();
defer eroded.deinit();
cv.imgproc.erode(gray, &eroded, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_erode.jpg", eroded);
std.debug.print("💾 Erosion → examples/tmp/morph_erode.jpg\n", .{});
// Dilation
var dilated = try cv.Mat.init();
defer dilated.deinit();
cv.imgproc.dilate(gray, &dilated, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_dilate.jpg", dilated);
std.debug.print("💾 Dilation → examples/tmp/morph_dilate.jpg\n", .{});
// Opening
var opened = try cv.Mat.init();
defer opened.deinit();
cv.imgproc.morphologyEx(gray, &opened, .open, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_open.jpg", opened);
std.debug.print("💾 Opening → examples/tmp/morph_open.jpg\n", .{});
// Closing
var closed = try cv.Mat.init();
defer closed.deinit();
cv.imgproc.morphologyEx(gray, &closed, .close, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_close.jpg", closed);
std.debug.print("💾 Closing → examples/tmp/morph_close.jpg\n", .{});
// Gradient
var gradient_out = try cv.Mat.init();
defer gradient_out.deinit();
cv.imgproc.morphologyEx(gray, &gradient_out, .gradient, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_gradient.jpg", gradient_out);
std.debug.print("💾 Gradient → examples/tmp/morph_gradient.jpg\n", .{});
// Top Hat
var tophat = try cv.Mat.init();
defer tophat.deinit();
cv.imgproc.morphologyEx(gray, &tophat, .tophat, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_tophat.jpg", tophat);
std.debug.print("💾 Top Hat → examples/tmp/morph_tophat.jpg\n", .{});
// Black Hat
var blackhat = try cv.Mat.init();
defer blackhat.deinit();
cv.imgproc.morphologyEx(gray, &blackhat, .blackhat, kernel, anchor, 1, 0);
try cv.imgcodecs.imwrite("examples/tmp/morph_blackhat.jpg", blackhat);
std.debug.print("💾 Black Hat → examples/tmp/morph_blackhat.jpg\n", .{});
std.debug.print("\n🎉 Morphology operations complete!\n", .{});
}