Convert imshow object to image directly for edge detection? - python

Is there a way to convert the imshow object to an image directly in python 2.7 code? I'm trying to obtain the spectrogram of a wav file and use the image to detect edges in it.
I have the code to get the spectrogram, I have the code to detect edges. Now, I'm manually saving the spectrogram and then detecting edges in the image. Is there a way to do it directly with the imshow object in python ?

Using PIL, you can use:
import PIL
from matplotlib import pyplot as plt
plt.plot(range(5), range(5))
canvas = plt.get_current_fig_manager().canvas
plt.draw()
PIL_image = PIL.Image.frombuffer('RGBA', canvas.get_width_height(), canvas.buffer_rgba())
Tried on jupyter, but how it works may depend on the specific backend, so try it for yourself (for me it also mirrored the image, but that's easy to fix afterward, I think).
Also, you would have to draw only the image without the ticks, see scipy: savefig without frames, axes, only content.

Related

PyVista plotter unable to produce background image for multiple subplots

I am trying to render a mesh in Python using pyvista.Plotter() while trying to show images alongside the rendered mesh. The code is currently in the form of
import pyvista as pv
from pyvista import examples
filenames = (['filename1.jpg','filename2.jpg','filename3.jpg',])
mesh = pv.PolyData('meshfile.ply')
p = pv.Plotter(shape='1|3')
p.subplot(0)
p.add_mesh(mesh)
t=1
for i in filenames:
p.subplot(t)
p.add_background_image(i)
#p.add_mesh(examples.load_airplane(), show_edges=False)
t +=1
where I thought the Plotter.add_background_image() would be the most convenient way to plot images using PyVista. The commented out line in the for loop actually produces the right arrangement but I would like the smaller plots to have background images rather than another mesh. However, only the final image file is actually shown and it is the background image of p.subplot(0) which should not have a background image. Would there be a more convenient way of displaying images alongside a pyvista 3d-rendered window?
Looking at the documentation of Plotter.add_background_image():
add_background_image(image_path, scale=1, auto_resize=True, as_global=True)
as_global (bool, optional) – When multiple render windows are present, setting as_global=False will cause the background to only appear in one window.
So you might just have to call the method as
p.add_background_image(i, as_global=False)

Add axis to PIL image

Is there a way to save large (4000000, 200, 3) numpy RGB array as an image with axis using PIL? Or maybe any other packages?
I tried use matplotlib but it has weak savefig method whic not allow me to save large figures
So then I try use PIL and it works ok but not allow me to add axis to the image

How to save an image plotted with matplolib 1.5

I want to save an image plotted with matplotlib. For that, I use the function savefig which has different parameters.
The problem is that when I saved the image, this function add additional white pixel.
In short, I would like to save the image I draw with the original size. In other words, if the data I draw has a dimension of 1000x560, I save the image with those dimensions without additional white parts.
Thus in this way a pixel of the saved image coincides with the pixel that the figure of matplotlib can see.
I'm using python 2.7
Can anyone help please?
Thanks
from matplotlib import pyplot as plt
plt.savefig('foo.png', bbox_inches='tight')

Can matshow display an image as imshow with PIL or another library?

I saw this post talking about matshow and PIL (PIL/pillow image output is redder than the desired RGB values) but usually when we want to display an image we use imshow and PIL. I am asking if there is a way to do that with matshow and PIL only.
Matplotlib matshow is a wrapper for imshow, in that it "sets origin to ‘upper’, ‘interpolation’ to ‘nearest’ and ‘aspect’ to equal."
Therefore, anything that is possible with one of them is equally possible with the other.

How to display axes on an image with opencv in python

Is there a way to show the row and column axes when displaying an image with cv2.imshow()? I am using the python bindings for opencv3.0
Not that I am aware of.
However, since you are using Python you are not constrained to use the rudimentary plotting capabilities of OpenCV HighGUI.
Instead, you can use the much more competent matplotlib library (or any of the other available Python plotting libraries).
To plot an image, including a default axis you do
import matplotlib.pyplot as plt
plt.imshow(image, interpolation='none') # Plot the image, turn off interpolation
plt.show() # Show the image window
I'm not sure I fully understand the question due to lack of info.
However you can use OpenCV's draw line function to draw a line from the example points (10,10) to (10,190), and another from (10,190) to (190,190)
On an example image that is 200X200 pixels in size, this will draw a line down the left hand side of the image, and a line along the bottom. You can then plot numbers or whatever you want along this line at increments of X-pixels.
Drawing text/numbers to an image is similar to drawing a line.
Once you have drawn the image, show with the usual image.imshow().
See OpenCV's drawing documentation here:
http://docs.opencv.org/modules/core/doc/drawing_functions.html
And an example to get you going can be found here:
http://opencvexamples.blogspot.com/2013/10/basic-drawing-examples.html#.VMj-bUesXuM
Hope this helps.

Categories

Resources