I'm working on a Haskell program for playing spatial games: I have a graph of a bunch of "individuals" playing the Prisoner's Dilemma, but only with their immediate neighbors, and copying the strategies of the people who do best.
I've reached a point where I need to draw an image of the world, and this is where I've hit problems. Two of the possible geometries are easy: if people have four or eight neighbors each, then I represent each one as a filled square (with color corresponding to strategy) and tile the plane with these. However, I also have a situation where people have six neighbors (hexagons) or three neighbors (triangles).
My question, then, is: what's a good Haskell library for creating images and drawing shapes on them? I'd prefer that it create PNGs, but I'm not incredibly picky. I was originally using Graphics.GD, but it only exports bindings to functions for drawing points, lines, arcs, ellipses, and non-rotated rectangles, which is not sufficient for my purposes (unless I want to draw hexagons pixel by pixel*). I looked into using foreign import, but it's proving a bit of a hassle (partly because the polygon-drawing function requires an array of gdPoint structs), and given that my requirements may grow, it would be nice to use an in-Haskell solution and not have to muck about with the FFI (though if push comes to shove, I'm willing to do that). Any suggestions?
* That is also an option, actually; any tips on how to do that would also be appreciated, though I think a library would be easier.
EDIT: Thank you all so much for your suggestions. Unfortunately, I wasn't able to get all of gtk2hs's required libraries to build, which ruled out a lot of solutions. For a variety of reasons, after I tried all your answers, failed to install a number of libraries and found that others could not do what I wanted, I ended up deciding to just export more of an FFI for libgd and used that instead.
Diagrams looks way cool, but if you want to avoid committing and stay super lightweight, you could generate svg directly. Stealing from Conrad Barski at http://www.lisperati.com/haskell/
type Point = (Float,Float)
type Color = (Int,Int,Int)
type Polygon = [Point]
writePoint :: Point -> String
writePoint (x,y) = (show x)++","++(show y)++" "
writePolygon :: (Color,Polygon) -> String
writePolygon ((r,g,b),p) = "<polygon points=\""++(concatMap writePoint p)++"\" style=\"fill:#cccccc;stroke:rgb("++(show r)++","++(show g)++","++(show b)++");stroke-width:2\"/>"
writePolygons :: [(Color,Polygon)] -> String
writePolygons p = "<svg xmlns=\"http://www.w3.org/2000/svg\">"++(concatMap writePolygon p)++"</svg>"
colorize :: Color -> [Polygon] -> [(Color,Polygon)]
colorize = zip.repeat
rainbow#[red,green,blue,yellow,purple,teal] = map colorize [(255,0,0),(0,255,0),(0,0,255),(255,255,0),(255,0,255),(0,255,255)]
t0 = writeFile "tut0.svg" $ writePolygons (blue [[(100,100),(200,100),(200,200),(100,200)],[(200,200),(300,200),(300,300),(200,300)]])
hexagon c r = translateTo c basicHexagon where
basicHexagon = top ++ (negate r, 0):bottom
top = [(r,0),(r * cos 1,(r * sin 1)),(negate (r * cos 1), r * (sin 1))]
bottom = map (\(x,y)->(x,negate y)) (reverse top)
translateTo (x,y) poly = map f poly where f (a,b)= ((a+x),(b+y))
t1 = writeFile "t1.svg" $ writePolygons (blue [hexagon (100,100) 50] )
hexField r n m = let
mkHex n = hexagon (1.5*n*(r*2),(r*2)) r
row n = map mkHex [1..n]
aRow = row n
in concat [map (offset (r*x)) aRow |x<-[1..m]]
offset r polys = map (oh r) polys where
oh r pt#(x,y) = (x+(1.5*r),y+(r*sin 1))
t2 = writeFile "t2.svg" $ writePolygons (blue $ hexField 50 4 5 )
run t2 and load the file into Firefox or some other program that supports svg.
t2.svg ,exported png http://img30.imageshack.us/img30/2245/93715707.png
I've used HOpenGL before, but the
problem is that (as far as I can tell)
it can't render to a file, but only
the screen; the same (again, as far as
I can tell) seems to be true of SDL
and Wx. I will look into Cairo,
though.
For some reason I can not reply to this post so I have to quote it. You're incorrect about GL and SDL, you can make an off-screen surface/buffer or render-to-texture. Those libraries don't need such a function (and doesn't make much sense either) because you can do it yourself quite easily by accessing pixels in the buffer and writing it out yourself, even with the screen buffers you can access pixel data.
Just the other day I showed somebody how to do this with the Haskell SDL bindings:
http://hpaste.org/fastcgi/hpaste.fcgi/view?id=25047
Use a library that can write out to .PNG files, they will most likely take a raw pointer to pixel buffer which you can get from SDL/GL or copy it to a format which the library needs.
I Just found a Haskell binding for the library DevIL which can output .PNG files. Check out the function called writeImageFromPtr
Cairo is a good bet if you want to generate PNGs. Wumpus also looks promising, though I have never used it. If you just need to see it on the screen, graphics-drawingcombinators is an easy interface to OpenGL that will do what you need in a few lines (see example.hs in the distribution).
Check out Diagrams:
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/diagrams
The examples are quite nice.
what's a good Haskell library for creating images and drawing shapes on them?
You have quite a few options, to my mind the following have all been used for games:
haskell-opengl
haskell-sdl
haskell-wx
haskell-gtk-cairo
Those are the most common, and you might just choose based on features/familiarity.
Related
I am working on a webgl viewer of IFC file now. Most IfcRepresentation objects are easy to understand, however, I am not good at coordination transformation. Are there any better expression to translate and rotate an Object3D in THREEJS as defined by IfcAxis2Placement3D? I guess it should rotate the object by Z axis then align the Z axis to a new vector, how to implement this ?
Another questions is about IfcObjectPlacement. It always requires a sub PlacementRelTo object until PlacementRelTo == null. I am a bit confused again, is it a forward transformation or backward transformation if I want to read the absolute coordinates from this placement? I mean, use a push-pop or a direct order? for example, if there are matrix like M1, M2.. Mn, then M = M1 x M2 x ... Mn or M = Mn x Mn-1 x ... x M2 x M1? I can find beautiful mesh objects in my project but the position is always wrong. Please help me.
Thanks.
Take a look at this article on matrix transformations for projection for a primer on Matrix transformations.
It's worth noting that most geometry in an IFC model will be 'implicit' rather than 'explicit' shapes. That means that extra processing needs to be performed before you can get the data into a form you could feed into typical 3D scene - and so you'll be missing a lot of shapes from your model as few will be explicitly modelled as a mesh. The long and short is that geometry in IFC is non-trivial (diagrams). That's before you start on placement/mapping/ transformation to a world coordinate system.
It's not clear if you are using a IFC toolkit to process the raw IFC STEP data. If not, I'd recommend you do as it will save a lot of work (likely years of work).
BuildingSmart maintain a list of resources to process IFC (commercial and open source)
I know the following toolkits are reasonably mature and capable of processing geometry. Some already have WebGL implementations you might be able to re-use.
http://ifcopenshell.org/ifcconvert.html & https://github.com/opensourceBIM
https://github.com/xBimTeam/XbimGeometry (disclosure - I am involved with this project)
http://www.ifcbrowser.com/
For a hobby project I'm attempting to align photo's and create 3D pictures. I basically have 2 camera's on a rig, that I use to make pictures. Automatically I attempt to align the images in such a way that you get a 3D SBS image.
They are high resolution images, which means a lot of pixels to process. Because I'm not really patient with computers, I want things to go fast.
Originally I've worked with code based on image stitching and feature extraction. In practice I found these algorithms to be too inaccurate and too slow. The main reason is that you have different levels of depth here, so you cannot do a 1-on-1 match of features. Most of the code already works fine, including vertical alignment.
For this question, you can assume that different ISO exposion levels / color correction and vertical alignment of the images are both taken care of.
What is still missing is a good algorithm for correcting the angle of the pictures. I noticed that left-right pictures usually vary a small number of degrees (think +/- 1.2 degrees difference) in angle, which is enough to get a slight headache. As a human you can easily spot this by looking at sharp differences in color and lining them up.
The irony here is that you spot it immediately as a human if it's correct or not, but somehow I'm not able to learn this to a machine. :-)
I've experimented with edge detectors, Hough transform and a large variety of home-brew algorithms, but so far found all of them to be both too slow and too inaccurate for my purposes. I've also attempted to iteratively aligning vertically while changing the angles slightly, so far without any luck.
Please note: Accuracy is perhaps more important than speed here.
I've added an example image here. It's actually both a left and right eye, alpha-blended. If you look closely, you can see the lamb at the top having two ellipses, and you can see how the chairs don't exactly line up at the top. It might seem negliable, but on a full screen resolution while using a beamer, you will easily see the difference. This also shows the level of accuracy that is required; it's quite a lot.
The shift in 'x' direction will give the 3D effect. Basically, if the shift is 0, it's on the screen, if it's <0 it's behind the screen and if it's >0 it's in front of the screen. This also makes matching harder, since you're not looking for a 'stitch'.
Basically the two camera's 'look' in the same direction (perpendicular as in the second picture here: http://www.triplespark.net/render/stereo/create.html ).
The difference originates from the camera being on a slightly different angle. This means the rotation is uniform throughout the picture.
I have once used the following amateur approach.
Assume that the second image has a rotation + vertical shift mismatch. This means that we need to apply some transform for the second image which can be expressed in matrix form as
x' = a*x + b*y + c
y' = d*x + e*y + f
that is, every pixel that has coordinates (x,y) on the second image, should be moved to a position (x',y') to compensate for this rotation and vertical shift.
We have a strict requirement that a=e, b=-d and d*d+e*e=1 so that it is indeed rotation+shift, no zoom or slanting etc. Also this notation allows for horizontal shift too, but this is easy to fix after angle+vertical shift correction.
Now select several common features on both images (I did selection by hand, as just 5-10 seemed enough, you can try to apply some automatic feature detection mechanism). Assume i-th feature has coordinates (x1[i], y1[i]) on first image and (x2[i], y2[i]) on the second. We expect that after out transformation the features have as equal as possible y-coordinates, that is we want (ideally)
y1[i]=y2'[i]=d*x2[i]+e*y2[i]+f
Having enough (>=3) features, we can determine d, e and f from this requirement. In fact, if you have more than 3 features, you will most probably not be able to find common d, e and f for them, but you can apply least-square method to find d, e and f that make y2' as close to y1 as possible. You can also account for the requirement that d*d+e*e=1 while finding d, e and f, though as far as i remember, I got acceptable results even not accounting for this.
After you have determined d, e and f, you have the requirement a=e and b=-d. This leaves only c unknown, which is horizontal shift. If you know what the horizontal shift should be, you can find c from there. I used the background (clouds on a landscape, for example) to get c.
When you know all the parameters, you can do one pass on the image and correct it. You might also want to apply some anti-aliasing, but that's a different question.
Note also that you can in a similar way introduce quadratic correction to the formulas to account for additional distortions the camera usually has.
However, that's just a simple algorithm I came up with when I faced the same problem some time ago. I did not do much research, so I'll be glad to know if there is a better or well-established approach or even a ready software.
I try to tilt compensate a magnetometer (BMX055) reading and tried various approaches I have found online, not a single one works.
I atually tried almost any result I found on Google.
I run this on an AVR, it would be extra awesome to find something that works without complex functions (trigonometry etc) for angles up to 50 degree.
I have a fused gravity vector (int16 signed in a float) from gyro+acc (1g gravity=16k).
attitude.vect_mag.x/y/z is a float but contains a 16bit integer ranging from around -250 to +250 per axis.
Currently I try this code:
float rollRadians = attitude.roll * DEG_TO_RAD / 10;
float pitchRadians = attitude.pitch * DEG_TO_RAD / 10;
float cosRoll = cos(rollRadians);
float sinRoll = sin(rollRadians);
float cosPitch = cos(pitchRadians);
float sinPitch = sin(pitchRadians);
float Xh = attitude.vect_mag.x * cosPitch + attitude.vect_mag.z * sinPitch;
float Yh = attitude.vect_mag.x * sinRoll * sinPitch + attitude.vect_mag.y * cosRoll - attitude.vect_mag.z *sinRoll * cosPitch;
float heading = atan2(Yh, Xh);
attitude.yaw = heading*RAD_TO_DEG;
The result is meaningless, but the values without tilt compensation are correct.
The uncompensated formula:
atan2(attitude.vect_mag.y,attitude.vect_mag.x);
works fine (when not tilted)
I am sort of clueless what is going wrong, the normal atan2 returns a good result (when balanced) but using the wide spread formulas for tilt compensation completely fails.
Do I have to keep the mag vector values within a specific range for the trigonometry to work ?
Any way to do the compensation without trig functions ?
I'd be glad for some help.
Update:
I found that the BMX055 magnetometer has X and Y inverted as well as Y axis is *-1
The sin/cos functions now seem to lead to a better result.
I am trying to implement the suggested vector algorithms, struggling so far :)
Let us see.
(First, forgive me a bit of style nagging. The keyword volatile means that the variable may change even if we do not change it ourselves in our code. This may happen with a memory position that is written by another process (interrupt request in AVR context). For the compiler volatile means that the variable has to be always loaded and stored into memory when used. See:
http://en.wikipedia.org/wiki/Volatile_variable
So, most likely you do not want to have any attributes to your floats.)
Your input:
three 12-bit (11 bits + sign) integers representing accelerometer data
three approximately 9-bit (8 bits + sign) integers representing the magnetic field
Good news (well...) is that your resolution is not that big, so you can use integer arithmetics, which is much faster. Bad news is that there is no simple magical one-liner which would solve your problem.
First of all, what would you like to have as the compass bearing when the device is tilted? Should the device act as if it was not tilted, or should it actually show the correct projection of the magnetic field lines on the screen? The latter is how an ordinary compass acts (if the needle moves at all when tilted). In that case you should not compensate for anything, and the device can show the fancy vertical tilt of the magnetic lines when rolled sideways.
In any case, try to avoid trigonometry, it takes a lot of code space and time. Vector arithmetics is much simpler, and most of the time you can make do with multiplys and adds.
Let us try to define your problem in vector terms. Actually you have two space vectors to start with, m pointing to the direction of the magnetic field, g to the direction of gravity. If I have understood your intention correctly, you need to have vector d which points along some fixed direction in the device. (If I think of a mobile phone, d would be a vector parallel to the screen left or right edges.)
With vector mathematics this looks rather simple:
g is a normal to a horizontal (truly horizontal) plane
the projection of m on this plane defines the direction a horizontal compass would show
the projection of d on the plane defines the "north" on the compass face
the angle between m and d gives the compass bearing
Now that we are not interested in the magnitude of the magnetic field, we can scale everything as we want. This reduces the need to use unity vectors which are expensive to calculate.
So, the maths will be something along these lines:
# projection of m on g (. represents dot product)
mp := m - g (m.g) / (g.g)
# projection of d on g
dp := d - g (d.g) / (g.g)
# angle between mp and dp
cos2 := (mp.dp)^2 / (mp.mp * dp.dp)
sgn1 := sign(mp.dp)
# create a vector 90 rotated from d on the plane defined by g (x is cross product)
drot := dp x g
sin2 := (mp.drot)^2 / (mp.mp * drot.drot)
sgn2 := sign(mp.drot)
After this you will have a sin^2 and cos^2 of the compass directions. You need to create a resolving function for one quadrant and then determine the correct quadrant by using the signs. The resolving function may sound difficult, but actually you just need to create a table lookup function for sin2/cos2 or cos2/sin2 (whichever is smaller). It is relatively fast, and only a few points are required in the lookup (with bilinear approximation even fewer).
So, as you can see, there are no trig functions around, and even no square roots around. Vector dots and crosses are just multiplys. The only slightly challenging trick is to scale the fixed point arithmetics to the correct scale in each calculation.
You might notice that there is a lot of room for optimization, as the same values are used several times. The first step is to get the algorithm run on a PC with floating point with the correct results. The optimizations come later.
(Sorry, I am not going to write the actual code here, but if there is something that needs clarifying, I'll be glad to help.)
I'm looking for an algorithm or library that can spot the differences between two images (like in a "find the errors" game) and output the coordinated of the bounding box containing those changes.
I'm open to the algorithm being in Python, C, or almost any other language.
If you just want to show the differences, so you can use the code below.
FastBitmap original = new FastBitmap(bitmap);
FastBitmap overlay = new FastBitmap(processedBitmap);
//Subtract the original with overlay and just see the differences.
Subtract sub = new Subtract(overlay);
sub.applyInPlace(original);
// Show the results
JOptionPane.showMessageDialog(null, original.toIcon());
For compare two images, you can use ObjectiveFideliy class in Catalano Framework.
Catalano Framework is in Java, so you can port this class in another LGPL project.
FastBitmap original = new FastBitmap(bitmap);
FastBitmap reconstructed = new FastBitmap(processedBitmap);
ObjectiveFidelity of = new ObjectiveFidelity(original, reconstructed);
int error = of.getTotalError();
double errorRMS = of.getErrorRMS();
double snr = of.getSignalToNoiseRatioRMS();
//Show the results
Disclaimer: I am the author of this framework, but I thought this would help.
There are many, suited for different purposes. You could get a start by looking at OpenCV, the free computer vision library with an API in C, C++, and also bindings to Python and many other languages. It can do subtraction easily and also has functions for bounding or grouping sets of points.
Aside from simple image subtraction, one of the specific uses addressed by OpenCV is motion detection or object tracking.
You can ask more specific image-related algorithmic related questions in the Signal Processing stackexchange site.
"Parse" the two images into multiple smaller images by cropping the original image.
The size of each "sub-image" would be the "resolution" of your scanning operation. For example, if the original images are 100 pixels x 100 pixels, you could set the resolution to 10 x 10 and you'd have one hundred 10 x 10 sub-images for each original image. Save the sub-images to disk.
Next, compare each pair of sub-image files, one from each original image. If there is a file size or data difference, then you can mark that "coordinate" as having a difference on the original images.
This algorithm assumes you're not looking for the coordinates of the individual pixel differences.
Imagemagick's compare (command-line) function does basically this, as you can read about/see examples of here. One constraint though, is that both images must be of the same size and not have been translated/rotated/scaled. If they are not of the same size/orientation/scale, you'll need to take care of that first. OpenCV contains some algorithms for that. You can find a good tutorial on OpenCV functions you could use to rectify the image here.
I'm trying to think of a fast algorithm for the following issue.
Given a goal image G, and two images A and B, determine which of A or B is more similar to G. Note that images A, B, and G are all the same dimension.
By more similar, I mean it looks more like image G overall.
Any ideas for algorithms? I am doing this in Objective-C, and have the capability to scan each and every single pixel in images A, B, and G.
I implemented the following: scan each and every pixel, determine the absolute error in each of red, green, and blue values for A to G and for B to G. The one with the less error is more similar. It works okay, but it is extremely extremely slow.
It is not possible to do better than X*Y where X, Y are the image dimensions. Since you need to scan each pixel of the input anyways.
However, one technique you can try is scan random pixels in the image and find the difference. Once you see an image considerably similar or dissimilar than A or B, you can stop.
# X, Y are the dimensions
sim_A = 0
sim_B = 0
while( abs(sim_A - sim_B) > MAX_DISSIMILARITY):
rand_x = random(X)
rand_y = random(Y)
sim_A += dissimilar(img_G, img_A, rand_X, rand_Y)
sim_B += dissimilar(img_G, img_B, rand_X, rand_Y)
You may try using SIFT Algorithm (Scale Invariant Feature Transform). As you just mentioned that you want to find which image is MORE similar to the goal image, then I guess this is the best algorithm. It basically extracts the Invariant features of the image (features that dont change with change in luminous intensity, scale, perspective etc) and then creates a feature vector of these. Then you may use this Feature vector to compare it with other images. you may check this and this for further reference.
Ideally there are computer vision libraries that make things way simpler (i guess it might be difficult to read and write to images in objective C, without any computer vision library). OpenCV (opensource computer vision Library) is best suited for stuff like these. It has many inbuilt functions to handle common stuff with images/videos.
Hope this helps :)
I would recommend checking out OpenCV, which is an image processing library. I don't think it has Objective-C support, but I think it is a better starting place than writing your own algorithm. Usually better not to reinvent the wheel unless you are doing it for personal practice.
The best way, I found out, is to do the following.
First, invert all pixels on the image to make the opposite of the image. This is the most dissimilar image.
Then, to compare image to the target image, compute how far away it is from the most dissimilar image. If it's more far, it's a better image.