Hello people,

I'm new here and new to java also... My only programming experience is in C, but now I have to do some work in java.

First, the problem is:

My input is an image file that contains different routes from a source to destination. (I have attached the sample input)

My output should be the shortest route highlighted.


Now, what I've thought of is, the way to find the shortest route is to find the route with the smallest number of black pixels. I know there is some way to return the cooridinates of pixels in java from my research so far.

I thought of traversing the image to find a blue pixel that has an adjacent black pixel to find a road, but then it occurred to me that my roads are obviously made of more than one pixel.

So, how do I differentiate between the roads, and how do I count the pixels in each road?

The highlighting part needs to have an array of all the co-ordinates of black pixels for each road, I would think. Then, according to the result of the count value, I have to change the RGB code for the co-ordinates of the shortest route so far.

Converting all this to java however is the major hurdle, I'm looking up some basic tutorials right now. If anyone could please help me, it would be much appreciated.

Thank you,


First you'll want to get a BufferedImage object for the input image. Example code for this here: http://exampledepot.com/egs/java.awt.image/Image2Buf.html?l=rel

BufferedImage will allow you to access the grid of pixels (http://exampledepot.com/egs/java.awt.image/ImagePixel.html?l=rel). You may be able to apply a thresholding function to the pixels to create an array map of the identified paths, perhaps just a binary mapping 0 or 1 to mark available paths.

From there, apply your pathfinding algorithm to the binary map and update the corresponding pixel values on the BufferedImage based upon the desired path and then save it back out to a new file (saving: http://www.exampledepot.com/egs/javax.imageio/Graphic2File.html)

Good luck!

commented: Really good explanation :) +1

Thanks for the reply.

I have been trying something, but came across an error. here is my code:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.PixelGrabber;

public class ReadImg {
    BufferedImage img;
    int[] src;
 //   public void paint(Graphics g) {
 //       g.drawImage(img, 0, 0, null);
 //   }

    public ReadImg() {
       try {
           img = ImageIO.read(new File("view.jpg"));
		       int w = img.getWidth(null);
     int h = img.getHeight(null);
    int[] rgbs = new int[w*h];
    img.getRGB(0, 0, w, h, rgbs, 0, w);
	   catch (IOException e) {}


public void main(String[] arguments) {
	int k=0,l=0;

for(int i = 0; i < img.getHeight(); i++) 
for(int j = 0; j < img.getWidth(); j++) 
if((img.getRGB(j, i) >= 000000)&&(img.getRGB(j, i) <= 0x0f0f0f))
if ((img.getRGB(j+1, i) >=0x0000ff) && (img.getRGB(j+1, i)<=0x0f0fff)||
    (img.getRGB(j+1, i+1) >=0x0000ff) && (img.getRGB(j+1, i+1)<=0x0f0fff)||
	(img.getRGB(j+1, i-1) >=0x0000ff) && (img.getRGB(j+1, i-1)<=0x0f0fff)||
	(img.getRGB(j-1, i) >=0x0000ff) && (img.getRGB(j-1, i)<=0x0f0fff)||
	(img.getRGB(j-1, i+1) >=0x0000ff) && (img.getRGB(j-1, i+1)<=0x0f0fff)||
    (img.getRGB(j-1, i-1) >=0x0000ff) && (img.getRGB(j-1, i-1)<=0x0f0fff)||
	(img.getRGB(j, i+1) >=0x0000ff) && (img.getRGB(j, i+1)<=0x0f0fff)||
	(img.getRGB(j, i-1) >=0x0000ff) && (img.getRGB(j, i-1)<=0x0f0fff))

  src[k] = j;
  src[k+1] = i;

System.out.print("Source coordinates: ");

I get this:
Exception in thread "main" java.lang.NoSuchMethodError : main

Could someone please help me out?


the main method in Java is defined as being static, so change your method signature to reflect that.

I had removed static because it was giving me the error:

cannot reference non-static variable from a static context

which indicates the real problem with your code, trying to write Java like it were a procedural language.

could someone help me convert procedural logic to Java? I'm so new to this and I'm really lost :(

Take the code that is in main() and place it into a method in the ReadImg class. Create an instance of ReadImg in main and call the appropriate methods on it as needed. The main() method is nothing more than a static access point to set up the object(s) needed to run the program. All other operation should occur inside those objects.