1

I have a zeros image with dimension 720*1280 and I have a list of pixels' coordinates to change:

x = [623, 623, 583, 526, 571, 669, 686, 697, 600, 594, 606, 657, 657, 657, 617, 646, 611, 657, 674, 571, 693, 688, 698, 700, 686, 687, 687, 693, 690, 686, 694]

y = [231, 281, 270, 270, 202, 287, 366, 428, 422, 517, 608, 422, 518, 608, 208, 214, 208, 231, 653, 652, 436, 441, 457, 457, 453, 461, 467, 469, 475, 477, 467] 

here is the scatter plot :

yy= [720 -x for x in y]
plt.scatter(x, yy, s = 25, c = "r")
plt.xlabel('x')
plt.ylabel('y')
plt.xlim(0, 1280)
plt.ylim(0, 720)
plt.show()

enter image description here

here is the code to generate binary image by set the pixel value to 255

image_zeros = np.zeros((720, 1280), dtype=np.uint8)
    for i ,j in zip (x, y):
            image_zeros[i, j] = 255

    plt.imshow(image_zeros, cmap='gray')
    plt.show()

here is the result : What is the problem!!

enter image description here

4
  • @unutbu, it does not work. could you generate the result? please Commented Aug 19, 2018 at 17:28
  • 1
    Resolution is the problem. The plot is less than 1280x720 pixels wide so you cannot see all the values in the array. Commented Aug 19, 2018 at 17:36
  • @Goyo, I did not understand what do you mean. You mean that we cannot show such image resolution using plt.imshow() ? Commented Aug 19, 2018 at 17:44
  • You can show any resolution with imshow as long as your output device supports it. But you cannot show 1280x720 values in a 378x223 picture. Commented Aug 19, 2018 at 17:48

1 Answer 1

1

As Goyo pointed out, the resolution of the image is the problem. The default figure size is 6.4 inches by 4.8 inches, and the default resolution is 100 dpi (at least for the current version of matplotlib). So the default image size is 640 x 480. The figure includes not only the imshow image, but also the tickmarks, ticklabels and the x and y axis and a white border. So there are are even fewer than 640 x 480 pixels available for the imshow image by default.

Your image_zeros has shape (720, 1280). The array is too large to be fully rendered in an image of 640 x 480 pixels.

Thus, to generate white dots using imshow, set the figsize and dpi so that the number of pixels available for the imshow image is bigger than (1280, 720):

import numpy as np
import matplotlib.pyplot as plt

x = np.array([623, 623, 583, 526, 571, 669, 686, 697, 600, 594, 606, 657, 657, 657, 617, 646, 611, 657, 674, 571, 693, 688, 698, 700, 686, 687, 687, 693, 690, 686, 694])
y = np.array([231, 281, 270, 270, 202, 287, 366, 428, 422, 517, 608, 422, 518, 608, 208, 214, 208, 231, 653, 652, 436, 441, 457, 457, 453, 461, 467, 469, 475, 477, 467])

image_zeros = np.zeros((720, 1280), dtype=np.uint8)
image_zeros[y, x] = 255

fig, ax = plt.subplots(figsize=(26, 16), dpi=100)
ax.imshow(image_zeros, cmap='gray', origin='lower')
fig.savefig('/tmp/out.png')

Here is a closeup showing some of the white dots:

enter image description here

To make the white dots easier to see, you may wish to use scatter instead of imshow:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([623, 623, 583, 526, 571, 669, 686, 697, 600, 594, 606, 657, 657, 657, 617, 646, 611, 657, 674, 571, 693, 688, 698, 700, 686, 687, 687, 693, 690, 686, 694])
y = np.array([231, 281, 270, 270, 202, 287, 366, 428, 422, 517, 608, 422, 518, 608, 208, 214, 208, 231, 653, 652, 436, 441, 457, 457, 453, 461, 467, 469, 475, 477, 467])
yy = 720 - y

fig, ax = plt.subplots()

ax.patch.set_facecolor('black')
ax.scatter(x, yy, s=25, c='white')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_xlim(0, 1280)
ax.set_ylim(0, 720)
fig.savefig('/tmp/out-scatter.png')

enter image description here

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.