Skip to content

Instantly share code, notes, and snippets.

@rafaelcorsi
Last active November 19, 2023 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafaelcorsi/12b83f36d117177709197543cb1c11b1 to your computer and use it in GitHub Desktop.
Save rafaelcorsi/12b83f36d117177709197543cb1c11b1 to your computer and use it in GitHub Desktop.
MO445A - project - python image marker
import cv2
import os
import numpy as np
def draw_line(event, x, y, flags, param):
global drawing, last_point, color, line_type, points, img_display
# Check if CTRL key is pressed
ctrl_pressed = flags & cv2.EVENT_FLAG_CTRLKEY
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
color = (
(0, 0, 255) if not ctrl_pressed else (0, 255, 0)
) # Red for normal left click, Green for left click + CTRL
line_type = 1 if not ctrl_pressed else 0 # 1 for Red, -1 for Green
last_point = (x, y)
points.append((x, y, line_type))
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.line(img_display, last_point, (x, y), color, 5)
points.append((x, y, line_type)) # Save every point along the line
last_point = (x, y)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.line(img_display, last_point, (x, y), color, 5)
points.append((x, y, line_type)) # Also save the end point
def save_points(image_name, points, image_size, img_display):
base_name = os.path.basename(image_name)
base_name = os.path.splitext(base_name)[0]
# 408 and 341 are the
with open(f"markers/{base_name}-seeds.txt", "w") as file:
width, height = image_size
file.write(
f"{len(points)} {height} {width} \n"
) # Write image dimensions and number of points
for point in points:
file.write(f"{point[0]} {point[1]} -1 {point[2]}\n")
cv2.imwrite(f"markers/{base_name}-seeds.png", img_display)
# List all images in the folder
folder_path = "images" # Replace this with the path to your folder
image_files = [
f for f in os.listdir(folder_path) if f.endswith((".png", ".jpg", ".jpeg"))
]
label_folder_path = "truelabels"
for image_file in image_files:
print(image_file)
img_path = os.path.join(folder_path, image_file)
original_img = cv2.imread(img_path)
label_img_path = os.path.join(label_folder_path, image_file)
label_img = cv2.imread(label_img_path)
if original_img is None:
continue # Skip if the file is not an image
img_display = cv2.addWeighted(original_img, 0.7, label_img, 0.3, 0)
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", draw_line)
drawing = False
last_point = None
color = (0, 0, 0)
line_type = 0
points = []
while True:
cv2.imshow("Image", img_display)
key = cv2.waitKey(1) & 0xFF
if key == 27: # Press ESC to clear strokes and restart
img_display = cv2.addWeighted(original_img, 0.7, label_img, 0.3, 0)
points.clear()
continue
elif key == 13: # Press Enter to save and go to next image
save_points(img_path, points, original_img.shape[:2], img_display)
break
cv2.destroyAllWindows()
@rafaelcorsi
Copy link
Author

  • Clique Esquerdo do Mouse: Desenha uma linha ou ponto vermelho na imagem.
  • Clique Esquerdo do Mouse + Tecla CTRL: Desenha uma linha ou ponto verde na imagem.
  • Tecla Enter: Salva as anotações em um arquivo .txt e avança para a próxima imagem.
  • Tecla ESC: Apaga todas as anotações na imagem atual e permite recomeçar a anotação.
  • Ctrl+C: Fecha o programa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment