Fig.1 Polygon with black background
Fig.2 desired output
Fig.3 Input
There are some images with a black background and white polygon inside (fig.1) since some lines of the polygon are not straight the value of colors in some pixels are not 0 or 255. I tried to change the color values to 0 for outside of the polygon and 255 for inside. This code is working perfectly for single image to change the colors (fig.2) but when I put it in the loop for all images (1024) it's not changing some values (fig 3) for instance please see pixel (137,588) in figs 1 and 2.
from skimage import io
import matplotlib.pyplot as plt
import scipy.io as spio
import numpy as np
pixels = 600
my_dpi = 100
num_geo=1024
## Load coordinates
mat = spio.loadmat('coordinateXY.mat', squeeze_me=True)
coord = mat['coordxy']*10
for i in range(num_geo):
geo = coord[:, :, i]
print(coord[:, :, i])
fig = plt.figure(num_geo,figsize=( pixels/my_dpi, pixels/my_dpi),facecolor='k', dpi=my_dpi)
plt.axes([0,0,1,1])
rectangle = plt.Rectangle((-300, -300), 600, 600, fc='k')
plt.gca().add_patch(rectangle)
polygon = plt.Polygon(coord[:, :, i],color='w')
plt.gca().add_patch(polygon)
plt.axis('off')
plt.axis([-300,300,-300,300])
plt.savefig('figure/%d.jpg' % i, dpi=my_dpi)
# Save as numpy file
img_mat = io.imread('figure/%d.jpg' % i)
np.save('img_mat.npy', img_mat)
data = np.load('img_mat.npy')
# # adjust the colors and save the revised version
data1 = np.where(data<180, 0, data)
data2 = np.where(data1>185, 255, data1)
arr=data2
plt.imsave('figureRev/%d.jpg' % i,arr)
plt.close()


