From d2ad2d934a7748b80d633d99906831da520fe372 Mon Sep 17 00:00:00 2001 From: lxowalle Date: Mon, 13 May 2024 11:19:12 +0800 Subject: [PATCH] * add more image examples --- examples/vision/image_basic/binary.py | 10 ++++++++ examples/vision/image_basic/find_apriltags.py | 18 +++++++++++++++ examples/vision/image_basic/find_barcodes.py | 16 +++++++++++++ examples/vision/image_basic/find_blobs.py | 20 ++++++++++++++++ examples/vision/image_basic/find_edges.py | 12 ++++++++++ examples/vision/image_basic/find_lines.py | 23 +++++++++++++++++++ examples/vision/image_basic/find_qrcodes.py | 15 ++++++++++++ 7 files changed, 114 insertions(+) create mode 100644 examples/vision/image_basic/binary.py create mode 100644 examples/vision/image_basic/find_apriltags.py create mode 100644 examples/vision/image_basic/find_barcodes.py create mode 100644 examples/vision/image_basic/find_blobs.py create mode 100644 examples/vision/image_basic/find_edges.py create mode 100644 examples/vision/image_basic/find_lines.py create mode 100644 examples/vision/image_basic/find_qrcodes.py diff --git a/examples/vision/image_basic/binary.py b/examples/vision/image_basic/binary.py new file mode 100644 index 00000000..752fa712 --- /dev/null +++ b/examples/vision/image_basic/binary.py @@ -0,0 +1,10 @@ +from maix import image + +# 1. load image +src_img = image.load("test.jpg") + +# 2. binarize the image +thresholds = ((0, 100, 20, 80, 10, 80)) +img = src_img.copy() +img.binary(thresholds) +img.save("binary.jpg") diff --git a/examples/vision/image_basic/find_apriltags.py b/examples/vision/image_basic/find_apriltags.py new file mode 100644 index 00000000..7bf183e6 --- /dev/null +++ b/examples/vision/image_basic/find_apriltags.py @@ -0,0 +1,18 @@ +from maix import camera, display, image +from maix.image import ApriltagFamilies + +cam = camera.Camera(160, 120) +disp = display.Display() + +families = ApriltagFamilies.TAG36H11 + +while 1: + img = cam.read() + + apriltags = img.find_apriltags(families=ApriltagFamilies.TAG36H11) + for a in apriltags: + corners = a.corners() + for i in range(4): + img.draw_line(corners[i][0], corners[i][1], corners[(i + 1) % 4][0], corners[(i + 1) % 4][1], image.COLOR_GREEN, 2) + + disp.show(img) diff --git a/examples/vision/image_basic/find_barcodes.py b/examples/vision/image_basic/find_barcodes.py new file mode 100644 index 00000000..b1f6e33c --- /dev/null +++ b/examples/vision/image_basic/find_barcodes.py @@ -0,0 +1,16 @@ +from maix import camera, display, image + +cam = camera.Camera(320, 240) +disp = display.Display() + + +while 1: + img = cam.read() + + barcodes = img.find_barcodes() + for b in barcodes: + rect = b.rect() + img.draw_rect(rect[0], rect[1], rect[2], rect[3], image.COLOR_BLUE, 2) + img.draw_string(0, 0, "payload: " + b.payload(), image.COLOR_GREEN) + + disp.show(img) diff --git a/examples/vision/image_basic/find_blobs.py b/examples/vision/image_basic/find_blobs.py new file mode 100644 index 00000000..b2c3932f --- /dev/null +++ b/examples/vision/image_basic/find_blobs.py @@ -0,0 +1,20 @@ +from maix import camera, display, image + +cam = camera.Camera(320, 240) +disp = display.Display() + +area_threshold = 1000 +pixels_threshold = 1000 +# thresholds = [[0, 80, 40, 80, 10, 80]] # red +thresholds = [[0, 80, -120, -10, 0, 30]] # green +# thresholds = [[0, 80, 30, 100, -120, -60]] # blue +while 1: + img = cam.read() + + blobs = img.find_blobs(thresholds, area_threshold = 1000, pixels_threshold = 1000) + for b in blobs: + corners = b.corners() + for i in range(4): + img.draw_line(corners[i][0], corners[i][1], corners[(i + 1) % 4][0], corners[(i + 1) % 4][1], image.COLOR_RED) + + disp.show(img) diff --git a/examples/vision/image_basic/find_edges.py b/examples/vision/image_basic/find_edges.py new file mode 100644 index 00000000..16843f64 --- /dev/null +++ b/examples/vision/image_basic/find_edges.py @@ -0,0 +1,12 @@ +from maix import camera, display +from maix.image import EdgeDetector + +cam = camera.Camera(320, 240) +disp = display.Display() + +edge_type = EdgeDetector.EDGE_CANNY + +while 1: + img = cam.read() + img.find_edges(edge_type, threshold=[50, 100]) + disp.show(img) diff --git a/examples/vision/image_basic/find_lines.py b/examples/vision/image_basic/find_lines.py new file mode 100644 index 00000000..d1b2c82c --- /dev/null +++ b/examples/vision/image_basic/find_lines.py @@ -0,0 +1,23 @@ +from maix import camera, display, image +import math + +cam = camera.Camera(320, 240) +disp = display.Display() + +threshold = 2000 + +while 1: + img = cam.read() + + lines = img.find_lines(threshold=2000) + for a in lines: + img.draw_line(a.x1(), a.y1(), a.x2(), a.y2(), image.COLOR_RED, 2) + theta = a.theta() + rho = a.rho() + angle_in_radians = math.radians(theta) + x = int(math.cos(angle_in_radians) * rho) + y = int(math.sin(angle_in_radians) * rho) + img.draw_line(0, 0, x, y, image.COLOR_GREEN, 2) + img.draw_string(x, y, "theta: " + str(theta) + "," + "rho: " + str(rho), image.COLOR_GREEN) + + disp.show(img) diff --git a/examples/vision/image_basic/find_qrcodes.py b/examples/vision/image_basic/find_qrcodes.py new file mode 100644 index 00000000..ab7c3261 --- /dev/null +++ b/examples/vision/image_basic/find_qrcodes.py @@ -0,0 +1,15 @@ +from maix import camera, display, image + +cam = camera.Camera(320, 240) +disp = display.Display() + +while 1: + img = cam.read() + qrcodes = img.find_qrcodes() + for q in qrcodes: + corners = q.corners() + for i in range(4): + img.draw_line(corners[i][0], corners[i][1], corners[(i + 1) % 4][0], corners[(i + 1) % 4][1], image.COLOR_RED) + img.draw_string(0, 0, "payload: " + q.payload(), image.COLOR_BLUE) + + disp.show(img)