How to get polygon points from image? - php

I need to get programmatically polygon points coordinates from image.
Here is an example: http://imgur.com/gbs96tt
Is there any libraries or any solutions for this? Desirable written in php.

Related

Search database for locations within polyline coordinates

I am using Google Maps to display locations stored in a MySQL database containing name, latitude and longitude information. An AJAX request is sent and the map is updated with all the locations. So far, so good.
Users have the ability to filter the locations by drawing polylines on the map to create a bounding box. The idea is that only locations within this bounding box are returned and they can draw whatever shape they want.
All the points used to draw the shape are returned a list of coordinates. Where I am struggling is taking the list of coordinates and finding everything within them. Can I achieve this with an SQL query? Is it even possible?
I have seen examples of returning locations within x radius from one set of coordinates and also a square box, but I have no idea how to take that further and get everything within a potentially infinite set of coordinates.
You should transform the shape the user enters into a polygon. Then you transform all locations within a square around this polygon into the same coordinate system.
Using a solution describe in the wikipedia page for point-in-polygon problems you will be able to find all locations within this shape and remap them to coordinates in your sql database and then into locations.
There are however similar question which might help you find a proper solution to your problem. See:
How can I determine whether a 2D Point is within a Polygon?
Point in Polygon Algorithm

Extract geometric polygon fron map img

I am trying to extract a colored polygon from a map img. Lets say for example this img
Could it be possible to put this img on lets say the top of google map and create some sort of script that could reproduce this polygon using the coordinates from a real map.
I was thinking of using 2 reference points from the img and the actual map, putting them one on top of the other, and extract the polygon by the color that outlines it.
That would be done on PHP.

Image curving with PHP or HTML5

I am looking to achieve:
http://i53.tinypic.com/2gule04.jpg
I have tried the answers mentioned at Curving an image that starts as a rectangle (uploaded by user), preferably using Canvas or JS
Based on the answers there, I have tried pixel wise transformation which didn't work. To understand a mesh based approach, you will need a skill set of 3d-2d developer which I don't possess.
I am a PHP developer and I am looking for an answer in either PHP or HTML5. I have tried number of things ranging from HTML5 canvas to splitting the image into smaller parts and then joining them however those don't seem to work.
A help in the right direction will be greatly appreciated.
If you can use ImageMagick, the Circular and Radial Distortion Methods examples should come pretty close.
I don't know whether the PHP ImageMagick extension (as opposed to calling ImageMagick from the command line) supports this as well, but it might.
To achieve a similar effect you want to try texture mapping and you need some 2d-3d subdivision and math skills. Basically the idea is to subdivide the texture in triangles and map them to the 2d coordinate using a transformation matrix. It's simplier to start with rectangles first and then use your curved form but I'm new to this too, so I don't know really if texture mapping is used at all to curve an image. Here is an example of a simple texture mapping: Image manipulation and texture mapping using HTML5 Canvas?.
In the aforementioned link there is this sub function:
n the following code I assume you have a picture object texture and 4 corners each of
which is an object with fields x,y,u,v where x,y are pixel coordinates on the target canvas and u,v are pixel coordinates on texture:
IMO this is information enough to start with texture mapping.
A posibility is to use rotoscopic animation instead of mathematical tweening. In other words, you can achieve such transformation with 5 or 6 (or as many as you want) frames that are sequentially drawn on the HTML5 canvas at your desired frame-rate.
You can easily draw each frame using Canvas native API. In your case, you just need to draw Text and then a closed path like:
ctx.beginPath();
ctx.moveTo(...);
ctx.arc(...);
ctx.lineTo(...);
ctx.arc(...);
ctx.lineTo(...);
ctx.closePath();
and just adjust the corresponding values for each frame.
It should be pretty easy!

PHP Image Libraries

