简体   繁体   English

如何使用 c++ opencv 找到区域矩形?

[英]how to find area rectangle using c++ opencv?

I'm use this code for this but I have to find out area of rectangle我为此使用此代码,但我必须找出矩形区域

import cv2
import numpy as np
import matplotlib.pyplot as plt

whiteblankimage = 255 * np.ones(shape=[60, 60, 3], dtype=np.uint8)

cv2.rectangle(whiteblankimage, pt1=(20,20), pt2=(40,40), color=(0,0,255), thickness=1)

plt.imshow(whiteblankimage)

plt.show()

If pt1 = {.x = 20, .y = 20 } and pt2 = {.x = 40, .y = 40 } and pt1 is a vertex of the rectangle and pt2 the opposite vertex to pt1 , then the width of the rectangle is abs(pt2.x - pt1.x) and the height of the rectangle is abs(pt2.y - pt1.y) , therefore the area is equal to width * height .如果pt1 = {.x = 20, .y = 20 }pt2 = {.x = 40, .y = 40 }并且pt1是矩形的一个顶点,而pt2pt1的对面顶点,那么矩形的widthabs(pt2.x - pt1.x) ,矩形的heightabs(pt2.y - pt1.y) ,因此area等于width * height

In your example: area = abs(40 - 20) * abs(40 - 20) = 20 * 20 = 400 .在您的示例中: area = abs(40 - 20) * abs(40 - 20) = 20 * 20 = 400

But let's be serious, cv::rectangle is a drawing function, not a class.但让我们严肃点, cv::rectangle是绘图 function,而不是 class。 You could use cv::Rect , which has a member function to calculate the area .您可以使用具有成员 function 的cv::Rect来计算area In addition to that, cv::Rect can be constructed from two points.除此之外, cv::Rect可以从两点构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM