image-segmentation-keras icon indicating copy to clipboard operation
image-segmentation-keras copied to clipboard

Thank you for your reply.

Open dehai-bai opened this issue 4 years ago • 1 comments
trafficstars

Thank you for your reply. In the data set "dataset1" you provided, the annotation pictures are also completely black.

I don’t know how to use the example code you provided to generate annotated images。 I created four folders. As the picture shows: 屏幕截图 2021-03-20 114704 I put the training and test data sets into the two folders, and how can I use this code to generate annotation files in the appropriate folder.

This is what you provide: `import cv2 import numpy as np

ann_img = np.zeros((30,30,3)).astype('uint8') ann_img[ 3 , 4 ] = 1 # this would set the label of pixel 3,4 as 1

cv2.imwrite( "ann_1.png" ,ann_img )` If I can receive your reply, it will be my honor.

Originally posted by @dehai-bai in https://github.com/divamgupta/image-segmentation-keras/issues/281#issuecomment-803239956

dehai-bai avatar Mar 20 '21 08:03 dehai-bai

Annotation images are grayscale images with pixel values according the class (i.e, values from 0 to 13 respectively). Here I provide you a code where you can read and plot an annotation image from the dataset1 set, and then generating a new annotation sample and save it to disk:

import numpy as np
import glob 
from PIL import Image
import matplotlib.pyplot as plt
import cv2

#Annotation images folder path
train_label = glob.glob("/annotations_prepped_train/*.png")

#Init annotated images dataset
y_train = np.zeros([len(train_img), 100, 100]).astype("uint8")

#Extract images  
for i in range(len(train_label)):
    
    #Read image
    im = Image.open(train_label[i])

    #Resize image
    im_r = im.resize((100, 100))
    im_r = np.array(im_r)
    
    #Add image to set
    y_train[i, :, :] = im_r
    
#See annotation sample size 
print("Size of image sample: " + str(y_train[0, :, :]))

#Plot image 
plt.figure(1)
plt.imshow(y_train[0,:,:])
plt.title("Annotation sample")
plt.show()

#Generate new annotation sample
sample = np.zeros([100, 100]).astype("uint8")

#Set class values 
sample[20, 20] = 5

#Save image sample
cv2.imwrite("image_sample.png", sample)

So, you can generate your custom annotation images (as grayscale images with pixel values between 0 to the class number), and then save they to disk.

iqedgarmg avatar May 28 '21 17:05 iqedgarmg