Is it possible to create a Q-Q plot when lacking a coordinate system? - rascal

I'm looking to create a Q-Q plot within Rascal using the Vis library. I have been told there is no positional system. Is this true? If true, how would I go about plotting this or any scatterplot? Does anyone have an example of this in use?

That's an excellent question. Certainly Rascal's Vis library is "point free" in the sense that its layout mechanism has no absolute coordinate system. However, there are certain Figure kinds which have a relative coordinate system wrt their own "origin". When you combine several of those using horizontal, vertical or overlay boxes (and align them properly), you can create the effect of bar charts, scatterplots and whatever you desire.
In particular the overlay Figure composition is interesting: http://tutor.rascal-mpl.org/Rascal/Libraries/Vis/Figure/Figure.html#/Rascal/Libraries/Vis/Figure/Figures/overlay/overlay.html
Figure point(num x, num y){ return ellipse(shrink(0.05),fillColor("red"),align(x,y));}
coords = [<0.0,0.0>,<0.5,0.5>,<0.8,0.5>,<1.0,0.0>];
ovl = overlay([point(x,y) | <x,y> <- coords]);
render(ovl);
Produces this (both code and image taken from the documentation linked above):
Each point is an ellipse which is aligned at the (x, y) position relative to the origin of the enclosing overlay box.
The origin by default of this overlay seems to be the upper-left corner, when no other FProperty's are given to the overlay. It's possible other alignment options for the overlay Figure also change the position of its origin.

With the help of Jurgen Vinju I wrote this code, hope it helps someone: https://gist.github.com/rlmhermans/c9e82a6a623b65f0c6957ab3ff2742cf

Related

Endless scrolling over a 3d map

I have some experience with Metal and quite a bit with Unity and am familiar with setting up meshes, buffers, and the backing data for drawing; but not so much the math/shader side. What I'm struggling with is how to get an endless scrolling world. So if I pan far to the right side I can see the left side and keep going.
The application of this would be a seamless terrain that a player could scroll in any direction forever and have it just wrap.
I don't want to duplicate everything on draw and offset it, that seems horrendously inefficient. I am hoping for a way to either use some magic matrix math or some sort of shader to get things wrapping/drawing where they should when panning the map. I've searched all over for some sort of guide or explanation of how to get this working but haven't come up with anything.
I know a lot of old (dos) games did this somehow, is it still possible? Is there a reason why it seems the industry has migrated away from this type of scrolling (bounding to edges vs wrapping)?
I have created a simple example demonstrating what you're looking for (I think).
The basic idea of it is that you draw the map in a repeating grid, using the drawPrimitives(type:vertexStart:vertexCount:instanceCount:) method on MTLRenderCommandEncoder. As the instance count you want to pass in the number of identical maps you want to draw, extending it as far as needed to not see where it ends. In my example I used a simple 5x5 grid.
To not have the user see the edge of the map, we're gonna calculate their position modulo 1 (or whatever size your map is):
func didDrag(dx: CGFloat, dy: CGFloat) {
// Move user position on drag, adding 1 to not get below 0
x += Float(dx) * draggingSpeed + 1
z += Float(dy) * draggingSpeed + 1
x.formTruncatingRemainder(dividingBy: 1)
z.formTruncatingRemainder(dividingBy: 1)
}
This is how it looks:
Just a follow up on what I have actually implemented. First I essentially have an array of x,y points with altitude, terrain type and all that jazz. Using some simple % and additions/subtractions it is trivial to get the nodes around a point to generate triangles
On a draw I calculate the first showing point and the last showing point and calculate the groups of triangles shown between those points. The first/last point take into account wrapping, it is then pretty trivial to have an endless wrapping world. For each group a translation offset is passed via a uniform matrix for that group which will position that section where it should belong.
I set it via renderEncoder.setVertexBytes(&uniform, length:..., offset:...)

Edge detection on pool table

