簡體   English   中英

在OpenCV中繪制矩形

[英]Draw rectangle in OpenCV

我想使用此函數在OpenCV中繪制一個矩形:

rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

但是當我使用它時,我遇到了一些錯誤。 我的問題是:有人能用一個例子來解釋這個功能嗎? 我發現了一些例子,但有另一個功能:

rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

這個例子關於第二個函數:

rectangle(image, pt2, pt1, Scalar(0, 255, 0), 2, 8, 0);

我理解這個函數,但是第一個函數我在參數Rect面臨一個問題。 我不知道怎么會變得更加致命呢?

接受兩個cv::Pointcv::rectangle函數同時采用矩形的左上角和右下角( 文檔中分別為pt1和pt2)。 如果該矩形與接受cv::Rectcv::rectangle函數一起使用,那么您將得到相同的結果。

// just some valid rectangle arguments
int x = 0;
int y = 0;
int width = 10;
int height = 20;
// our rectangle...
cv::Rect rect(x, y, width, height);
// and its top left corner...
cv::Point pt1(x, y);
// and its bottom right corner.
cv::Point pt2(x + width, y + height);
// These two calls...
cv::rectangle(img, pt1, pt2, cv::Scalar(0, 255, 0));
// essentially do the same thing
cv::rectangle(img, rect, cv::Scalar(0, 255, 0))

這是在圖像上繪制預定義矩形的簡單示例

using namespace cv;

int main(){
Mat img=imread("myImage.jpg");

Rect r=Rect(10,20,40,60);
//create a Rect with top-left vertex at (10,20), of width 40 and height 60 pixels.

rectangle(img,r,Scalar(255,0,0),1,8,0);
//draw the rect defined by r with line thickness 1 and Blue color

imwrite("myImageWithRect.jpg",img);


return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM