opencv中计算两个contour 的最大距离代码
时间: 2024-02-07 15:02:52 浏览: 52
以下是使用OpenCV计算两个轮廓最大距离的示例代码:
```python
import cv2
# 读取图像并转换为灰度图像
image = cv2.imread('image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 阈值化处理
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 计算最大距离
max_dist = 0
for i in range(len(contours)):
for j in range(i+1, len(contours)):
dist = cv2.matchShapes(contours[i], contours[j], cv2.CONTOURS_MATCH_I1, 0)
if dist > max_dist:
max_dist = dist
print("最大距离为:", max_dist)
```
在这个示例中,我们首先读取图像并将其转换为灰度图像,然后进行阈值化处理并查找轮廓。接下来,我们使用一个嵌套的循环计算所有可能的轮廓间距离,并找到最大距离。最后,我们输出最大距离的值。
相关问题
opencv图像处理怎么找轮廓两点的距离
要找轮廓中的两个点之间的距离,可以通过以下步骤实现:
1. 使用findContours函数找到轮廓。
2. 使用approxPolyDP函数将轮廓近似为一个多边形,以便更好地计算轮廓的长度。
3. 使用arcLength函数计算轮廓的长度。
4. 使用cv::Point函数找到轮廓上的两个点。
5. 使用norm函数计算两个点之间的距离。
下面是示例代码:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("example.jpg", IMREAD_GRAYSCALE);
// 二值化图像
Mat binaryImage;
threshold(image, binaryImage, 128, 255, THRESH_BINARY);
// 查找轮廓
vector<vector<Point>> contours;
findContours(binaryImage, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// 近似轮廓为多边形
vector<Point> contour = contours[0];
vector<Point> approx;
approxPolyDP(contour, approx, 0.01 * arcLength(contour, true), true);
// 找到两个点
Point p1 = approx[0];
Point p2 = approx[approx.size() - 1];
// 计算两点之间的距离
double distance = norm(p1 - p2);
cout << "Distance between two points: " << distance << endl;
return 0;
}
```
在此示例中,我们首先读取一张图像,然后将其转换为二值图像。接下来,我们使用findContours函数查找轮廓,并使用approxPolyDP函数将其近似为多边形。然后,我们找到轮廓的第一个和最后一个点,并计算它们之间的距离。最后,我们输出距离结果。
使用opencv 编写能够识别图片手势的代码
好的,下面是一个简单的使用 OpenCV 和 Python 编写的手势识别代码示例:
```python
import cv2
import numpy as np
# 将图像转换为黑白格式
def preprocess(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
_, thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
return thresh
# 查找手掌轮廓
def find_contour(thresh):
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = max(contours, key=lambda x: cv2.contourArea(x))
return cnt
# 计算手掌的凸包和凸缩点
def convex_hull(cnt):
hull = cv2.convexHull(cnt)
defects = cv2.convexityDefects(cnt, cv2.convexHull(cnt, returnPoints=False))
return hull, defects
# 计算手势的数量
def count_fingers(defects, cnt):
count = 0
for i in range(defects.shape[0]):
s, e, f, d = defects[i, 0]
start = tuple(cnt[s][0])
end = tuple(cnt[e][0])
far = tuple(cnt[f][0])
# 计算手指长度和手掌中心距离
a = np.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = np.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = np.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = np.arccos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))
# 根据角度判断手指数量
if angle <= np.pi / 2:
count += 1
return count
# 主函数
def main():
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
thresh = preprocess(frame)
cnt = find_contour(thresh)
hull, defects = convex_hull(cnt)
count = count_fingers(defects, cnt)
# 在图像上绘制手指数量
cv2.putText(frame, str(count), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
```
该代码使用 OpenCV 对摄像头捕获到的图像进行处理,通过计算手指数量来识别手势。具体实现过程包括以下几个步骤:
1. 将图像转换为黑白格式,并进行高斯模糊和二值化处理,以便更好地查找手掌轮廓。
2. 查找手掌轮廓,这里使用了 OpenCV 中的 `findContours` 函数。
3. 计算手掌的凸包和凸缩点,这里使用了 `convexHull` 和 `convexityDefects` 函数。
4. 计算手势的数量,根据手指长度和手掌中心距离的关系,通过计算手势中相邻两个凸缩点之间的角度,来判断手指的数量。
5. 在图像上绘制手指数量,以便展示结果。
希望这个示例能帮助你理解手势识别的基本原理和实现方法。