cv2.putText(ori_img,label,(10,10),cv2.FONT_HERSHEY_DUPLEX,2, (10,255,10), 2) cv2.imshow(label,ori_img) cv2.waitKey(0)
时间: 2024-04-27 07:25:30 浏览: 159
这是使用OpenCV库对原始图像加入文字标签并显示出来的代码,cv2.putText()函数用于在图像上添加文字,第一个参数是原始图像,第二个参数是要添加的文字内容,第三个参数是文字的起始位置坐标,第四个参数是字体类型,第五个参数是字体大小,第六个参数是文字颜色,第七个参数是文字线条宽度。cv2.imshow()函数用于显示图像,第一个参数是窗口名称,第二个参数是图像数据。cv2.waitKey()函数用于等待用户按下按键,参数为0表示无限等待,直到用户按下按键才会继续执行后续代码。
相关问题
import cv2import numpy as np# 读取图像image = cv2.imread('工件图像.jpg')# 转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 使用二值化处理获取缺陷图像ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)# 使用形态学操作进行缺陷的去噪和填充kernel = np.ones((3,3), np.uint8)closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)# 获取缺陷轮廓contours, hierarchy = cv2.findContours(opening, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)# 遍历每一个缺陷,提取几何特征并绘制位置for i in range(len(contours)): # 获取面积和周长 area = cv2.contourArea(contours[i]) perimeter = cv2.arcLength(contours[i], True) # 获取质心 moments = cv2.moments(contours[i]) cx = int(moments['m10'] / moments['m00']) cy = int(moments['m01'] / moments['m00']) # 绘制位置和特征 cv2.drawContours(image, [contours[i]], -1, (0, 255, 0), 2) cv2.circle(image,(cx,cy), 5, (0,0,255), -1) cv2.putText(image, "Area: {:.2f}".format(area), (cx+10, cy-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2) cv2.putText(image, "Perimeter: {:.2f}".format(perimeter), (cx+10, cy+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)# 显示结果cv2.imshow('Defect Detection', image)cv2.waitKey(0)# 保存结果cv2.imwrite('Defect Detection.jpg', image)
这是Python中的两个库,cv2是OpenCV库,用于图像和视频处理,包括读取、写入、显示、处理、分析等。numpy是一个数学库,用于处理数字和数组。一般情况下,当我们读取图像时,使用cv2,而对图片进行处理时,使用numpy。
import cv2 import mediapipe as mp import time cap = cv2.VideoCapture(0) mpHands = mp.solutions.hands hands = mpHands.Hands() mpDraw = mp.solutions.drawing_utils pTime = 0 cTime = 0 while True: success, img = cap.read() imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) results = hands.process(imgRGB) #print(results.multi_hand_landmarks) if results.multi_hand_landmarks: for handLms in results.multi_hand_landmarks: for id, lm in enumerate(handLms.landmark): #print(id,lm) h, w, c = img.shape cx, cy = int(lm.x *w), int(lm.y*h) #if id ==0: cv2.circle(img, (cx,cy), 7, (255,0,255), cv2.FILLED) mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS) cTime = time.time() fps = 1/(cTime-pTime) pTime = cTime cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3) cv2.imshow("Image", img) cv2.waitKey(1)
这段代码使用OpenCV和MediaPipe库实现了手部姿势检测,并在视频流中绘制了手部关键点和连接线。具体实现如下:
1. 导入必要的库和模块:
```
import cv2
import mediapipe as mp
import time
```
2. 初始化摄像头并创建MediaPipe Hands模型:
```
cap = cv2.VideoCapture(0) # 初始化摄像头
mpHands = mp.solutions.hands # 创建MediaPipe Hands模型
hands = mpHands.Hands() # 实例化模型
mpDraw = mp.solutions.drawing_utils # 创建绘图工具
```
3. 循环读取视频帧并进行手部姿势检测:
```
while True:
success, img = cap.read() # 读取视频帧
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 将BGR图像转换为RGB图像
results = hands.process(imgRGB) # 进行手部姿势检测
```
4. 绘制手部关键点和连接线:
```
if results.multi_hand_landmarks:
for handLms in results.multi_hand_landmarks:
for id, lm in enumerate(handLms.landmark):
h, w, c = img.shape
cx, cy = int(lm.x *w), int(lm.y*h)
cv2.circle(img, (cx,cy), 7, (255,0,255), cv2.FILLED)
mpDraw.draw_landmarks(img, handLms, mpHands.HAND_CONNECTIONS)
```
5. 计算并显示帧率:
```
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img,str(int(fps)), (10,70), cv2.FONT_HERSHEY_PLAIN, 3, (255,0,255), 3)
```
6. 显示处理后的帧:
```
cv2.imshow("Image", img)
cv2.waitKey(1)
```
阅读全文