0
I have a code that finds the edges around what was labeled in the image and places the value of -1 in the pixels of that border found. I’m having trouble converting the image already with the edges -1 in its value, from BGR to RGB. because when trying to convert, the value -1 is lost and only find values 0 (rest) and 255 (labeled) in the saved image. I wanted to keep the values 0, -1, 255 when saving the image in RGB format.
Follows code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib
import urllib.request
from PIL import Image
def mostrar_imagem(img):
plt.imshow(img)
plt.colorbar()
plt.show()
img = cv2.imread('framepng/frame24.png',0)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img, kernel, iterations = 1)
img = cv2.dilate(erosion, kernel, iterations = 1)
_, binario = cv2.threshold(img, 90, 255, cv2.THRESH_BINARY)
copia = img.copy()
tmp = cv2.findContours(binario, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contornos = tmp[0] if len(tmp) == 2 else tmp[1]
img_contornos = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
mostrar_imagem(img_contornos)
cv2.drawContours(img_contornos, contornos, -1, (0, 255, 0), 1)
mask = cv2.inRange(img_contornos, (0, 254, 0), (0, 255, 0))
mostrar_imagem(mask)
binario = np.int16(binario)
binario[mask == 255] = -1
binario = np.ma.masked_where(binario == -1, binario)
cmap = matplotlib.cm.Greys # Can be any colormap that you want after the cm
cmap.set_bad(color='red')
plt.show()
matplotlib.image.imsave('framepng-1/teste3.png', binario)
Try to convert the image to RGB before the use matplotlib to save the image.
– Anna Maule
I did just that, but as I said after doing that the value of -1 that is the edge that I mentioned above is lost. The current image I have inside the binary variable is with the colors purple (rest), white (border), yellow (labeled), my intention is only to change the colors, putting as purple = black, white and yellow = white.
– marcelim122