cv2.imshow("jiangzaotu.jpg",np.uint8(out_img))怎么改能带坐标显示
时间: 2024-03-02 21:49:41 浏览: 53
如果你想在显示图像的同时显示某个点的坐标,可以在图像上使用cv2.putText函数将其添加到图像上。下面是一个示例代码:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread('jiangzaotu.jpg')
# 假设你要显示的坐标是(100, 200)
x, y = 100, 200
# 在图像上添加坐标
cv2.putText(img, f'({x}, {y})', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们先读取了一张图像,然后定义了要显示的坐标是(100, 200)。然后使用cv2.putText函数在图像上添加了这个坐标,最后显示图像。注意,这里使用的坐标是图像上的坐标,而不是窗口上的坐标。
相关问题
cv2.imshow("jiangzaotu.jpg",np.uint8(out_img))怎么显示带坐标系的图像
如果你想在图像上显示坐标系,并将其显示出来,可以使用cv2.line()函数在图像上绘制直线。下面是一个示例代码:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread('jiangzaotu.jpg')
# 定义坐标系起点
origin = (50, 50)
# 绘制坐标系
cv2.line(img, origin, (origin[0], origin[1] + 100), (0, 255, 0), 2) # y轴
cv2.line(img, origin, (origin[0] + 100, origin[1]), (0, 0, 255), 2) # x轴
# 定义要显示的点的坐标
x, y = 100, 200
# 在图像上添加坐标
cv2.putText(img, f'({x}, {y})', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 1, cv2.LINE_AA)
# 显示图像
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
在这个示例中,我们先读取了一张图像,然后定义了坐标系起点为(50, 50)。我们使用cv2.line函数分别在图像上绘制了x轴和y轴。然后我们定义了要显示的点的坐标为(100, 200),并使用cv2.putText函数在图像上添加了该点的坐标。最后,我们用cv2.imshow函数显示了带有坐标系和坐标的图像。
import cv2 import numpy as np depth_image = cv2.imread('f.png', cv2.IMREAD_UNCHANGED) depth_image = depth_image / 1000.0 cv2.imshow('Depth Image', depth_image) cv2.waitKey(0) # 初始化灰度图像,注意这里创建的是单通道的8位灰度图像 Gray = np.zeros((depth_image.shape[0], depth_image.shape[1]), dtype=np.uint8) # 最大最小深度值 max = 255 # 注意:如果原深度图像只有8位,则应该将其设为255 min = 0 # 遍历每个像素,并进行深度值映射 for i in range(depth_image.shape[0]): data_gray = Gray[i] data_src = depth_image[i] for j in range(depth_image.shape[1]): if data_src[j] < max and data_src[j] > min: data_gray[j] = int((data_src[j] - min) / (max - min) * 255.0) else: data_gray[j] = 255 # 深度值不在范围内的置为白色 # 输出灰度图像,并保存 cv2.imwrite('/home/witney/test/0.jpg', Gray) cv2.imshow('gray', Gray) cv2.waitKey(0) #对图像进行二值化处理以便于轮廓检测 ret, thresh = cv2.threshold(Gray, 127, 255, cv2.THRESH_BINARY) cv2.imshow('thresh', thresh) cv2.waitKey(0) # 读取文本文件中的坐标位置信息 with open('f.txt', 'r') as f: positions = [] for line in f.readlines(): x1, y1, x2, y2 = map(float, line.strip().split(' ')) positions.append((x1, y1, x2, y2)) # 循环遍历每个坐标位置信息,绘制矩形框并截取图片内容 for i, pos in enumerate(positions): x1, y1, x2, y2 = pos # 根据坐标位置信息绘制矩形框 cv2.rectangle(thresh, (x1, y1), (x2, y2), (0, 255, 0), 2) # 利用数组切片功能截取图片中的内容 crop_img = thresh[y1:y2, x1:x2] # 保存截取的图片 cv2.imwrite(f'crop_image_{i}.jpg', crop_img)
这段代码中,第一行import cv2后面应该有一个空格,否则会出现语法错误。此外,如果您的f.png文件是16位深度图像,则需要使用cv2.IMREAD_UNCHANGED标志来读取,否则会被默认解释为8位深度图像。
另外,代码中的“max”变量应该命名为“max_depth”或类似的名称,以避免与Python内置函数“max()”发生冲突。
最后,你应该在程序的结尾处添加cv2.destroyAllWindows()语句,以确保所有的OpenCV窗口都已关闭。
修改后的代码如下:
```python
import cv2
import numpy as np
depth_image = cv2.imread('f.png', cv2.IMREAD_UNCHANGED) # 读取16位深度图像
depth_image = depth_image / 1000.0
cv2.imshow('Depth Image', depth_image)
cv2.waitKey(0)
# 初始化灰度图像,注意这里创建的是单通道的8位灰度图像
Gray = np.zeros((depth_image.shape[0], depth_image.shape[1]), dtype=np.uint8)
# 最大最小深度值
max_depth = 255 # 注意:如果原深度图像只有8位,则应该将其设为255
min_depth = 0
# 遍历每个像素,并进行深度值映射
for i in range(depth_image.shape[0]):
data_gray = Gray[i]
data_src = depth_image[i]
for j in range(depth_image.shape[1]):
if data_src[j] < max_depth and data_src[j] > min_depth:
data_gray[j] = int((data_src[j] - min_depth) / (max_depth - min_depth) * 255.0)
else:
data_gray[j] = 255 # 深度值不在范围内的置为白色
# 输出灰度图像,并保存
cv2.imwrite('/home/witney/test/0.jpg', Gray)
cv2.imshow('gray', Gray)
cv2.waitKey(0)
#对图像进行二值化处理以便于轮廓检测
ret, thresh = cv2.threshold(Gray, 127, 255, cv2.THRESH_BINARY)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
# 读取文本文件中的坐标位置信息
with open('f.txt', 'r') as f:
positions = []
for line in f.readlines():
x1, y1, x2, y2 = map(float, line.strip().split(' '))
positions.append((x1, y1, x2, y2))
# 循环遍历每个坐标位置信息,绘制矩形框并截取图片内容
for i, pos in enumerate(positions):
x1, y1, x2, y2 = pos
# 根据坐标位置信息绘制矩形框
cv2.rectangle(thresh, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
# 利用数组切片功能截取图片中的内容
crop_img = thresh[int(y1):int(y2), int(x1):int(x2)]
# 保存截取的图片
cv2.imwrite(f'crop_image_{i}.jpg', crop_img)
cv2.destroyAllWindows()
```
阅读全文
相关推荐
















