True shortest path in binary image/map - dijkstra

How can i find the true shortest path in a binary image/map?
I have looked into Different algorithms, e.g. Dijkstra and A* but they only yield an approximateion of the the shortest path as all pixels are only connected in a "8 connected" way.
What algorithm can i use to get the true shortest path - The red line in the figure below?

Well, using a 8-connected grid implies that you only can will find paths which advance in the orientations given by the 8-neighbors (0, +/-45, +/-90, +/-135, 180). As you show in the picture attached.
If you are required to solve this problem using A*, Dijkstra or similar, the only way to patch this problem is to increase the variety of angles for your paths, increasing the connectivity of your grid (16 or 32-connected). Even with this, you have limited orientations for your paths.
To solve the problem you are describing in your question, other kind of algorithms are used, like Theta*, Field D* or Block A*. These algorithms are able to find paths in any angle (as you require in your problem) even using a 8-connected grid as basis for the search. Take a look to the Wikipedia entry: Any-angle path planning to have more information about this kind of search.
I hope my answer helps.

Related

Template matching not necessarily invariant to rotation and scale, but should detect artifacts (of 3X3 pixels or more)

I want to compare a specific pattern (as on template image) and output "yes"/"no" according to the match. I don't require the method to be scale invariant. It just has to be translation invariant and rotation invariant (only till +/-2 degrees maximum)
Also, even if there's a slight mismatch between the template image and runtime image, the output should be "no".
So far, here are a couple of codes I have tried:
Template Matching by Alaa Eleyan. This detects even if the pattern has noise, which I don't want.
Simple template match. in matlab. This outputs a score of the match. The variation of scores of images with and without noise didn't seem to vary much.
Fast/Robust Template Matching by Dirk-Jan Kroon. This too detects the pattern even if it's noisy.
Template Matching using Correlation Coefficients by Yue Wu. It is similar to #3 but takes more time.
Many of them are not invariant to rotation. So at present, I match SURF features and calculate how much the runtime image has been rotated wrt template pattern. I then rotate it in the opposite direction so I need not apply an algorithm which is invariant to rotation (this is why I don't need rotation invariance).
In many cases even if there's no pattern present, it's still falsely detected. Here is a screenshot of output using #3:
Here is another such wrong output:
I have also worked on a couple of Algorithms based on SIFT/ASIFT Features earlier. They are very robust and obviously match patterns even if it's noisy. Hence, I am not using these in the present application.
I have attached an example Template image, and Yes and No images for your reference.
Please let me know an algorithm for this purpose. I thought it was a simple template match, but in many cases, it was falsely detected.
At present I think I can use #3 algorithm to at least detect the position of pattern in runtime image (as shown in 2nd screenshot above) and match this region. Is this possible? I can't use image subtraction because it's not exactly a pixel-to-pixel match. There may be very slight variations.
Any inputs will be appreciated.
Regards,
Meghana.
Edit: I tried using Zernike Moments by Amir Tahmasbi. Although I got two outputs for each image, A and phi, I am not sure if I can count on this output. The value for A seems to be very close for good ('yes') and bad ('no') images. I have the screen shot posted here, along with the two outputs for each image. For my issue, is there any other way to infer these two outputs of each image so I can categorise them as good/bad?
This is for good image:
This is for three bad images:

How to Extract part of Image using Matlab

i have not used matlab much, i need to extract the part of Left and Right Coronary Arteries of a heart from a given heart image.
this is my image,
based on morphological operations, this is what i have come up with,
f=imread('heart.jpg');
diam=strel('diamond',19);
line=strel('line',10,90);
linef=imclose(f,line);
line120=strel('line',10,120);
line120f= imclose(f,line120);
bothline=linef+line120f;
diamf=imclose(f,diam);
arterybm=diamf-bothline;
binaryartery= im2bw(arterybm,0);
mask=cast(binaryartery,class(f));
redPlane=f(:,:,1);
greenPlane=f(:,:,2);
bluePlane=f(:,:,3);
maskedRed=redPlane.*mask;
maskedGreen=greenPlane.*mask;
maskedBlue=bluePlane.*mask;
maskedRGBImage=cat(3,maskedRed,maskedGreen,maskedBlue);
subplot(2,3,1);imshow(f);title('Input Image');subplot(2,3,2);imshow(diamf);title('imclose with Diamond Mask');subplot(2,3,3);imshow(bothline);title('imclose with Line 120 and 90 mask');subplot(2,3,4);imshow(arterybm);title('Difference of line and diamond');subplot(2,3,5);imshow(binaryartery);title('Convert to binary image');subplot(2,3,6);imshow(maskedRGBImage);title('Apply mask to input image');
is there any better approach ?
This task is quite a difficult one, worth academic article if you find solution working flawlessly in most cases. My suggestion: search for articles on the topic, and also try "Matlab File Exchange" (http://www.mathworks.com/matlabcentral/fileexchange/). If you are very lucky, someone might have already solved this problem and posted a solution.
Have a look at the Frangi filter (aka ridge-detection filter), it is meant to detect blood vessels.
There is an implementation available on the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/24409-hessian-based-frangi-vesselness-filter

Why do we call it "Relaxing" an edge?

In Dijkstra's shortest path algorithm and others, to examine an edge to see if it offers a better path to a node is referred to as relaxing the edge. Why is it called relaxing?
In general mathematically, relaxation is making a change that reduces constraints. When the Dijkstra algorithm examines an edge, it removes an edge from the pool, thereby reducing the number of constraints.
It's not horribly useful terminology, but think how cool you'll sound saying it.
I do not think the question is related to the mathematical concept talked about in the current accepted answer. I am basing my answer on the second edition of Cormen's (CLRS) book, specifically on chapter 24 in a section about relaxation.
Context:
You are searching for the shortest path between a node s and all the other nodes. Imagine you have two nodes u and v. You already have an intermediate path for them.
relax(u,v) is a function that should be read as "relax v using u". I prefer to understand the function as "shorten v's distance to s using u". You are asking yourself if you should give up on your current path s-v in favor of transforming it into s-u-v. All you have to do to see if the distance of s-u plus the additional cost of the arrow u-v are better than the distance s-v. The picture examplifies the
function
A picture from CLRS's explanation on Relaxation

trainning neural network

I have a picture.1200*1175 pixel.I want to train a net(mlp or hopfield) to learn a specific part of it(201*111pixel) to save its weight to use in a new net(with the same previous feature)only without train it to find that specific part.now there are this questions :what kind of nets is useful;mlp or hopfield,if mlp;the number of hidden layers;the trainlm function is unuseful because "out of memory" error.I convert the picture to a binary image,is it useful?
What exactly do you need the solution to do? Find an object with an image (like "Where's Waldo"?). Will the target object always be the same size and orientation? Might it look different because of lighting changes?
If you just need to find a fixed pattern of pixels within a larger image, I suggest using a straightforward correlation measure, such as crosscorrelation to find it efficiently.
If you need to contend with any of the issues mentioned above, then there are two basic solutions: 1. Build a model using examples of the object in different poses, scalings, etc. so that the model will recognize any of them, or 2. Develop a way to normalize the patch of pixels being examined, to minimize the effect of those distortions (like Hu's invariant moments). If nothing else, yuo'll want to perform some sort of data reduction to get the number of inputs down. Technically, you could also try a model which is invariant to rotations, etc., but I don't know how well those work. I suspect that they are more tempermental than traditional approaches.
I found AdaBoost to be helpful in picking out only important bits of an image. That, and resizing the image to something very tiny (like 40x30) using a Gaussian filter will speed it up and put weight on more of an area of the photo rather than on a tiny insignificant pixel.

Dijkstra algorithm for iPhone

It is possible to easily use the GPS functionality in the iPhone since sdk 3.0, but it is explicitly forbidden to use Google's Maps.
This has two implications, I think:
You will have to provide maps yourself
You will have to calculate the shortest routes yourself.
I know that calculating the shortest route has puzzled mathematicians for ages, but both Tom Tom and Google are doing a great job, so that issue seems to have been solved.
Searching on the 'net, not being a mathematician myself, I came across the Dijkstra Algorithm. Is there anyone of you who has successfully used this algorithm in a Maps-like app in the iPhone?
Would you be willing to share it with me/the community?
Would this be the right approach, or are the other options?
Thank you so much for your consideration.
I do not believe Dijkstra's algorithm would be useful for real-world mapping because, as Tom Leys said (I would comment on his post, but lack the rep to do so), it requires a single starting point. If the starting point changes, everything must be recalculated, and I would imagine this would be quite slow on a device like the iPhone for a significantly large data set.
Dijkstra's algorithm is for finding the shortest path to all nodes (from a single starting node). Game programmers use a directed search such as A*. Where Dijkstra processes the node that is closest to the starting position first, A* processes the one that is estimated to be nearest to the end position
The way this works is that you provide a cheap "estimate" function from any given position to the end point. A good example is how far a bird would fly to get there. A* adds this to the current distance from the start for each node and then chooses the node that seems to be on the shortest path.
The better your estimate, the shorter the time it will take to find a good path. If this time is still too long, you can do a path find on a simple map and then another on a more complex map to find the route between the places you found on the simple map.
Update
After much searching, I have found an article on A* for you to to read
Dijkstra's algorithm is O(m log n) for n nodes and m edges (for a single path) and is efficient enough to be used for network routing. This means that it's efficient enough to be used for a one-off computation.
Briefly, Dijkstra's algorithm works like:
Take the start node
Assign it a depth of zero
Insert it into a priority queue at its depth key
Repeat:
Pop the node with the lowest depth from the priority queue
Record the node that you came from so you can track the path back
Mark the node as having been visited
If this node is the destination:
Break
For each neighbour:
If the node has not previously been visited:
Calculate depth as depth of current node + distance to neighbour
Insert neighbour into the priority queue at the calculated depth.
Return the destination node and list of the nodes through which it was reached.
Contrary to popular belief, Dijkstra's algorithm is not necessarily an all-pairs shortest path calculator, although it can be adapted to do this.
You would have to get a graph of the streets and intersections with the distances between the intersections. If you had this data you could use Dijkstra's algorithm to compute a shortest route.
If you look at technology tomtom calls 'IQ routes', they measure actual speed and travel time per roadstretch per time of day. This makes the arrival time more accurate. So the expected arrival time is more fact-based http://www.tomtom.com/page/iq-routes
Calculating a route using the A* algorithm is plenty fast enough on an iPhone with offline map data. I have experience of doing this commercially. I use the A* algorithm as documented on Wikipedia, and I keep the road network in memory and re-use it; once it's loaded, routing even over a large area like Spain or the western half of Canada is practically instant.
I take data from OpenStreetMap or elswhere and convert it into a directed graph, assuming (which is the right way to do it according to those who know) that any two roads sharing a point with the same ID are joined. I assign weights to different types of roads based on expected speeds, and if a portion of a road is one-way I create only a single arc; two-way roads get two arcs, one in each direction. That's pretty much the whole thing apart from some ad-hoc code to prevent dangerous turns, and implementing routing restrictions.
This was discussed earlier here: What algorithms compute directions from point a to point b on a map?
Have a look at CloudMade. They offer a free service for iPhone and iPad that allows navigation based on your current location. It is built on open street maps and has some nifty features like making your own mapstyle. It is a little slow from time to time but its totally free.