I'm working on a PHP application that needs to obtain data from a topographical map (different elevations denoted by different colors). I need to do two things:
Using this map (800x600), find a way to determine the exact pixel location of a particular city. For example, San Francisco is located at precisely 121x585.
Using the location from (1) above, I need to read the exact color at that location.
Note: The map provider does not provide location-based data, only a colored map. I suspect multiple libraries would be needed to map coordinates to locations on the map (via a ratio?) and then use OCR to read the color.
Are there any PHP libraries/tools that do this? How would you pull this off?
Once you know the pixel-coordinates, you can use PHP's built-in GD library to sample the color of an arbitrary pixel quite easily.
The tricky bit will be determining the pixel to sample, which can get pretty darned tricky. The earth being sphere-like, maps use various projections to produce a 2-d representation. If you know how the colored map image is projected, and you know the latitude/longitude of the pixel at (0,0), you should be able to write a function to convert lat/long to a pixel coordinate.
I may not have understood the problem entirely, but you should map the locations you want into an array (of objects?)
$city_mapping = array(new City("San Francisco", 121, 585), new City....); //Map your cities to an array.
Where City should be defined as a class to contain those variables.
Then use imagecolorat() to check for the color.

Is there a way to find images of a certain color from specified sites?

First off, I don't mean google image search!
I would like to give users the ability to select a hex color value and then have a search programatically return (from specified sites/directories online) images where the dominant color is the color they specified (or close to it).
Is there a technology that can do this? I'd prefer PHP/MySQL, but I'd be willing to use other languages if it would be simpler.
EDIT
Taking several suggestions, I managed to find this: http://www.coolphptools.com/color_extract which does a decent job at extracting the most common colors from the image.
The next step is calculating distance from the extracted colors to the color being searched for. I have no issue implementing it except I'm unclear on the best way to calculate the color distance?
I've scoured this site and google for a concrete answer, but come up dry. The tool above extracts colors into hex color codes. I am currently converting this to RGB and using those.
Should I attempt to convert RGB to Y'UV? I'm attempting that by using:
sqrt(((r - r1) * .299)^2 + ((g - g1) * .587)^2 + ((b - b1) * .114)^2)
(based on an answer here: RGB to closest predefined color)
It's not very accurate. What should I swap that color distance formula with so it calculates accurate color distance (to the human eye)?
Interesting.
The first problem is: "What is the dominant colour of an image?" Maybe the one most pixels have. What do you do with similar shades of the same colour? Would you cluster around similar colours?
I would implement it this way:
Grab all images inside your search paths. Cluster the colors used in each of them and the biggest cluster is the dominant color. You will have to play around a bit with cluster sizes and number of clusters. If this color is within a certain range of hue, saturation and brightness of your searched color it is a match.
Firstly, I wonder how can you crawl over the sites/directories to search for a particular image color, unless you have a big list of websites. If it isn't related to your question then just ignore it.
Back to your question, I personally think this is an interesting question as well. Since it requires quite a few research, I just want to point out some ideas for you to reference.
What you need to do is to get user-specified hex colors and convert them into RGB colors, because most of the image functions in PHP that I know only work with RGB. Now, if you have a list of directories that you can search for, then just crawl over them and use some basic functions to get hold of the desired webpage' contents (e.g. file_get_contents, or cURL). Once you have the contents of a specific page, you will need to use DOM functions to get images' URLs from that page (you can work it out yourself, using: getElementsByTagName() and getAttribute()). Now assuming that you are holding a list of image URLs, now you need to get their colors and try to match them with your user-specified colors (remember to convert everything into RGB).
In PHP we have a very convenient GD library that works with images. If your server support GD2 then you can have a look at imagecolorclosest(). This function "Returns the index of the color in the palette of the image which is "closest" to the specified RGB value". Note that the function only returns the closest match (not exactly match), so you have to do some comparisons to choose the right images (I believe this is easy because you now have RGB colors with very handy values to work with, say, using some subtraction and adjustment method).
Moreover, not only the images, when you have a specific page content, you can try to search for the color scheme of that page (by getting its "background-color" value), there are quite a few details that you can get and play around with :) Of course, an image's color is somehow related to its page's styling scheme colors, think logically wider.
If I'm saying something not clear, don't hesitate to comment on my reply :)
Happy coding.

Categories