Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

วิธีวาดวงกลมใน OpenCV C++

บทนำ

วงกลมต้องการเพียงจุดศูนย์กลางและรัศมี ฟังก์ชัน circle() อยู่ใน header <opencv2/imgproc/imgproc.hpp> ใช้งานง่ายกว่าวงรีเพราะไม่ต้องระบุมุมหมุนและแกน

Syntax

circle(img, center, radius, color, thickness);

ตัวอย่างโค้ด

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;

int main() {
    Mat whiteMatrix(200, 200, CV_8UC3, Scalar(255, 255, 255));
    
    Point center(100, 100);        // จุดศูนย์กลาง
    int radius = 50;               // รัศมี 50 พิกเซล
    Scalar line_Color(0, 0, 0);    // สีดำ
    int thickness = 2;             // ความหนา 2 พิกเซล
    
    namedWindow("whiteMatrix");
    circle(whiteMatrix, center, radius, line_Color, thickness);
    imshow("WhiteMatrix", whiteMatrix);
    waitKey(0);
    return 0;
}

พารามิเตอร์ของ circle()

  • center: จุดศูนย์กลาง (Point)
  • radius: รัศมี (int, พิกเซล)
  • color: สีเส้น (Scalar B,G,R)
  • thickness: ความหนาเส้น (int) ใช้ -1 หรือ FILLED เพื่อเติมสี
  • lineType (optional): LINE_8, LINE_AA (Anti-aliased)
  • shift (optional): บิตเลขทศนิยม

ตัวอย่างการเติมสี

// วาดวงกลมเติมสีฟ้า
circle(img, Point(100,100), 50, Scalar(255,0,0), FILLED);

// วาดวงกลม Anti-aliased (เส้นเรียบ)
circle(img, Point(100,100), 50, Scalar(0,0,0), 2, LINE_AA);

ผลลัพธ์

วิธีวาดวงกลมใน OpenCV C++

การประยุกต์ใช้

  • Mark จุดสนใจ (Keypoints, Face landmarks)
  • วาด UI: ปุ่มกลม, Indicator, Progress ring
  • Visualize: Blob detection, Particle tracking
  • สร้าง Mask วงกลมสำหรับ ROI