Plotting a graph with GD - php

Here it goes.
I have been thinking about this for a long time, and havent really been able to put up a proper way to do it yet. I havent implemented anything yet, as im still designing the thing.
The idea is that i crawl a website for internal links, i got this settled, its easy, but after the crawling, i end up with an array with lots of links, and how many times those particular link appears on the site that i crawled (and how they're connected).
With this huge array, i want to draw a graph somehow. Assuming i can handle the data correctly, the real question here is how i can draw this in a image by the use of the GD library.
I figured if theres less than 12 elements, i can align them up on a unit circle spacing them up as a circle and then connecting them accordingly, so anything up to 12 elements shouldn't be a problem, but if theres more than 12, it could be awesome getting them lined up like this http://nayena.com/stackoverflow/graph.png Or well, thats just a rough drawing, but i guess its just to prove a point.
So i'm here looking for guidance or tips towards getting the math down to getting the stuff lined up in a good way.
I have previously made bar-graphs, so i have little experience doing math with GD. If possible, id prefer not using some plotter-library - in the end, it gives me a better understanding on how things are supposed to be.

This is a whole subfield of CS/IS, with textbooks, research papers and symposia devoted to the subject of graph drawing.
GraphDrawing.org
Graph Drawing: Algorithms for the Visualization of Graphs
Graph Drawing Software (Mathematics and Visualization)
Drawing Graphs: Methods and Models (Lecture Notes in Computer Science)
http://vis.computer.org/
You could devote an entire career to how to plot a graph.

Related

GD/imagick code to find if an image is well-focussed or not?

I run a website with thousands of user-contributed photos on it. What I'd like is a script to help me weed out poor photos from good photos. Obviously this isn't 100% possible, but it should be possible to determine if an image has no discernable focussed area? I think?
I did a bit of googling and couldn't find much on the subject.
I've written a very simple script that iterates over the pixels, and sums the difference in brightness between neighbouring pixels. This gives a high value for sharp contrasty images, and a low value for blurred/out of focus images. It's far from ideal though, as if there's a perfectly focussed small subject in the frame, and a nice bokeh background, it'll give a low value.
So I think what I want is a script that can determine if a part of an image is well-focussed, and if none is then to alert me?
Any bright ideas? Am I wasting my time?
I'd be interested in any code that can determine other sorts of "bad" photos too - too dark, too light, too flat, that sort of thing.
Too dark and too light are easy - calculate a colour average as you iterate through every pixel.
For your focus issue, I think you're going to run into a lot of problems with this one. I would strongly recommend looking up kernel convolution, as I have a sinking feeling that you'll need it. This allows you to perform more complex operations on pixels based on neighbors - and is how most Photoshop filters are done!
Once you've got the maths background to do it, what I would do is to convert your image to an array of unique values (as opposed to RGB) representing brightness. From there, use an edge-finder kernel (Sobel operator should do the trick) and find the edges. Once that is done, iterate over again, mapping the bits with no edge, and calculate the largest square area without an edge from this. It is probably the least CPU-intensive solution, though not the most esoteric.

Find most favored area on an image

What mean-stat-equation should I use when I have an image with N-number sample-size of selections?
I have a unique problem for which i was hoping to get some advice, so that i don't miss out on anything.
The Problem: To find the most favored/liked/important area on an image based on user selection of areas in different selection ratios.
Scenario: Consider an Image of a dog, and hundreds of users selecting area over this image in various resolutions, the obvious area of focus in most selections will be the area containing the dog. I can record the x1,x2,y1,y2 co-ordinates and put them into a db, now if i want to automatically generate versions of this image in a set of resolutions i should be able to recognize the area with the max attraction of the users.
The methods i think could work are:
Find the average center point of all selections and base the selection in that. - Very simple but would not be as accurate.
Use some algorithm like K Means or EM Clustering but i don't know which one would be best suited.
Looking forward to some brilliant solution to my problem
More info on the problem:
The Actual image will be most probably be a 1024x768 image, and the selections made on it will be of the most common mobile phone resolutions. The objective is to automatically generate mobile phone wallpapers by intelligent learning based on user selections.
I believe that you have two distinct problems identified above:
ONE: Identification of Points
For this, you will need to develop some sort of heuristic for identifying whether a point should be considered or not.
I believe you mentioned that hundreds of users will be selection locations over this image? Hundreds may be a lot of points to cluster. Consider excluding outliers (by removing points which do not have a certain number of neighbors within a particular distance)
Anything you can do to reduce your dataset will be helpful.
TWO: Clustering of Points
I believe that K Means Clustering would be best suited for this particular problem.
LINK
Your particular problem seems to closely mirror the standard Cartesian coordinate clustering examples used in explaining this algorithm.
What you're trying to do appears to be NP-Hard, but should be satisfied by the classical approximations.
Once clustered, you can take an average of the points within that cluster for a rather accurate approximation.
In Addition:
You dataset sounds like it will already be tightly clustered. (i.e. Most people will pick the dog's face, not the side of it's torso.) You need to be aware of local minima. LINK These can really throw a wrench into your algorithm. Especially with a small number of clusters. Be aware that you may need a bit of dynamic programming to combat this. You can usually introduce some variance into your algorithm, allowing the average points to "pop out" of these local minima. Local Minima/Maxima
Hope this helps!
I think you might be able to approach your problem in a different way. If you have not heard of Seam Carving then I suggest you check it out, because the data you have available to use is perfectly suited to it. The idea is that instead of cropping an image to resize it, you can instead remove paths of pixels that are not necessarily in a straight line. This allows you to resize an image while retaining more of the 'interesting' information.
Ordinarily you choose paths of least energy, where energy here is some measurement of how much the hue/intensity changes along the path. This will fail when you have regions of an image that are very important (like a dog's face), but where the energy of those regions is not necessarily very high. Since you have user data indicating what parts of the image are very important you can make sure to carve around those regions of the image by explicitly adding a little energy to a pixel every time someone selects a region with that pixel.
This video shows seam carving in action, it's cool to watch even if you don't think you'll use this. I think it's worth trying, though, I've used it before for some interesting resizing applications, and it's actually pretty easy to implement.

Image search using webservices

Ive got an assignment to create an image gallery using flickr - this has to be done using keyword(s) entered by a user in a form, and the image gallery returned must be paginated and display a certain number of results per page. Each image in the gallery must also be a thumbnail.
Im graded based on how efficient, maintainable, consise and clear the code is..
Ive finished the assignment but i think it could be optimised alot better, these are the points that i might be lacking.
All my code is in a single file (the form and the gallery code) - should i separate this?
Im not using oo at all, is there someway i can make use of it here?
Im resizing the thumbnails using html length and width
thanks for any help
This question is really broad, and it seems like you want someone to write the code for you, which I won't do but I'll help point in the right direction.
First, Flickr has an API, I would reccomend checking that out.
http://www.flickr.com/services/api/
You are going to need a library to make thumbnails, you can do that quickly with GD
http://php.net/manual/en/book.image.php
As far as separating the code, it probably wouldn't be a bad idea. One file is great if its a few hundred lines, but if you have a lot going on, it doesnt hurt to separate files by their function (ex: scrape.php, resize.php etc.). That's mostly personal preference. Since you're being graded on maintainability and clarity I would suggest breaking it up.
At the risk of starting a flamewar, I would suggest making it OOP. If you want it to be clear and readable this will help a lot. Break it into classes and functions and group them in a logical manner so someone knows if they need to change something they have a good idea where to start. Document it well and this will help also.
My suggestion would be to create a class for each of these functions:
Grab images from Flickr
Store the metadata related to the images
Resize the images
Pull the information from the database
Display the pages.
Try to keep the data functions separate from the presentation. Use a class to build your pages, and call another class to get the data. This is pretty general advise, but it's something you'll need to know when building future apps. This doesn't necessarily need to be full MVC, but separation of these tasks will make the code make more sense.

What is the difference between PHPGraphLib and JpGraph?

I am trying to generate simple bar charts to be displayed on a website. I am looking at PHPGraphLib and JpGraph. They seem fairly comparable and with proper configuration they can probably produce nearly identical charts. Is there a reason to choose one over the other? Are there are any real differences between the two? Are there alternatives I may be overlooking?
PHPGraphLib looks to be more light wieght, and not quite as capable as JpGraph. If you just look at the examples and the documentation, you can see how much more JpGraph does. If your goal is to be lightweight and you don't need the extras JpGraph offers, then go with PHPGraphLib. If you want to have a lot of flexibility in the graphs you can generate, go with JpGraph. I have used JpGraph for a ton of things, and its very versatile, and you can figure out how to do anything using the huge number of examples they include with the source.
jpGraph has far more capabilities than PHPGraphLib. On the other hand, PHPGraphLib is a lot easier to install and very fast. In fact, it is the only one that I was able to get to work immediately. It consists of only three files. I am still struggling with jpGraph and some truetype fonts that it cannot find. If PHPGraphLib has enough features for you, by all means, run with it. I wish I could.
Both of them are free for personal use, and a few hundred dollars for unlimited servers. Pretty good deal, and I think the developers deserve getting paid what they charge.

Generating maps Dynamically

I am looking to generate transport maps in a style similar to the iconic London underground [tube] map.
These maps will change from time to time and many will be required so instead of drawing them up manually in inkscape [or similar] I am hoping to have them generated dynamically from a db or dataset.
Does anyone know if there is any library apis etc. out there that would help with this task, or any suggestions in general of how [or how not] to go about this ?
I am thinking svg's would be the best way to go with this, plus there may be need for basic interactivity down the line.
I am working in php so otherwise it's GDlib, ImageMagick ?
Thanks in advance.
.k
Well, the answer really isnt in how to use GD or ImageMagick, there are manuals for that. As for helper libraries, most libraries focus on graphing, anything else you will have to write yourself. Your best bet as a solution would be to have your admin interface generate the images when data in the backend changes and cache the images, since there's no reason to build the image every time someone accesses it.
For generating maps, i think your best bet would be defining stations with one or many 'lines' which determine some sort of indicator of relationship to the stations around, and an x,y. You'd probably only need to determine a 'parent' station since you're just drawing lines from a-b. That way you can position them in the same manner as they're typically rendered on the actual trains, use the lines and surrounding stations to draw mappings.
Doesn't sound like too difficult a problem. 3 tables:
stations [stationid,name,x,y,meta1,meta2],
placements [placementid,stationid,lineid,parentstationid],
lines [lineid,name,meta1,meta2,colour,etc].
SVG would be pretty good at this sort of thing, and you would avoid the whole image building and caching process, but be wary of browser support issues.
Sounds like a pretty interesting project though, good luck :)
One strategy I use when I need to generate graphs from data in a db is to extract the data in some kind of XML way (e.g. Oracle SQLX or Cocoon XSP/ESQL or eXist-db XQuery) and process it through an XSLT to generate SVG. Good old Cocoon is fine for this kind of job if you don't want to write any code (except the XSL of course ;-).
The SVG itself can be loaded in some graphic tools to reprocess.
These maps will change from time to
time and many will be required so
instead of drawing them up manually in
inkscape [or similar] I am hoping to
have them generated dynamically from a
db or dataset.
If I were in your shoes, the very first thing I'd do is try to prove that the Google Maps API won't work for your application. Then, maybe, prove that ArcGIS won't work. (Even if they don't work, they're widely used, and you get to add lines to your CV.)

Categories