Reputation: 1750
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()
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!!
Upvotes: 1
Views: 2713
Reputation: 879501
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:
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')
Upvotes: 1