Python: how to add curves in an image [duplicate] - python

This question already has an answer here:
matplotlib: add circle to plot
(1 answer)
Closed 5 years ago.
I have an image as follows:
The image is generated in Python. I want to further plot a curve on top of the image, which encloses the white region. I have already got the pixel indices for the region border. I know in Matlab, e.g., it can be simply done by :
hold on
plot(x,y,'r-') %x and y are the indices of boundary pixels.
The resultant image should be like this:
How can I do that contour on top of the image in Python, not by cv2.drawContours? Anyone can help me with the problem?

You can use the Pillow library for it. Without going into the whole solution, there's a good example in their tutorial on how to read an image an do some postscript drawing on top of it: https://pillow.readthedocs.io/en/4.1.x/handbook/tutorial.html#drawing-postscript
You pretty much only need to change the rectangle to a circle instead.
The cut down example is:
from PIL import Image
from PIL import PSDraw
im = Image.open("lena.ppm")
box = (1*72, 2*72, 7*72, 10*72) # in points
ps = PSDraw.PSDraw() # default is sys.stdout
ps.begin_document(title)
# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box)
ps.end_document()
PS. I'm assuming that by "I have an image as follows:" you mean, you have an image file and want to process that, not that you're generating that graph in python already. If you're using something like matplotlib to generate it in the first place, you can add it there instead.
If you're already doing it in matplotlib, then the question is likely a duplicate of matplotlib: add circle to plot

Related

Is there a function in Pygame to save a portion of the screen as an image? [duplicate]

This question already has answers here:
How can I crop an image with Pygame?
(4 answers)
Closed 1 year ago.
I am working on making a drawing program in Pygame. All features are basically complete except for the ability to save your image. Is there any built in Pygame function that does this, or will I have to find some other way (E.G. taking a screenshot or something).
screen is a Surface object which has method subsurface(Rect) to get part of surface - and this part will be also a Surface object.
And there is function pygame.image.save(surface, filename)
Other method: at start create smaller Surface and draw on this surface (instead of drawing directly on screen) and blit() this surface on screen to display it and save() this surface to file.
You can even use many surfaces and keep them on list to create function Undo. When you use new tool then you duplicate surface and put it on list. When you use Undo then you get last surface from list.
If it's a drawing game, then you'll probably find it very easy to manually make the image.
I know it sounds scary, but it really isn't, especially in python.
You can use PyPng to convert a 2D array into an image, as I assume you already use a 2D array to store the drawing

How to plot a transparent cube with mayavi?

Basically this is the question - how to plot a transparent cube with given coordinates of two opposite corners. Surely something simple exists, but I cannot find it. I have tried using tvtk.visual.box, but it doesn't support transparency as far as I could tell.

Rotate an image without black area

When we use some image processing library to rotate an image, the rotated image will always contains some black area. For example, I use the following python code to rotate an image:
from scipy import misc
img = misc.imread('test.jpg')
img = misc.imrotate(img,15)
misc.imsave('rotated.jpg')
The image is as follows:
My question is: how can I rotate an image without producing black area. I believe there exists some interpolation method to compensate for the missing area, which makes the image more natural.
It will be appreciated if anyone can provide a python code to achieve my task.
If you want to 'clone' or 'heal' the missing areas based on some part of the background, that's a complex problem, usually done with user intervention (in tools like Photoshop or GIMP).
Alternatives would be to fill the background with a calculated average colour - or just leave the original image. Neither will look 'natural' though.
The only approach that will work for all images will be to crop the rotated image to the largest rectangle within the rotated area. That will achieve your objective of having no black areas and looking natural, but at the cost of reducing the image size.
isnt there a simple paint fill function in your "some image library" ?, simple do that at all 4 corner pixels and then make it white or so.

How to make a heat map using python and openCV [duplicate]

This question already has answers here:
How to convert a grayscale image to heatmap image with Python OpenCV
(2 answers)
Closed 10 months ago.
I am trying to implement this:
YouTube link- https://www.youtube.com/watch?v=Qk4V_x6B7jY&t=5s
Blog link- http://www.businessinsider.in/These-Heat-Maps-Show-How-Retailers-Track-You-As-You-Shop/articleshow/29512380.cms
I want to use python and openCV but I am a beginner in openCV and hence, I have no idea How to implement this.
I have some basic idea. I have been able to track motion and draw a rectangle around the moving object and i am saving the co-ordinates of the rectangle in an external csv file. But, i am stuck on plotting heat map part. How to make it so that, over time when people are moving more and more in an area, the color changes from blue(normal movement ) to red (high movement) ? Please help..
So the basic idea for plotting a heat map is to visually get some feedback for the probability of a given particular event, You may write your own method which may take probability in range 0-1 and output a color in range (255, 0, 0) - (0, 0, 255). Or Opencv has provision of color-maps. You may be interested in using the COLORMAP_JET:
And Now you have to normalize the probability in range 0-255 instead of 0-1 and the you may use cv2.applyColorMap(input_prob, cv2.COLORMAP_JET) to get the desired output.

Find pixel offset for active plot area [duplicate]

This question already has answers here:
Incorrect rectangle location in matplotlib
(2 answers)
Clickable matrix image with python and matplotlib [duplicate]
Closed 8 years ago.
I saved multiple matrix images using
imshow
savefig
to disk and now I would like to detect the offset between the picture frame and the first pixel.
My problem is that I have to use labels around the plot area which moves the real data view around based on the label text length -> offsets become arbitrary.
Hence, I would like to automatically detect the offset between the upper left corner in the image and the upper left corner of the active plot area + the width & height used to plot one matrix entry.
I guess the parameter must be somewhere buried in the
axes.transData
axes.transAxes
properties (?), but I seem not to find it.
Thanks,
EL

Categories