Reputation: 376
I'm working on Mask(Labels) generation for Semantic segmentation problem
I have a NumPy array of 250x321
. It's a single-channel image. I've to annotate this image. But since this is a single-channel image, when trying to save it in the normal mode of matplotlib.pyplot
, dimensions are changed along with a change in the channel (4). When storing just a single-channel image in png
format, the image is all black, hence unable to annotate the image.
So I'm unable to annotate png
format image (All Black) and also unable to use normal matplotlib.pyplot
save function (Dim are changed along with channels)
Can someone suggest me how to achieve this job!
I've used code given below
plt.imshow(data[:, 400:650].T)
plt.tight_layout()
plt.xticks([])
plt.yticks([])
plt.axis('off')
plt.savefig('/content/new.png',bbox_inches='tight', pad_inches=0.0)
The above code is generating an array of shape 245x315x4
when used plt.imread
when in the original array was of shape 250x321x1
.
Upvotes: 0
Views: 1009
Reputation: 4718
First of all, if you want to just save the image, avoid using plt.savefig
and instead use either plt.imsave
(or alternatives from other libraries, e.g. opencv's imwrite
, or the tools from PIL
, etc). The reason for this is that plt.savefig
is implemented to save the entire figure, which means saving axes, labels, text etc. Furthermore, it takes a boolean argument Transparent
which will handle the fourth channel. So, without knowing the inner workings and exact implementation of plt.savefig
, it's a safe bet to say that it shouldn't be used to save grayscale images.
In your case however, since you're not using plt.savefig
to plot anything besides the image itself, there is a simple workaround that I'll come to after covering two points in your question. First, your code is
plt.imshow(data[:, 400:650].T)
but since you want a grayscale image, you should set cmap="gray"
. Second, regarding this part
When storing just a single-channel image in png format, the image is all black
There isn't any info in your question on the range of values that are in your data
, but I'm guessing that this is just an issue of "float" vs "uint8" datatype. Try setting data.astype(float64)
or if using uint8
, make sure you normalize it in [0,255]
and not [0,1]
.
Now regarding the simple workaround, once you've figured out how to correct the above and saved your figure, you'll notice when loading the image with plt.imread
that all your four channels have the same values (assuming you've set cmap=gray
), so you can just select any of the four channels and discard the others.
But again, it's a better idea to use plt.imsave
or opencv.
Upvotes: 1