2

Fig.1 Polygon with black background

Polygon with black background

Fig.2 desired output

without for-loop

Fig.3 Input

enter image description here

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()
6
  • Do you have some example input/output, a minimal reproducible example? Commented Jan 22, 2020 at 2:27
  • The image you see is without using the loop and if click on number 2 you can see the image which is gendered inside the for-loop. Commented Jan 22, 2020 at 3:19
  • Please try to improve your question - I have no idea what the problem is! I don't see any images with a black background and white polygons inside... I only see red and blue rectangles with white digits inside. Thank you. Commented Jan 22, 2020 at 13:58
  • Thanks for comment. I add the image and tried to improve the question. Commented Jan 22, 2020 at 14:26
  • 1
    I still don't understand. What has the polygon with the black background got to do with the red and blue rectangles? What is the input? What is the correct output of your program? Commented Jan 22, 2020 at 14:30

1 Answer 1

1

Oh, I see now!

It's either because you used JPEG which is lossy and allowed to change your data - in which case try PNG format which is lossless.

Or, it's because the diagonal line has been drawn with "anti-aliasing" Wikipedia linkwhich you can turn off if you don't want it. Alternatively, you can threshold your data at say 127 to ensure all values below and equal become zero and all values above become 255.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.