基于OpenCV灰度图像转GCode的单向扫描实现

news2025/3/11 3:32:25
  • 基于OpenCV灰度图像转GCode的单向扫描实现
    • 引言
    • 单向扫描存在的问题
    • 灰度图像单向扫描代码示例
    • 结论

基于OpenCV灰度图像转GCode的单向扫描实现

本文将介绍如何使用OpenCV库将灰度图转换为GCode,并通过单向扫描实现对图像的激光雕刻。GCode是一种用于控制数控机床和3D打印机的指令语言,而OpenCV是一种开源计算机视觉库。通过结合这两者,我们可以实现从图像到GCode的转换,进而在机器上实现图像的物理输出。

引言

在数字制造时代,将图像转换为GCode是实现自动化加工和打印的关键步骤。本文将探讨如何利用OpenCV库将灰度图转换为GCode,并通过单向扫描的方式实现对图像的激光雕刻。
未优化的单向扫描
上图是未做任何处理,直接从灰度图转换成GCode。
已优化的单向扫描
优化后生成的GCode如上所示:
原始图像
原始图像如上所示:

单向扫描存在的问题

单向操作存在来回折返空行程问题,导致加工时间变长。
本文主要通过使用以下形式的代码,删除了多余的行程(空跑没任何意义的G0)。

while(++x < image.cols && image.at<std::uint8_t>(y, x) == 255) {
    length++;
}
--x;

实现了未优化版本和优化版本的单向扫描,两者加工时间从生成的GCode代码上,可以看出有了很大差异。

红色是 G0,绿色是加工部分 G1。

当然如果使用双向扫描方向,加工时间差异会更大。

灰度图像单向扫描代码示例

编译器要求最低 C++23

#pragma once
#include <opencv2/opencv.hpp>
#include <fstream>
#include <print>
#include <vector>
#include <optional>
#include <ranges>

struct G0 {
    std::optional<float> x, y;
    std::optional<int> s;

    std::string toString() {
        std::string command = "G0";
        if(x.has_value()) {
            command += std::format(" X{:.3f}", x.value());
        }
        if(y.has_value()) {
            command += std::format(" Y{:.3f}", y.value());
        }
        if(s.has_value()) {
            command += std::format(" S{:d}", s.value());
        }
        return command;
    }

    explicit  operator std::string() const {
        std::string command = "G0";
        if(x.has_value()) {
            command += std::format(" X{:.3f}", x.value());
        }
        if(y.has_value()) {
            command += std::format(" Y{:.3f}", y.value());
        }
        if(s.has_value()) {
            command += std::format(" S{:d}", s.value());
        }
        return command;
    }
};

struct G1 {
    std::optional<float> x, y;
    std::optional<int> s;

    std::string toString() {
        std::string command = "G1";
        if(x.has_value()) {
            command += std::format(" X{:.3f}", x.value());
        }
        if(y.has_value()) {
            command += std::format(" Y{:.3f}", y.value());
        }
        if(s.has_value()) {
            command += std::format(" S{:d}", s.value());
        }
        return command;
    }

    explicit operator std::string() const {
        std::string command = "G1";
        if(x.has_value()) {
            command += std::format(" X{:.3f}", x.value());
        }
        if(y.has_value()) {
            command += std::format(" Y{:.3f}", y.value());
        }
        if(s.has_value()) {
            command += std::format(" S{:d}", s.value());
        }
        return command;
    }
};

class ImageToGCode
{
public:
    // 激光模式
    enum class LaserMode {
        Cutting,    // 切割 M3 Constant Power
        Engraving,  // 雕刻 M4 Dynamic Power
    };

    // 扫描方式
    enum class ScanMode {
        Unidirection,  // 单向
        Bidirection,   // 双向
    };

    struct kEnumToStringLaserMode {
        constexpr std::string_view operator[](const LaserMode mode) const noexcept {
            switch(mode) {
                case LaserMode::Cutting: return "M3";
                case LaserMode::Engraving: return "M4";
            }
            return {};
        }

        constexpr LaserMode operator[](const std::string_view mode) const noexcept {
            if(mode.compare("M3")) {
                return LaserMode::Cutting;
            }
            if(mode.compare("M4")) {
                return LaserMode::Engraving;
            }
            return {};
        }
    };

    ImageToGCode() = default;

    ~ImageToGCode() = default;

    auto &setInputImage(const cv::Mat &mat) {
        this->mat = mat;
        return *this;
    }

    auto &setOutputTragetSize(double width, double height, double resolution = 10.0 /* lin/mm */) {
        this->width      = width;
        this->height     = height;
        this->resolution = resolution;
        return *this;
    }

