-1

I am trying to write a python script which will crop image by annotated data. Can anyone help me?

Image: enter image description here

Annotated data:

0 0.514583 0.716204 0.136458 0.102778

Jony Ghosh
  • 37
  • 5
  • please review [ask] and [mre]. show your research (googling) and your own effort. -- those values are relative to total width and height... and the first is probably the class or index of annotation or something – Christoph Rackwitz Aug 23 '22 at 09:11

3 Answers3

1

The YOLO coordinates mentioned are in format: x_center, y_center, width_box, height_box, normalised wrt image height and width. You can convert it to normal/usual format for drawing rectangle using:

x = x_center * image_width
y = y_center * image_height
w = width_box * image_width
h = height_box * image_height

If you looking for bounding box format as: xmin, ymin, xmax, ymax, then:

xmin = int(x - width_box/2)
ymin = int(y - height_box/2)
xmax = int(x + width_box/2)
ymax = int(y + height_box/2)
Prashant Maurya
  • 639
  • 4
  • 13
0

you can do like this >>

import cv2, os

image = cv2.imread('/file/path.ext')
lh,lw,_ = image.shape

# now take your coordinates
x,y,w,h = 0.514583, 0.716204, 0.136458, 0.102778
x,y,w,h = int(x*lw), int(y*lh), int(w*lw), int(h*lh) ## to match the bounding box coordinates with actual width, height

boxedImage = image[y:y+h, x:x+w]
cv2.imshow(boxedImage)

Hope this helps!

temp
  • 82
  • 1
  • 10
  • Please use correct upper case letters. Also add some more information's to you solution. Code-only is not an answer. – buhtz Feb 01 '23 at 10:49
0

You can use this code to load your image and annotation files into the program, crop the images using the annotation coordinates, and save the cropped images as new files.

import cv2
import numpy as np
import os

# Function to display an image using OpenCV
def show_image(image, title='Image'):
    cv2.imshow(title, image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# Load the YOLO annotations
annotations_file = r"path/to/annotation.txt"
annotations = np.loadtxt(annotations_file, delimiter=" ")

# Load the image
image_file = r"path/to/image.jpg"
image = cv2.imread(image_file)
print(image.shape)
# Convert the YOLO annotations to pixel coordinates
image_height, image_width, _ = image.shape

x_center = annotations[1]
y_center = annotations[2]
width = annotations[3]
height = annotations[4]
x_min = int((x_center - width / 2) * image_width)
y_min = int((y_center - height / 2) * image_height)
x_max = int((x_center + width / 2) * image_width)
y_max = int((y_center + height / 2) * image_height)

print("x_min:", x_min)
print("y_min:", y_min)
print("x_max:", x_max)
print("y_max:", y_max)
# Crop the image
cropped_image = image[y_min:y_max, x_min:x_max]

# Show the cropped image
show_image(cropped_image)


# Save the cropped image
output_dir = r"path/to/crop_files_dir"
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
output_file = os.path.join(output_dir, "cropped_image.jpg")
cv2.imwrite(output_file, cropped_image)

original imageenter image description here

cropped imageenter image description here