I am currently working on an algorithm to detect the playing area of a pool table. For this purpose, I captured an image, transformed it to grayscale, and used a Sobel operator on it. Now I want to detect the playing area as a box with 4 corners located in the 4 corners of the table.
Detecting the edges of the table is quite straightforward, however, it turns out that detecting the 4 corners is not so easy, as there are pockets in the pool table. Now I just want to fit a line to each of the side edges, and from those lines, I can compute the intersects, which are the corners for my table.
I am stuck here, because I could not yet come up with a good solution to find these lines in my image. I can see it very easily when I used the Sobel operator. But what would be a good way of detecting it and computing the position of the corners?
EDIT: I added some sample Images
Basic Image:
Grayscale Image
Sobel Filter (horizontal only)
For a general solution, there will be many sources of noise: problems with cloth around the rails, wood texture (or no texture) on the rails, varying lighting, shadows, stains on the cloth, chalk on the rails, and so on.
When color and lighting aren't dependable, and when you want to find the edges of geometric objects, then it's best to think in terms of edge pixels rather than gray/color pixels.
A while back I was thinking of making a phone-based app to save ball positions for later review, including online, so I've though a bit about this problem. Although I can provide some guidance for your current question, it occurs to me you'll run into new problems each step of the way, so I'll try to provide a more complete answer.
Convert the image to grayscale. If we can't get an algorithm to work in grayscale, we'll inevitably run into problems with color. (See below)
[TBD] Do some preprocessing to reduce noise.
Find edge points using Sobel or (if you must) Canny.
Run Hough lines detection, but with a few caveats and parameterizations as described below.
Find the lines described a keystone-shaped quadrilateral. (This will likely be the inner quadrilateral of two: one inside the rail on the bed, and the other slightly larger quadrilateral at the cloth/wood rail edge at top.)
(Optional) Use the side pockets to help determine the orientation of the quadrilateral.
Use an affine transform to map the perspective-distorted table bed to a rectangle of [thankfully] known relative dimensions. We know the bed sizes in advance, so you can remap the distorted rectangle to a proper rectangle. (We'll ignore some optical effects for now.)
Remap the color image to the perspective-corrected rectangle. You'll probably need to tweak the positions of some balls.
General notes:
Filtering by color in the general sense can be difficult. It's tempting to think of the cloth as being simply green, blue, or red (or some other color), but when you look at the actual RGB values and try to separate colors you'll begin to appreciate what a nightmare working in color can be.
Optical distortion might throw off some edges.
The far short rail may be difficult to detect, BUT you do this: find the inside lines for the two long rails, then search vertically between the two rails for the first strong horizontal-ish edge at the far side of the image. That'll be the far short rail.
Although you probably want to use your phone camera for convenience, using a Kinect camera or similar (preferably smaller) device would make the problem easier. Not only would you have both color data and 3D data, but you would eliminate some problems with lighting since the depth data wouldn't depend on visible lighting.
For your app, consider limiting the search region for rail edges to a perspective-distorted rectangle. The user might be able to adjust the search region. This could greatly simplify the processing, and could help you work around problems if the table isn't lit well (as can be the case).
If color segmentation (as suggested by #Dima) works, get the outline of the blob using contour following. Then simplify the outline to a quadrilateral (or a polygon of few sides) by the Douglas-Peucker algorithm. You should find the four table edges this way.
For more accuracy, you can refine the edge location by local search of transitions across it and perform line fitting. Then intersect the lines to get the corners.
The following answer assumes you have already found the positions of the lines in the image. This however can be done "easily" by directly looking at the pixels and seeing if they are in a "line". Usually it is easier to detect this if the image has been deskewed first as well, i.e. Rotated so the rectangle (pool table) is more like this: [] than like /=/. Then it is just a case of scanning the pixels and if there are ones of similar colour alongside it assuming a line is between them.
The code works by looping over the lines found in the image. Whenever the end points of each line falls within a tolerance on within the x and y coordinates it is marked as a corner. Once the corners are found I take the average value between them to find where the corner lies. For example:
A horizontal line ending at 10, 10 and a vertical line starting at 12, 12 will be found to be a corner if there is a tolerance of 2 or more. The corner found will be at: 11, 11
NOTE: This is just to find Top Left corners but can easily be adapted to find all of them. The reason it has been done like this is because in the application where I use it, it is faster to sort each array first into an order where relevant values will be found first, see: Why is processing a sorted array faster than an unsorted array?.
Also note that my code finds the first corner for each line which might not be applicable for you, this is mainly for performance reasons. However the code can easily be adapted to find all the corners with all the lines then either select the "more likely" corner or average through them all.
Also note my answer is written in C#.
private IEnumerable<Point> FindTopLeftCorners(IEnumerable<Line> horizontalLines, IEnumerable<Line> verticalLines)
{
List<Point> TopLeftCorners = new List<Point>();
Line[] laHorizontalLines = horizontalLines.OrderBy(l => l.StartPoint.X).ThenBy(l => l.StartPoint.Y).ToArray();
Line[] laVerticalLines = verticalLines.OrderBy(l => l.StartPoint.X).ThenBy(l => l.StartPoint.Y).ToArray();
foreach (Line verticalLine in laVerticalLines)
{
foreach (Line horizontalLine in laHorizontalLines)
{
if (verticalLine.StartPoint.X <= (horizontalLine.StartPoint.X + _nCornerTolerance) && verticalLine.StartPoint.X >= (horizontalLine.StartPoint.X - _nCornerTolerance))
{
if (horizontalLine.StartPoint.Y <= (verticalLine.StartPoint.Y + _nCornerTolerance) && horizontalLine.StartPoint.Y >= (verticalLine.StartPoint.Y - _nCornerTolerance))
{
int nX = (verticalLine.StartPoint.X + horizontalLine.StartPoint.X) / 2;
int nY = (verticalLine.StartPoint.Y + horizontalLine.StartPoint.Y) / 2;
TopLeftCorners.Add(new Point(nX, nY));
break;
}
}
}
}
return TopLeftCorners;
}
Where Line is the following class:
public class Line
{
public Point StartPoint { get; private set; }
public Point EndPoint { get; private set; }
public Line(Point startPoint, Point endPoint)
{
this.StartPoint = startPoint;
this.EndPoint = endPoint;
}
}
And _nCornerTolerance is an int of a configurable amount.
A playing area of a pool table typically has a distinctive color, like green or blue. I would try a color-based segmentation approach first. The Color Thresholder app in MATLAB gives you an easy way to try different color spaces and thresholds.

How to find sizes and shapes of Microsoft Powerpoint objects?

I have a slide with some hand-drawn circles on it. I'd like to get a list of the coordinates and radii (sizes) of them. Attached is an image and link. Anyone have an idea how?
I started looking into computer vision techniques, but it seems like there should be a much more direct way.
If you are familiar with openCV the method HoughCircles() will do the job:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
Are you familiar with Matlab? imfindcircles() will do it:
http://www.mathworks.com/help/images/ref/imfindcircles.html
If this is a one time job you can post it as a job for someone else to do it for you for a small fee. Example: https://www.mturk.com/mturk/welcome
If you don't know any programming language and this is a one time job, you can do it manually. You can select each circle in photoshop, count the amount of pixels (and using the formulae of circumference = 2*pi*radius) find the radius. The center of mass of all the pixels will be the center of the circle.
It is a bit tricky to separate overlapping circles but you can do it by hand
I found a suitable method using vector graphics.
Select all the circles in powerpoint, right click and 'save as a picture'. Use .emf (windows metafile) format (this option was only available on my windows machine, not mac).
Open the emf file in inkscape, and save it to an 'svg' format, which is ascii and human readable.
Extract the information from the path commands.
E.g.: Each circle is represented as a path object, with a line:
d="m 36.527169,36.434607 c 0,-9.696733 9.075703,-17.551993 20.274845,-17.551993 11.194626,0 20.270329,7.85526 20.270329,17.551993 0,9.69264 -9.075703,17.552246 -20.270329,17.552246 -11.199142,0 -20.274845,-7.859606 -20.274845,-17.552246"
Here, the (x,y) following the 'm' character is the center of the circle, and the 12 (x,y) pairs following 'c' denote a 4-segment polybezier curve in which pairs 3,6,9,12 are the four compass points. Therefore in the above object, this is not a circle but an ellipse with axes ~ 20.27 and 17.55.

2D filled.contour plot with 1D histograms by axes by R

I hope this question is beyond "read the manual". My task is simple, just to plot the following, but the plot in the middle should be a filled.contour plot:
http://gallery.r-enthusiasts.com/graph/Scatterplot_with_marginal_histograms_78
Background: I prefer filled.contour rather than hist2d. Because, I could use kernel smooth, so the plot for discrete data won't be too ugly. I also tried image() and then contour(), but the number on contour is not clear and no indication about the color.
My problem: in filled.contour function, it uses layout() for filledcontour() plot and rect() plot (color bar). However, I use layout() in the outside code to organize 2 histogram and one filled.contour plot. Looks like, the layout outside is shadowed by filled.contour(). I am not sure how R deal with this problem. Should I rewrite filled.contour() somehow?
Thanks
If you look at the help page ?filled.contour you will see that it also mentions another function called .filled.contour (extra . at the front) which does just the bare bones plotting without calling layout and causing the problems that you see. You need to do more of the checking and prelim work, but you should be able to do what you want using .filled.contour for the main plot and setting up the layout yourself.

amount of rect intersection?

i am trying to check the degree of overlap between 2 CGPaths.
the easiest way i have come up with is get the percentage of the overlap between the bounding CGRects. I know this will fail when different paths occupy similar bounds. but oh well, if you know of a better way ... please help.
anyway, the current question regards calculating the percentage overlap between the rects.
i see the CGRectIntersection function to obtain the rectangle of intersection. I can calculate the area of this rect, but there doesn't seem to be an easy way to get the area of the non-intersected regions. any ideas? would subtracting that area from the area of the rectUnion make sense? if i understand rectUnion correctly, if the union and the intersection are the same size, they completely overlap?
Not quite understanding, I think. Isn't the "non-intersecting region" of a CGRect A with another one B just A's area minus the intersecting region? Or more to the point, isn't the percentage overlap just equal to the intersecting area divided by the combined total area:
Area(A ^ B)/(Area(A) + Area(B) - Area(A^B))
(BTW, I don't think you want to deal with RectUnion as that potentially has a huge amount of space in neither A or B. )
Oh, and on your original question, that's beyond my graphics ability, but the basic technique seems to be to draw both Paths in a graphic context (maybe with an XOR) and see which pixels are still left on. There seems to be some code pointing the way here: Clipping CGPath to a CGRect

Resources