Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用opencv操作将'plt.imsave'替换为'cmap'选项设置为'gray'?_Python_Opencv_Matplotlib - Fatal编程技术网

Python 如何使用opencv操作将'plt.imsave'替换为'cmap'选项设置为'gray'?

Python 如何使用opencv操作将'plt.imsave'替换为'cmap'选项设置为'gray'?,python,opencv,matplotlib,Python,Opencv,Matplotlib,这是我正在处理的源图像: 我使用github存储库(我使用的文件是tools/test\u lanenet.py)来进行二进制车道分割。现在我看到这张图片: 第二个图像实际上是由以下命令生成的图像: # this line results in an array with the shape of (512, 256). this is just a hypothetical line of code. what I really care is the line which saves t

这是我正在处理的源图像:

我使用github存储库(我使用的文件是
tools/test\u lanenet.py
)来进行二进制车道分割。现在我看到这张图片:

第二个图像实际上是由以下命令生成的图像:

# this line results in an array with the shape of (512, 256). this is just a hypothetical line of code. what I really care is the line which saves the image with matplotlib library. 
binary_seg_image = lane_segmenter.binary_segment()
# this line saves the image
plt.imsave('binary_image_plt.png', binary_seg_image[0] * 255, cmap='gray')
首先,我必须对opencv模块执行相同的操作,最好更快

在下一个操作中,我必须将第二个图像中分割的车道映射到源图像道路车道上。我想我必须使用第二幅图像作为遮罩,并使用
cv2.bitwise_和
来完成这项工作,对吗?有人能帮我吗


谢谢大家

你们的图片大小不一样。要将黑白图像遮罩到彩色图像上,它们需要对齐。我试图简单地将它们裁剪到左上角相同的最小尺寸,但这并没有使它们正确对齐

然而,这段Python/OpenCV代码将为您提供一些想法,一旦您弄清楚如何对齐它们,就可以开始了

颜色输入:

B/W车道图像:



如果您想给存在掩码的图像上色,那么这是使用Python/OpenCV的一种方法。代替按位_和,您只需在遮罩为白色的地方进行numpy着色即可。请再次注意,您的图像大小不同,我不知道如何最好地对齐它们。我把这个留给你。我正在使用你的两个输入图像作为我的另一个答案。代码几乎相同

import cv2
import numpy as np

# read image
img = cv2.imread('road.png')
ht, wd, cc = img.shape
print(img.shape)

# read mask as grayscale
gray = cv2.imread('road_mask.png', cv2.IMREAD_GRAYSCALE)
hh, ww = gray.shape
print(gray.shape)

# get minimum dimensions
hm = min(ht, hh)
wm = min(wd, ww)
print(hm, wm)

# crop img and gray to min dimensions
img = img[0:hm, 0:wm]
gray = gray[0:hm, 0:wm]

# threshold gray as mask
thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)[1]
print(thresh.shape)

# apply mask to color image
result = img.copy()
result[thresh==255] = (0,0,255)

cv2.imshow('image', img)
cv2.imshow('gray', gray)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('road_colored_by_mask.png', result)


阅读opencv文档。请参阅第页的imwrite()。imwrite()不添加任何颜色贴图。因此,如果您的图像是二进制、灰度或彩色的,则写入的图像将是相同的。请尝试使用'cv2.imwrite('binary\u image\u plt.png',(binary\u seg\u image[0]*255).astype('uint8')。抱歉,我不知道lane\u segmenter.binary\u segment,所以我不确定它的输出。@fmw42我使用了相同的函数,但没有
.astype('uint8')
,它不起作用。所以我想这是关键。那么第二部分在源图像上映射(512,256)数组呢?请参阅下面的答案。但我实际上需要在源图像上绘制二进制图像中的白色通道。但是你做的恰恰相反。看看我的新答案。
import cv2
import numpy as np

# read image
img = cv2.imread('road.png')
ht, wd, cc = img.shape
print(img.shape)

# read mask as grayscale
gray = cv2.imread('road_mask.png', cv2.IMREAD_GRAYSCALE)
hh, ww = gray.shape
print(gray.shape)

# get minimum dimensions
hm = min(ht, hh)
wm = min(wd, ww)
print(hm, wm)

# crop img and gray to min dimensions
img = img[0:hm, 0:wm]
gray = gray[0:hm, 0:wm]

# threshold gray as mask
thresh = cv2.threshold(gray,128,255,cv2.THRESH_BINARY)[1]
print(thresh.shape)

# apply mask to color image
result = img.copy()
result[thresh==255] = (0,0,255)

cv2.imshow('image', img)
cv2.imshow('gray', gray)
cv2.imshow('thresh', thresh)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save results
cv2.imwrite('road_colored_by_mask.png', result)