    auto &builder() {
        command.clear();
        try {
            matToGCode();
        } catch(cv::Exception &e) {
            std::println("cv Exception {}", e.what());
        }

        std::vector<std::string> header;
        header.emplace_back("G17G21G90G54");                                             // XY平面;单位毫米;绝对坐标模式;选择G54坐标系
        header.emplace_back(std::format("F{:d}", 30000));                                // 移动速度 毫米/每分钟
        header.emplace_back(std::format("G0 X{:.3f} Y{:.3f}", 0.f, 0.f));                // 设置工作起点及偏移
        header.emplace_back(std::format("{} S0", kEnumToStringLaserMode()[laserMode]));  // 激光模式
        if(airPump.has_value()) {
            header.emplace_back(std::format("M16 S{:d}", 300));  // 打开气泵
        }

        std::vector<std::string> footer;
        footer.emplace_back("M5");
        if(airPump.has_value()) {
            footer.emplace_back("M9");  // 关闭气泵,保持 S300 功率
        }

        command.insert_range(command.begin(), header);
        command.append_range(footer);

        return *this;
    }

    bool exportGCode(const std::string &fileName) {
        std::fstream file;
        file.open(fileName, std::ios_base::out | std::ios_base::trunc);
        if(!file.is_open()) {
            return false;
        }

        for(auto &&v: command | std::views::transform([](auto item) { return item += "\n"; })) {
            file.write(v.c_str(), v.length());
        }

        return true;
    }

    auto setLaserMode(LaserMode mode) {
        laserMode = mode;
        return *this;
    }

    auto setScanMode(ScanMode mode) {
        scanMode = mode;
        return *this;
    }

private:
    void matToGCode() {
        assert(mat.channels() == 1);
        assert(std::isgreaterequal(resolution, 1e-5f));
        assert(!((width * resolution < 1.0) || (height * resolution < 1.0)));

        unidirectionStrategy();
    }

    void internal(cv::Mat &image, auto x /*width*/, auto y /*height*/) {
        auto pixel = image.at<cv::uint8_t>(y, x);
        if(pixel == 255) {
            command.emplace_back(G0(x / resolution, y / resolution, std::nullopt));
        } else {
            auto power = static_cast<int>((1.0 - static_cast<double>(pixel) / 255.0) * 1000.0);
            command.emplace_back(G1(x / resolution, y / resolution, power));
        }
    }

    // 单向扫描
    // 未做任何优化处理,像素和G0、G1一一映射对应。
    void unidirectionStrategy() {
        cv::Mat image;
        cv::resize(mat, image, cv::Size(static_cast<int>(width * resolution), static_cast<int>(height * resolution)));
        cv::imshow("mat",image);
        cv::waitKey(0);
        for(int y = 0; y < image.rows; ++y) {
            command.emplace_back(G0(0, y / resolution, std::nullopt).toString());
            for(int x = 0; x < image.cols; ++x) {
                auto pixel = image.at<uchar>(y, x);
                if(pixel == 255) {
                    command.emplace_back(G0(x / resolution, std::nullopt, std::nullopt));
                } else {
                    auto power = static_cast<int>((1.0 - static_cast<double>(pixel) / 255.0) * 1000.0);
                    command.emplace_back(G1(x / resolution, std::nullopt, power));
                }
            }
        }
    }

    // 单向扫描优化版本V1
    // 删除多余空行程,这里空行程指连续的无用的G0。
    void unidirectionOptStrategy() {
        cv::Mat image;
        cv::resize(mat, image, cv::Size(static_cast<int>(width * resolution), static_cast<int>(height * resolution)));
        int offset = 0;  // The frist consecutive G0
        int length = 0;
        for(int y = 0; y < image.rows; ++y) {
            command.emplace_back(G0(offset / resolution, y / resolution, std::nullopt).toString());
            for(int x = 0; x < image.cols; ++x) {
                auto pixel = image.at<uchar>(y, x);
                length     = 0;
                if(pixel == 255) {
                    while(++x < image.cols && image.at<std::uint8_t>(y, x) == 255) {
                        length++;
                    }
                    --x;

                    // Whether continuous GO exists
                    if(length) {
                        if(x - length == 0) {  // skip The frist consecutive G0
                            offset = length;
                            command.emplace_back(G0((x) / resolution, std::nullopt, std::nullopt));
                            continue;
                        }

                        if(x == image.cols - 1) {  // skip The last consecutive G0
                            command.emplace_back(G0((x - length) / resolution, std::nullopt, std::nullopt));
                            continue;
                        }
                        // Continuous GO
                        command.emplace_back(G0(x / resolution, std::nullopt, std::nullopt));
                    } else {
                        // Independent GO
                        command.emplace_back(G0(x / resolution, std::nullopt, std::nullopt));
                    }
                } else {
                    auto power = static_cast<int>((1.0 - static_cast<double>(pixel) / 255.0) * 1000.0);
                    command.emplace_back(G1(x / resolution, std::nullopt, power));
                }
            }
        }
    }

