OpenCV Draw rectangle from center x,y

11,087

Considering that center=(x,y), then:

cv::rectangle( image, 
               cvPoint(x-w/2,y-h/2),
               cvPoint(x+w/2,y+h/2),
               CV_RGB(r,g,b), 1, 8
             );

CvRect stores the top-left point as reference. So:

CvRect myrect=cvRect(x-w/2,y-h/2,w,h);
Share:
11,087
Charles Osei
Author by

Charles Osei

www.jhack.co.uk

Updated on August 10, 2022

Comments

  • Charles Osei
    Charles Osei over 1 year

    I want to draw a rectangle using only a point to the center. So pretty much a rectangle around my center point. What is the easiest way of doing this?Thanks.

  • Carpetfizz
    Carpetfizz about 7 years
    Why y+h/2/2 on the 3rd line?
  • LovaBill
    LovaBill about 7 years
    maybe a typo? dont really remember. try it and fix it! :)
  • Carpetfizz
    Carpetfizz about 7 years
    Fixed! Bottom point should be center + height / 2 not center + height / 4