OpenCV is not graying an image with cv2.cvtColor when displayed with matplotlib - matplotlib

I tried to gray an image but it does not work properly like it should. It just applied a kind of filter as you can see by using cv2.COLOR_BGR2GRAY function. Kindly someone help me to get over with this issue.

Your issue is that you're using matplotlib to show your grayscale image. Matplotlib applies a colormap. By default that's a colorful one.
Add cmap="gray" in your imshow call: plt.imshow(img_gray, cmap="gray")

Related

Pyplot too small to read and png saves as blank image

I have read
Sklearn plot_tree plot is too small
answers but still can't get this to work. I have this decision tree where the text is too small to read. I have tried savefig as suggested in the question in the link, but can only save blank images, not the plot. Please advise how I can view the text in this tree either by explainig how I can save image with high DPI as in the other question or another method. thanks.
plt.figure(figsize=(8, 8))
with plt.rc_context({'figure.facecolor':'black','text.color':'black'}):
plot_model(tuned_dt, plot='tree',)

matplotib: two overlaid transparent images look different when saved as svg or png

I'm trying to overlay two transparent images with matplotlib and save the result, but the result looks different depending on the file type. Specifically it's much more washed-out when saving to svg.
Here's an example. In this case, I could just add the two images before displaying them, but this is just a simple example. In reality what I'm trying to do is more complicated (images of different sizes with different colormaps), so they have to be plotted separately.
Example code:
f, ax = plt.subplots(figsize=(2,2))
ax.imshow(np.eye(3), alpha=.5)
ax.imshow(np.eye(3)[::-1], alpha=.5)
f.savefig('example.png')
f.savefig('example.svg')
The png file looks just like it does on the screen, but the svg file looks washed out. I would like to know how to save as svg, without the washed-out effect (i.e. it should look like it does on the screen).
As a bonus question, why does the png plot appear different depending on the order in which I plot the transparent images? The second image always looks stronger. Interestingly, in the svg, both are equally washed out.
Example saved as png:
Example saved as svg:
matplotlib version: 3.1.3
python version: 3.7.7
Thanks for any tips!
I'll post what I think is going on, but if someone can answer with more legit information I'll accept it.
I think that every time you call imshow with an alpha value, it blends the current image in the axis with the new image, using (new * alpha + current * (1-alpha)). The problem with this is that if you display 10 images each with alpha 0.5, then the first image is attenuated to nothing by the iterative blending, whereas the last image gets to be 50% of the final result. Nonetheless this is apparently the method used for rendering to the screen and saving to png.
In contrast, when saving to svg, it saves each image as a separate overlay with its own alpha. The svg container or renderer then uses some more intelligent method that considers all overlaid images at once. However, in my particular case, this leads to a more washed-out look because all the images are partially transparent.

Mpld3: blurry image

I created a matplotlib figure (matplotlib on server) and displaying it with mpld3.draw_figure (nodejs on client) only gives blurry images, similar to the ones here:
How to 'turn off' blurry effect of imshow() in matplotlib?
mpld3.draw_figure(`chartBox-${round}`,chart,false,true)
Interpolation setting 'nearest', 'none', did not change anything:
plt.imshow(G,interpolation='nearest')
I assume its some interpolation problem... maybe also there is a 0.5 shift, so that gridpoints 1 -> 1.5 and are then interpolated.
First switching back to matplotlib 1.5.1 and then
mpl.use('Agg')
solved it
see here:
Generating a PNG with matplotlib when DISPLAY is undefined

Removing background on a 3d plot Julia

I am having some real trouble trying to plot nice things with Julia.
I first used PyPlot which I was happy with but I had to be able to change the angle on a 3D plots (in that case using surface) so I now use Plots directly with the camera speficication. I would like to remove the background on a 3D plot.
using PyPlot, the following command :
ax=gca(); ax:set_axis_off
works fine.
When using Plots, I tried the following :
using Plots; pyplot(grid=false)
and then my code for the surface, and it does not change the output.
I also tried the first example on this page :https://github.com/JuliaPlots/Plots.jl/pull/695 and grid specification does not work (grids are the same on every area of the layout).
I tried after updating every package and it still does not work.
Does anyone have an idea about what would the problem be please?
Thank you in advance !

Plots Frame (including axes) blackened in Qtconsole

When I launch qtconsole with the --colors=linux option and plot something the frame of the plot is blackened so I cannot see the axes because the qtconsole background is also black.
I used to launch this before without problem but have this problem after a recent update of pandas. I am not sure about what changed but I thought there might be a setting I can change to fix this anyway without worrying about what the update modified that broke this.
It looks like the axes are set to transparent by default (this was not happening before).
The following plots as desired, showing the white axes on black background:
import pandas as pd
import matplotlib.pylab as plt
fig = plt.figure()
fig.patch.set_alpha(1)
temp = pd.Series(range(100))
temp.plot()
I would also like to set this behavior as the default one. I have not been able to do that yet. This seemed like a good lead,
http://matplotlib.org/users/customizing.html
but I could not find an option for exactly that yet.
Any suggestion is welcome. Thank you.