    // Define additional strategy functions here
private:
    cv::Mat mat;                                 // 灰度图像
    double width {0};                            // 工作范围 x 轴
    double height {0};                           // 工作范围 y 轴
    double resolution {0};                       // 精度 lin/mm
    ScanMode scanMode {ScanMode::Bidirection};   // 默认双向
    LaserMode laserMode {LaserMode::Engraving};  // 默认雕刻模式
    std::optional<int> airPump;                  // 自定义指令 气泵 用于吹走加工产生的灰尘 范围 [0,1000]
    // add more custom cmd
    std::vector<std::string> command;            // G 代码
};

int main() {
    // 读取以灰度的形式读取一个图像
    cv::Mat mat = cv::imread(R"(ImageToGCode\image\tigger.jpg)", cv::IMREAD_GRAYSCALE);
    cv::flip(mat, mat, 0);

    // 实例化一个对象
    ImageToGCode handle;
    // 设置相关参数
    // setInputImage 输入图像
    // setOutputTragetSize 输出物理尺寸大小 以 mm 为单位,这里输出 50x50 mm 大小
    // builder 开始执行图像转GCode操作
    // exportGCode 导出 gcode 文件
    handle.setInputImage(mat).setOutputTragetSize(50,50).builder().exportGCode(R"(ImageToGCode\output\001.nc)");
}

结论

通过结合OpenCV和GCode,我们成功地将灰度图转换为机器可执行的指令,实现了对图像的单向扫描激光雕刻。这种方法可应用于数控机床和3D打印机等领域,为数字制造提供了更灵活的图像处理和加工方式。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1427569.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

物联网自动虫情测报仪器

TH-CQ3S在农业生产的进程中&#xff0c;病虫害的防治始终是关键的一环。然而&#xff0c;传统的病虫害监测手段往往存在着效率低下、准确度不高等问题&#xff0c;这无疑给农业生产带来了巨大的困扰。好在&#xff0c;随着科技的飞速发展&#xff0c;一款基于物联网技术的自动虫…

Oracle闪回日志管理(flashbackup log)

Oracle闪回日志管理&#xff08;flashbackup log&#xff09; 1.开启闪回日志 shutdown immediate startup mount; alter database archivelog; alter database flashback on; alter database open; 2、查看闪回日志保留期限 show parameter flash 默认是1440分钟&#xff0c…

【软件设计师笔记】程序语言设计考点

【考证须知】IT行业高含金量的证书(传送门)&#x1f496; 【软件设计师笔记】计算机系统基础知识考点(传送门)&#x1f496; 【软件设计师笔记】操作系统考点(传送门)&#x1f496; &#x1f413; 编程语言之间的翻译形式 汇编 高级程序不能直接在计算机上执行&#xff0c;…

极狐GitLab 和飞书的集成实践

飞书集成和通知 如果您想在飞书的群组中查看极狐GitLab 项目中的事件变更&#xff0c;如创建议题、流水线故障或关闭合并请求等&#xff0c;您可以将飞书与极狐GitLab 进行集成。 飞书集成 配置飞书 在飞书中创建机器人在飞书群组中添加机器人 在飞书中创建机器人&#xff…

【全网最全】2024美赛ABCDEF题思路模型全解(后续会更新)

欲获取更多资料&#xff0c;一定要点击这里并关注文末的公众号&#xff01;&#xff01;&#xff01; 最新更新&#xff1a;我们团队不仅在第一时间更新了24美赛全题目的深度翻译和深入分析&#xff0c;经过爆肝奋战&#xff0c;我们在第一时间给出了ABCDEF全题目的完整建模过程…

OSPF——开放最短路径优先、多区域OSPF

目录 1 内部网关协议 OSPF 1.1 三个主要特点&#xff08;生成拓扑图) 其他特点 1.2 链路状态数据库 (link-state database) 1.3 链路状态路由过程 1.4 OSPF 的五种分组类型 1.4.1 OSPF 分组用 IP 数据报传送 1.5 OSPF 工作过程 1.6 OSPF 定义五种网络类型 1.7 多路访…

WAF 无法防护的八种风险

一、目录遍历漏洞 测试用例&#xff1a;Apache 目录遍历漏洞 测试环境搭建&#xff1a; apt intsall apache2 && cd /var/www/html/ && rm index.html无法拦截原因&#xff1a; 请求中无明显恶意特征&#xff0c;无法判断为攻击行为 实战数据&#xff1a; 截…

飞腾D2000+X100的UART串口调试方法

一、测试使用主板的串口说明 D2000自带的UART0直接引出9针全功能RS232串口,UART1为CPU调试串口,UART2和UART3接485接口芯片转为RS485,此外,主板还有X100 usb2.0转出8路RS232,详细硬件框图如下: 源芯片串口号串口电平连接器丝印D2000UART0RS232COM0D2000UART1TTL调试串口…

