ในการวาดรูปสี่เหลี่ยมผืนผ้า เราต้องการสี่จุด ดูรูปต่อไปนี้

ในรูป มีสี่จุด x1, x2, y1 และ y2 สี่จุดเหล่านี้สร้างสี่พิกัด ในการวาดรูปสี่เหลี่ยมผืนผ้าโดยใช้ OpenCV เราต้องกำหนดจุดเหล่านี้และแสดงสี่เหลี่ยมที่เราต้องการเมทริกซ์ เราต้องประกาศค่าที่เกี่ยวข้องอื่นๆ เช่น สีของเส้นและความกว้างของเส้น
ไวยากรณ์พื้นฐานของวิธีนี้มีดังนี้ −
ไวยากรณ์
rectangle(whiteMatrix, starting, ending, line_Color, thickness);
โปรแกรมต่อไปนี้แสดงวิธีการวาดสี่เหลี่ยมใน OpenCV
ตัวอย่าง
#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));// Declaring a white matrix//
Point starting(40, 40);//Declaring the starting coordinate//
Point ending(160, 100);//Declaring the ending coordinate
Scalar line_Color(0, 0, 0);//Color of the rectangle//
int thickness = 2;//thickens of the line//
namedWindow("whiteMatrix");//Declaring a window to show the rectangle//
rectangle(whiteMatrix, starting, ending, line_Color, thickness);//Drawing the rectangle//
imshow("WhiteMatrix", whiteMatrix);//Showing the rectangle//
waitKey(0);//Waiting for Keystroke
return 0;
} ผลลัพธ์
