简体   繁体   中英

How can I replace `plt.imsave` with `cmap` option set to `gray` with opencv operations?

This is the source image I am working with:

车道分割的源图像

I am using this github repository (the file I'm using is tools/test_lanenet.py ) to do binary lane segmentation. now I get this image:

操作后得到的二进制图像

The second image is actually an image resulted from this command:

# 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')

First I have to do the same operation with opencv module and preferably faster.

In next operation I have to map the lanes segmented in second image on the source image road lanes. I think I have to use the second image as mask and use cv2.bitwise_and to do job right? Can anybody help me?

thank you guys

If you want to color the image where the mask exists, then this is one way using Python/OpenCV. In place of bitwise_and, you simply have to do numpy coloring where the mask is white. Note again, your images are not the same size and I do not know how best to align them. I leave that to you. I am using your two input images as in my other answer. The code is nearly the same.

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)


在此处输入图片说明

Your images are not the same size. To mask the black/white image onto the color image, they need to align. I tried to simply crop them to the same minimum dimensions at the top left corner, but that did not align them properly.

However, this Python/OpenCV code will give you some idea how to start once you figure out how to align them.

Color Input:

在此处输入图片说明

B/W Lane Image:

在此处输入图片说明

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)

# make thresh 3 channels as mask
mask = cv2.merge((thresh, thresh, thresh))

# apply mask to color image
result = cv2.bitwise_and(img, mask)

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

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


在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM