Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a really hard task to do, which is to calculate the area of the polygon using earlier created script in PYTHON to read the data from a set of Polygons stored in Well Known Text format and calculate the area for each Polygon using the formula..(I don't know the actual formula... I know it's theformula to calculate the area of a polygon using the summation of triangles.)
then script should read the data from the file and store it as a list or lists. The script should compute the polygon area and save the areas of each polygon to a file and also script should use a function to calculate the area of the polygon.
Please help, as I have no idea how to do it in python(never used it before). You are my last chance people!
You can use shapely library to calculate the area.
https://pypi.python.org/pypi/Shapely
Create a Polygon using the coordinates of the vertices of the polygon.
poly = Polygon([list of point pairs])
The area of the polygon is returned by:
poly.area
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an analog speedometer image, with the needle pointing to the current speed. I am trying to find a way to get the speed that the needle is pointing out to. I tried using HoughCircles() from OpenCV, but it is throwing an error as the image contains only speedometer and which is a semi-circle. Any resources to help me move forward will be appreciated.
Assuming the needle will have a different colour to the rest of the speedometer OR its size is distinctly larger than the rest of the elements on the speedometer (which is often the case), I'll do something like below.
Convert the image to grayscale.
Apply colour thresholding (or size-based thresholding) to detect the pixel area representing the needle.
Use HoughLines() or HoughLinesP() functions in OpenCV to fit a line to the shape you detected in Step 2.
Now it's a matter of measuring the angle of the line you generated in Step 3 (example provided here: How can I determine the angle a line found by HoughLines function using OpenCV?)
You can then map the angle of the line to the speed through a simple equation (Will need to see an image of the speedometer to generate this).
let me know how it went.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to detect the center&radius of an arc like shown below for my thesis by using open cv. I tried many things and searched a lot, but cant figure out. Could please somebody help me? I would be really glad.
I would not do the center finding itself with OpenCV but with simple 2D geometry instead see first bullet in Circular approximation of polygon (or its part) so:
filter out blobs not on curve
segmentate and remove too small (unconnected) blobs
find 3 points on your curve
They should be far enough from each and should form 2 lines (black). You can apply thinning algorithms to enhance precision. than simply find 2 most distant points from the curve and one that is in half way between them.
cast normal axises from their mid points (brown)
simply rotate the line slope by 90 deg by swapping x,y of direction vector and negating one of them so (-y,x) or (y,-x).
find the intersection its the center you are looking for
find radius
its the average of distance between center and the 3 points ...
Here a small example I just did in paint (its hand drawn so not pixel perfect):
Here is my simple approach algorithm:
Look at the angle contour by wide-view, like:
Check each pixel of this wide-view image one by one and find the norms(lengths) for each point of the contours. (To be clear: for each pixel, find lengths to those contour points)
If all lengths are equal for a pixel then that pixel is the center of the circle.
Note: This is simple approach and absolutely works. Just not sure about does it take long time to calculate for cpu.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am trying to find out how to do this.
All the examples which I find online, are asking me to first generate some list of points/couples (x,y) and then plot these couples.
But I don't quite want this... Is there a library/module where I can just:
provide the interval for x;
the formula/function for y (y=f(x));
... maybe also specify which part of the plane to visualize
and just have the library draw the graphic of y.
I guess there should be a way to do this since it does not seem complicated at all. Seems a pretty standard things, I think.
Example:
Say I want to plot y = sqrt(x*(1-x)) for x in [0,1]... but show this drawing in a 600x400 screen area (scaled properly). How do I do this?
Since the question is tagged with matplotlib, here is how it's done; for example plot y=f(x)=x^2 in the interval [-3,3].
import numpy as np
import matplotlib.pyplot as plt
interval = -3,3
f = lambda x: x**2
x = np.linspace(*interval, 301)
plt.plot(x, f(x))
plt.show()
The reason you keep seeing suggestions to generate points is that in order to draw the curve on a computer screen, a set of points must be generated to determine which pixels to set. As a human we tend to think that the graph of a function is based on the function. For a computer to do it is needs to know at every point on the screen, is this point part of the function or not. A screen is just a bit array of pixels. For the computer to know which pixel to set, it must know whether it is on the curve or not. The easiest way to do that is to generate a set of pixels which are on the curve - that is, create points (x,y) that satisfy y=f(x).
Any library that draws the curve will generate the points. This step becomes so trivial that most graphing libraries will expect you've already done that step. There's not much point the library doing it, because the difference would only be equivalent to:
plt.plot(xvals, f(xvals))
compared to:
yvals = f(xvals)
plt.plot(xvals, yvals)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Need to plot graph the intensity versus wavelength for the spectrum. Also, me need to determine the wavelengths from the graph (the image below):
With which program I can do this? I searched in Google program, but they all work with video (example theremino spectrometer)?. I need to work the image.
That's not a graph but a picture. Anyhow you can get started as follows.
You can load the image with scipy. Then, in the simplest case, do a horizontal cut which will give you intensity vs pixel position.
import scipy.misc as misc
import matplotlib.pyplot as plt
img = misc.imread('spectrum.png', mode='L')
mid_line = img[len(img)//2]
plt.plot(mid_line)
plt.show()
There is quite some background luminosity there. Doing a vertical averaging would give a smoother spectra (img_mean = img.mean(axis=0)).
Then you have to find a way to "calibrate" the pixel positions to wavelength. For that you need an external source of "truth", which I don't know what you have available. For example you can say that the maximum at the green line is 510 nm (pixel 405) and the bright blue one 460 nm (pixel 302). Then, depending on your experimental setup you might be able to say that the distance in pixels is linear with wavelength and then you have you conversion.
Hope this guides you a bit.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to learn how to do a heatmap in Python using matplotlib. I'm going to be plotting a series of locations in a huge X,Y grid based off of an array of tuples. I found this code example which gives a perfect example of what I want to do. I can't seem to understand what the different parts of it mean though. Ultimately I want to output this on an overlay of an existing image. Thanks!
Nothing there is complicated. Anyway, here is a more simplified edition:
import pylab as pl
import numpy as np
n = 300 #number of sample data
x,y = np.random.rand(2,n) #generate random sample locations
pl.subplot(121) #sub-plot area 1 out of 2
pl.scatter(x,y,lw=0,c='k') #darw sample points
pl.axis('image') #necessary for correct aspect ratio
pl.subplot(122) #sub-plot area 2 out of 2
pl.hexbin(x,y,C=None,gridsize=15,bins=None,mincnt=1) #hexbinning
pl.scatter(x,y,lw=0.5,c='k',edgecolor='w') #overlaying the sample points
pl.axis('image') #necessary for correct aspect ratio
pl.show() #to show the plot
Sample Points:
Hexbin result:
Note that mincnt=1 avoids plotting hexagon for empty cells. A hexagon cell with dark red has more number of sample points (here 5) inside. Dark blue hexagons have only one sample inside.