-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
101 lines (71 loc) · 2.8 KB
/
main.cpp
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
92
93
94
95
96
97
98
99
100
101
#include "nanosam/nanosam.h"
#include "utils.h"
void segmentClickedPoint(NanoSam& nanosam, string imagePath) {
auto image = imread(imagePath);
// Create a window to display the image
cv::namedWindow("Image");
// Data structure to hold clicked point
PointData pointData;
pointData.clicked = false;
cv::Mat clonedImage = image.clone();
int clickCount = 0;
// Loop until Esc key is pressed
while (true)
{
// Display the original image
cv::imshow("Image", clonedImage);
if (pointData.clicked)
{
pointData.clicked = false; // Reset clicked flag
auto mask = nanosam.predict(image, { pointData.point }, { 1.0f });
cv::circle(clonedImage, pointData.point, 5, cv::Scalar(0, 0, 255), -1);
if (clickCount >= CITYSCAPES_COLORS.size()) clickCount = 0;
overlay(image, mask, CITYSCAPES_COLORS[clickCount * 9]);
clickCount++;
}
// Set the callback function for mouse events on the displayed cloned image
cv::setMouseCallback("Image", onMouse, &pointData);
// Check for Esc key press
char key = cv::waitKey(1);
if (key == 27) // ASCII code for Esc key
{
clonedImage = image.clone();
}
}
cv::destroyAllWindows();
}
void segmentBbox(NanoSam& nanosam, string imagePath, string outputPath, vector<Point> bbox)
{
auto image = imread(imagePath);
// 2 : Bounding box top-left, 3 : Bounding box bottom-right
vector<float> labels = { 2, 3 };
auto mask = nanosam.predict(image, bbox, labels);
overlay(image, mask);
rectangle(image, bbox[0], bbox[1], cv::Scalar(255, 255, 0), 3);
imwrite(outputPath, image);
}
void segmentWithPoint(NanoSam& nanosam, string imagePath, string outputPath, Point promptPoint)
{
auto image = imread(imagePath);
// 1 : Foreground
vector<float> labels = { 1.0f };
auto mask = nanosam.predict(image, { promptPoint }, labels);
overlay(image, mask);
imwrite(outputPath, image);
}
int main()
{
/* 1. Load engine examples */
// Option 1: Load the engines
//NanoSam nanosam("data/resnet18_image_encoder.engine", "data/mobile_sam_mask_decoder.engine");
// Option 2: Build the engines from onnx files
NanoSam nanosam("data/resnet18_image_encoder.onnx", "data/mobile_sam_mask_decoder.onnx");
/* 2. Segmentation examples */
// Demo 1: Segment using a point
segmentWithPoint(nanosam, "assets/dog.jpg", "assets/dog_mask.jpg", Point(1300, 900));
// Demo 2: Segment using a bounding box
segmentBbox(nanosam, "assets/dogs.jpg", "assets/dogs_mask.jpg", { Point(100, 100), Point(750, 759) });
// Demo 3: Segment the clicked object
segmentClickedPoint(nanosam, "assets/dogs.jpg");
return 0;
}