移动和写入图像的像素 -Python-
Moving and writing pixels of an image -Python-
好的,我在 python 中遇到了新手问题。
所以我有一个列表,其中包含我感兴趣的一些像素的坐标。
我想将这些像素移动到另一个图像或将这些像素保存在新图像中。
到目前为止,我正在使用 cv2 和 matplotlib,我什至想过用 RGB 值保存坐标并将其写入另一个图像,但我不知道如何开始。
对不起,如果这是一个愚蠢的问题,谢谢
您可以像在 numpy 数组中一样执行此操作。例如,您想要使用所有三个通道获取 RGB 图像的前 10x10 部分。基本上;
new_image = np.zeros([10, 10, 3], dtype=np.uint8)
new_image[:, :, :] = old_image[:10, :10, :]
img_src = cv2.imread('someimg.jpg')
h, w, c = img_src.shape
# an empty black image for saving pixels
img_dest = np.zeros((h, w, c), dtype=np.uint8)
for ixy in xy_list:
x, y = xy_list[ixy]
# copy pixels at given locations
img_dest[x, y, :] = img_src[x, y, :]
cv2.imwrite('output.jpg', img_dest)
这能解决您的问题吗?
好的,我在 python 中遇到了新手问题。 所以我有一个列表,其中包含我感兴趣的一些像素的坐标。 我想将这些像素移动到另一个图像或将这些像素保存在新图像中。 到目前为止,我正在使用 cv2 和 matplotlib,我什至想过用 RGB 值保存坐标并将其写入另一个图像,但我不知道如何开始。 对不起,如果这是一个愚蠢的问题,谢谢
您可以像在 numpy 数组中一样执行此操作。例如,您想要使用所有三个通道获取 RGB 图像的前 10x10 部分。基本上;
new_image = np.zeros([10, 10, 3], dtype=np.uint8)
new_image[:, :, :] = old_image[:10, :10, :]
img_src = cv2.imread('someimg.jpg')
h, w, c = img_src.shape
# an empty black image for saving pixels
img_dest = np.zeros((h, w, c), dtype=np.uint8)
for ixy in xy_list:
x, y = xy_list[ixy]
# copy pixels at given locations
img_dest[x, y, :] = img_src[x, y, :]
cv2.imwrite('output.jpg', img_dest)
这能解决您的问题吗?