1

I want to create image labeling program with opencv c++ to label images for yolo object detector, but i'm struggling in converting rectangle coordinates (x1,y1,x2,y2) to yolo format which is "object-class x_center y_center width height". And according to the documentation x_center and y_center are center of rectangle (are not top-left corner).

I tried this code on already labeled image

            double centerX = (x1 + x2) / (2.0 * imageWidth);
            double centerY = (y1 + y2) / (2.0 * imageHeight);
            double width = double(abs(x2 - x1) / imageWidth);
            double height = double(abs(y2- y1) / imageHeight);

and get 0 0.396759 0.278906 0.0109375 0.326852 which is deferent from 0 0.40703125 0.5194444444444445 0.25364583333333335 0.5851851851851851. How can i get it to work?

EK_code
  • 11
  • 3
  • can you provide me the input? – Hridoy_089 Aug 07 '20 at 12:13
  • The image dimensions are 1920x1080, selected rectangle coordinates(x1,y1,x2,y2)=(539, 253, 522, 601) and the output is 0 0.276302 0.39537 0.00885417 0.322222. But the output should be roughly 0 0.40703125 0.5194444444444445 0.25364583333333335 0.5851851851851851. – EK_code Aug 07 '20 at 12:42
  • Why you divide imagewidth and image height? What is the math behind it? Why such small numbers, you dont wanna get the centers? – Yunus Temurlenk Aug 07 '20 at 17:25
  • Center x 0.40 means 40 % of the image width, center y is % of the image height and so on. This is what yolo uses to detect where bounding box is. – EK_code Aug 08 '20 at 18:26
  • So center x is the center of the selected rectangle width, relative to the image width, and center y is the center of the selected rectangle height, relative to the image height. – EK_code Aug 08 '20 at 18:38
  • As a reference computation, I can refer [pybboxes](https://github.com/devrimcavusoglu/pybboxes). – null May 01 '22 at 09:27

1 Answers1

0

I finally found the code that works:

Point centralPoint = Point((selectedRect.tl().x + selectedRect.br().x) / 2, (selectedRect.tl().y + selectedRect.br().y) / 2);
                centerX = centralPoint.x / imageWidth;
                centerY = centralPoint.y / imageHeight;

                rectWidth = abs(selectedRect.br().x - selectedRect.tl().x) / imageWidth;
                rectHeight = abs(selectedRect.br().y - selectedRect.tl().y) / imageHeight;
EK_code
  • 11
  • 3