LightDB24.1 存储过程中声明的不带参数的游标,支持open 游标名加括号的调用方式

背景 oracle 存储过程中不带参数声明的游标&#xff0c;可以在open打开时加上括号进行调用。在老的业务中经常能够看到这种用法。虽然觉得不合理&#xff0c;但是oracle人家支持&#xff0c;作为兼容性极强的LightDB不会在这个小水沟翻车的&#xff0c;果断进行支持一波。 L…

【鸿蒙千帆起】高德地图携手 HarmonyOS NEXT,开启智能出行新篇章

2024 年 1 月 18 日下午&#xff0c;华为举办了鸿蒙生态千帆启航仪式&#xff0c;对外宣布 HarmonyOS NEXT 星河预览版现已开放申请&#xff0c;同时&#xff0c;首批 200鸿蒙原生应用加速开发&#xff0c;鸿蒙生态设备数量更是突破了 8 亿大关。这些进展反映了开发者和合作伙伴…

Jmeter+ant+Jenkins 接口自动化框架完整版

接口自动化测试单有脚本是不够的&#xff0c;我们还需要批量跑指定接口&#xff0c;生成接口运行报告&#xff0c;定位报错接口&#xff0c;接口定时任务&#xff0c;邮件通知等功能。批量跑指定接口&#xff1a;我们可以利用ant批量跑指定目录下的Jmeter脚本生成接口运行报告&…

上传upload及显示img图片预览、删除

上传图片文件a-upload html部分 <div className="clearfix"><a-upload:custom-request="customRequest"listType="picture-card":fileList="fileList":onPreview="handlePreview":on-remove="del">&…

【算法与数据结构】300、674、LeetCode最长递增子序列 最长连续递增序列

文章目录 一、300、最长递增子序列二、674、最长连续递增序列三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、300、最长递增子序列 思路分析&#xff1a; 第一步&#xff0c;动态数组的含义。 d p [ i ] dp[i] dp[i…

什么是接口的幂等性,如何保证接口的幂等性?

✅作者简介&#xff1a;大家好&#xff0c;我是Leo哥&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Leo哥的博客 &#x1f49e;当前专栏&#xff1a; Java ✨特色专栏&#xff1a; MyS…

如何在Windows部署GoLand并通过SSH远程连接Linux服务器

文章目录 1. 安装配置GoLand2. 服务器开启SSH服务3. GoLand本地服务器远程连接测试4. 安装cpolar内网穿透远程访问服务器端4.1 服务器端安装cpolar4.2 创建远程连接公网地址 5. 使用固定TCP地址远程开发 本文主要介绍使用GoLand通过SSH远程连接服务器&#xff0c;并结合cpolar内…

想好新年去哪了吗?合合信息扫描全能王用AI“留住”年味

还有不到十天&#xff0c;除夕就要到了。近几年春节假期中&#xff0c;有人第一次带着孩子直击海面冰风&#xff0c;坐船回老家&#xff1b;也有人选择“漫游”国内外&#xff0c;在旅行中迎接新春的朝气。合合信息旗下扫描全能王APP通过AI扫描技术&#xff0c;提供了一种全新的…

白皮书发布,石油石化数字孪生加速

近日&#xff0c;《数字石化 孪生智造——石油石化数字孪生白皮书》发布。白皮书聚焦石油石化行业发展机遇&#xff0c;剖析数字孪生技术在行业中的案例实践与应用场景&#xff0c;展望石油石化企业未来孪生发展新态势。 当前&#xff0c;国家大力推动减污降碳协同增效&#x…

【机器学习】基于K-近邻的车牌号识别

实验四: 基于K-近邻的车牌号识别 1 案例简介 ​ 图像的智能处理一直是人工智能领域广受关注的一类技术&#xff0c;代表性的如人脸识别与 CT 肿瘤识别&#xff0c;在人工智能落地的进程中发挥着重要作用。其中车牌号识别作为一个早期应用场景&#xff0c;已经融入日常生活中&…

vue2使用echarts自定义tooltip内容

先上最终效果图 # 实现过程&#xff1a; 一、下载引入echarts 下载 npm install echarts --save在main.js中引入 import * as echarts from "echarts"; Vue.prototype.$echarts echarts;二、使用 <template><div id"myechart" style"…

[香橙派开发系列]使用蓝牙和手机进行信息的交换

文章目录 前言一、HC05蓝牙模块1.HC05概述2.HC05的连接图3.进入HC05的命令模式4.常用的AT指令4.1 检查AT是否上线4.2 重启模块4.3 获取软件版本号4.4 恢复默认状态4.5 获取蓝牙的名称4.6 设置蓝牙模块的波特率4.7 查询蓝牙的连接模式4.8 查询模块角色 5.连接电脑6.通过HC05发送…