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.
Related
I'm trying to do some image processing with matplotlib. However the y-axis is decreasing bottom up. I want it to be increasing bottom up without flipping the image upside down
I have the following code
import matplotlib.pyplot as plt
import numpy as np
img_path = '/path/to/image.tif'
img = plt.imread(img_path)
plt.imshow(img, cmap = 'gray')
it produces the following image:
The images can be obtained there
I tried plt.gca().invert_yaxis() without success
What shall I do
The default behavior of imshow is to put the origin of the coordinate system in the upper left corner. This is different from plotting scientific data, such as two entities x and y against each other, where the origin, i.e. the point corresponding to the coordinate (0,0), is in the lower left corner, with the (positive) x-axis extending to the right and the (positive) y-axis extending towards the top.
The latter is just scientific convention, though one that goes back centuries. Arguably (albeit near impossible to back up with historical evidence), the x-axis is traditionally aligned left-to-right because that's how text is written in many languages, while the y-axis is oriented towards the top as that's how people intuit an increase — much like the elevation of terrain.
For images, on the other hand, the existing convention is rooted in the memory layout of pixel data and the way one might arrange consecutive pixels on the canvas: from left to right on the first line (by the same logic as above), then from the left again on the next line, and so on for all other lines, going from top to bottom. Just like words arranged on a page — in languages written from left to right and, much more universally so, top to bottom.
It is for that reason that the y-axis in your image is oriented the way it is. To have the y-values increase from the bottom up, you could invoke the option origin='lower' so that the input data is interpreted as per the scientific convention. However, you then also need to flip the image's lines upside down so that, when displayed on the screen, the image appears in its intended orientation. That's because what used to be the first line when the image was loaded into memory now corresponds to the last y-coordinate, the one at the top.
Bottom line (pun not unintended), just call imshow like so in the above code:
plt.imshow(np.flipud(img), cmap='gray', origin='lower')
To illustrate further, here is a self-contained example that demonstrates the behavior:
from imageio import imread
image = imread('https://upload.wikimedia.org/wikipedia/commons'
'/thumb/6/6a/Mona_Lisa.jpg/158px-Mona_Lisa.jpg')
from matplotlib import pyplot
figure = pyplot.figure(tight_layout=True)
(axes1, axes2, axes3) = figure.subplots(nrows=1, ncols=3)
axes1.set_title("origin='upper'")
axes1.imshow(image)
axes2.set_title("origin='lower'")
axes2.imshow(image, origin='lower')
axes3.set_title("'lower' + flipped")
axes3.imshow(image[::-1], origin='lower')
pyplot.show()
The example requires ImageIO to be installed in order to retrieve the sample image. Its output is:
(In the example's code, I used image[::-1] to flip the image, instead of the aforementioned, and equivalent, syntax np.flipud(image). All that does is avoid an explicit import of NumPy (as np), i.e., an extra line of code. Implicitly, NumPy is still doing the work.)
Similar questions:
Matplotlib inverted image
How to use "extent" in matplotlib.pyplot.imshow
How to make imshow read (x,y) coordinates from other Numpy arrays
Why does imshow's extent argument flip my images?
Matlab image and plot with unexpected flip
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')
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.
I would like to blit a 2d image (like a png) to a location in 3-space on a 3D matplotlib figure. Essentially, I want the image to always be facing the user regardless of how the plot is rotated. It would be best if the blitted image did not scale as the user zoomed in and out as well.
One would think that this would be easy to do by accessing the low level rastering functions in matplotlib, but I can't find any documentation that describes what I would like to do.
If you put in an image as an annotation, zooming in on the axes will not scale the image. For example, see this demo. (To run the demo script, replace grace_hopper.png with lena.png.)
For example, just for an easy reference on zooming I've added a line of blue squares to the plot. The top image below show the unzoomed, and I've zoomed in in the lower one.
I have some data made of coordinates and the count of each coordinate which I plot in a heatmap like this:
pyplot.subplot(211)
pyplot.scatter(longitudes, latitudes, c=counts)
pyplot.colorbar()
which is inspired by this great answer here in SO.
If you look closely you can see, that the dots shape the worldmap somehow. To underline this effect I'd like to put the real country boarders (simply drawn would be enough) as background to my plot. Is this possible with matplotlib? Maybe there is some (hidden) builtin in matplotlib?
You can likely achieve this if you have some image of the world map that you want as a background. You can read this into a numpy array and plot the image. Then you should be able to add your scatter plot overtop of the image. This matplotlib cookbook example shows how to insert images and such. There is also the matplotlib image tutorial that may be of use.
I've not used it, but you may also be interested in the basemap toolkit for matplotlib. In particular, the section on drawing a map background mentions specifically a drawcountries() method.