img = np.uint8(img)有什么作用
时间: 2023-10-24 21:07:53 浏览: 46
将变量 `img` 转换为 `uint8` 数据类型的目的是将其转换为无符号8位整数类型。在图像处理中,常用的像素值范围是0到255之间的整数,其中0代表黑色,255代表白色。通过将图像数据类型设置为 `uint8`,可以确保像素值在这个范围内,以便进行正确的图像处理操作。这个转换通常在读取图像数据或进行数据处理操作之前进行。
相关问题
drawing = np.zeros(img.shape[:], dtype=np.uint8)
This line of code creates a numpy array called 'drawing' with the same shape as the input image (img) and data type 'unsigned integer 8-bit' (dtype=np.uint8). The array is initialized with all values set to zero.
This is commonly used in computer vision and image processing tasks to create a blank canvas where we can draw lines, shapes or other objects on top of the input image. The resulting 'drawing' array can then be overlaid onto the original image to visualize the changes made.
import matplotlib.pyplot as plt import matplotlib.image as mping import numpy as np img = mping.imread('D:\作业\哆啦a梦.jpg') plt.figure(1) plt.imshow(img) a = img.shape[0] b = img.shape[1] # 只保留左半部分,右半部分置为白色 img2 = np.ones((a, b, 3), dtype=np.uint8) * 255 img2[:, :b//2, :] = img[:, :b//2, :] img2[:, b//2:, :] = 255 plt.figure(2) plt.imshow(img2) # 左右镜像 img3 = np.ones((a, b, 3), dtype=np.uint8) * 255 img3[:, :b//2, :] = img[:, :b//2, :] img3[:, b//2:, :] = np.fliplr(img[:, :b//2, :]) plt.figure(3) plt.imshow(img3)完善代码
import matplotlib.pyplot as plt
import matplotlib.image as mping
import numpy as np
img = mping.imread('D:\作业\哆啦a梦.jpg')
plt.figure(1)
plt.imshow(img)
a = img.shape[0]
b = img.shape[1]
# 只保留左半部分,右半部分置为白色
img2 = np.ones((a, b, 3), dtype=np.uint8) * 255
img2[:, :b//2, :] = img[:, :b//2, :]
img2[:, b//2:, :] = 255
plt.figure(2)
plt.imshow(img2)
# 左右镜像
img3 = np.ones((a, b, 3), dtype=np.uint8) * 255
img3[:, :b//2, :] = np.fliplr(img[:, b//2:, :])
img3[:, b//2:, :] = img[:, :b//2, :]
plt.figure(3)
plt.imshow(img3)
plt.show()
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)