-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (57 loc) · 2.25 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (57 loc) · 2.25 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
76
77
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <cmath>
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Usage: ./generate_gcode_from_image <image_path>\n";
return 1;
}
std::string imagePath = argv[1];
cv::Mat img = cv::imread(imagePath, cv::IMREAD_GRAYSCALE);
if (img.empty()) {
std::cerr << "Failed to load image\n";
return 1;
}
cv::resize(img, img, cv::Size(180, 180));
cv::Mat blurred;
cv::GaussianBlur(img, blurred, cv::Size(5, 5), 0);
cv::Mat bw;
cv::threshold(blurred, bw, 128, 255, cv::THRESH_BINARY_INV);
cv::Mat kernel = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::dilate(bw, bw, kernel);
std::vector<std::vector<cv::Point>> contours;
cv::findContours(bw, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
std::ofstream gcodeFile("output.gcode");
if (!gcodeFile) {
std::cerr << "Error opening output file\n";
return 1;
}
gcodeFile << "G21 ; Set units to mm\n";
gcodeFile << "G90 ; Absolute positioning\n";
gcodeFile << "G0 Z5 ; Pen up\n";
double scale = 1;
for (const auto& contour : contours) {
if (contour.empty() || cv::contourArea(contour) < 50) continue;
std::vector<cv::Point> approx;
double epsilon = 1.5;
cv::approxPolyDP(contour, approx, epsilon, true);
if (approx.empty()) continue;
int startX = static_cast<int>(std::round(approx[0].x * scale));
int startY = static_cast<int>(std::round(approx[0].y * scale));
gcodeFile << "G0 Z5 ; Pen up\n";
gcodeFile << "G0 X" << startX << " Y" << startY << "\n";
gcodeFile << "G1 Z0 ; Pen down\n";
for (size_t i = 1; i < approx.size(); ++i) {
int X = static_cast<int>(std::round(approx[i].x * scale));
int Y = static_cast<int>(std::round(approx[i].y * scale));
gcodeFile << "G1 X" << X << " Y" << Y << "\n";
}
gcodeFile << "G1 X" << startX << " Y" << startY << "\n";
gcodeFile << "G0 Z5 ; Pen up\n";
}
gcodeFile << "M30 ; Program end\n";
gcodeFile.close();
std::cout << "G-code written to 'output.gcode' with resized 180x180 input\n";
return 0;
}