c++版本实现的labelme代码
时间: 2023-12-27 17:03:51 浏览: 161
labelme当前最新Windows版本541
以下是使用C语言实现的一个简单的Labelme工具:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LABELS 50
#define MAX_LABEL_LENGTH 20
#define MAX_IMG_LENGTH 100
void print_menu(void);
void add_label(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int *num_labels);
void remove_label(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int *num_labels);
void list_labels(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int num_labels);
void label_image(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int num_labels, char img_name[MAX_IMG_LENGTH]);
int main(void) {
char labels[MAX_LABELS][MAX_LABEL_LENGTH];
int num_labels = 0;
char img_name[MAX_IMG_LENGTH];
while (1) {
print_menu();
char choice[2];
fgets(choice, 2, stdin);
switch (choice[0]) {
case '1':
add_label(labels, &num_labels);
break;
case '2':
remove_label(labels, &num_labels);
break;
case '3':
list_labels(labels, num_labels);
break;
case '4':
label_image(labels, num_labels, img_name);
break;
case '5':
printf("Exiting program...\n");
exit(0);
default:
printf("Invalid input. Please try again.\n");
break;
}
}
return 0;
}
void print_menu(void) {
printf("======== Labelme ========\n");
printf("1. Add label\n");
printf("2. Remove label\n");
printf("3. List labels\n");
printf("4. Label image\n");
printf("5. Exit\n");
printf("Enter your choice: ");
}
void add_label(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int *num_labels) {
if (*num_labels == MAX_LABELS) {
printf("Maximum number of labels reached.\n");
return;
}
char label[MAX_LABEL_LENGTH];
printf("Enter label name (max %d characters): ", MAX_LABEL_LENGTH - 1);
fgets(label, MAX_LABEL_LENGTH, stdin);
label[strcspn(label, "\n")] = '\0'; // remove newline character
if (strlen(label) == 0) {
printf("Label name cannot be empty.\n");
return;
}
for (int i = 0; i < *num_labels; i++) {
if (strcmp(label, labels[i]) == 0) {
printf("Label already exists.\n");
return;
}
}
strcpy(labels[*num_labels], label);
(*num_labels)++;
printf("Label added successfully.\n");
}
void remove_label(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int *num_labels) {
if (*num_labels == 0) {
printf("No labels to remove.\n");
return;
}
char label[MAX_LABEL_LENGTH];
printf("Enter label name to remove: ");
fgets(label, MAX_LABEL_LENGTH, stdin);
label[strcspn(label, "\n")] = '\0'; // remove newline character
if (strlen(label) == 0) {
printf("Label name cannot be empty.\n");
return;
}
int found = 0;
for (int i = 0; i < *num_labels; i++) {
if (strcmp(label, labels[i]) == 0) {
found = 1;
for (int j = i; j < *num_labels - 1; j++) {
strcpy(labels[j], labels[j+1]);
}
(*num_labels)--;
printf("Label removed successfully.\n");
break;
}
}
if (!found) {
printf("Label not found.\n");
}
}
void list_labels(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int num_labels) {
if (num_labels == 0) {
printf("No labels to list.\n");
return;
}
printf("Labels:\n");
for (int i = 0; i < num_labels; i++) {
printf("%d. %s\n", i+1, labels[i]);
}
}
void label_image(char labels[MAX_LABELS][MAX_LABEL_LENGTH], int num_labels, char img_name[MAX_IMG_LENGTH]) {
if (num_labels == 0) {
printf("No labels to use for labeling.\n");
return;
}
printf("Enter image name (max %d characters): ", MAX_IMG_LENGTH - 1);
fgets(img_name, MAX_IMG_LENGTH, stdin);
img_name[strcspn(img_name, "\n")] = '\0'; // remove newline character
if (strlen(img_name) == 0) {
printf("Image name cannot be empty.\n");
return;
}
printf("Labeling image '%s'...\n", img_name);
// code to label image goes here
}
```
这个程序允许用户添加、删除和列出标签,以及对图像进行标记。请注意,这只是一个示例程序,并且在实际使用中需要进行修改和改进。
阅读全文