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,

Kelvin

Dani AI

Generated

A short, practical checklist that ties together the existing replies from and and points to the concrete fixes and a safe path-finding approach.

Keep main static and use an instance for image work (this fixes the NoSuchMethodError / non‑static complaints). Move the pixel-traverse logic into instance methods, initialize any arrays or use an ArrayList<Point> instead of an uninitialized int[] src, and always check bounds before accessing neighbors (avoid j+1 / i-1 without testing). For color tests, extract components and apply simple thresholds rather than comparing full ARGB ints (mask with 0xFFFFFF or shift to r/g/b).

A reliable workflow:

  • Read BufferedImage with ImageIO.read(...).
  • Build a binary occupancy grid: true = road pixel (use r/g/b thresholds).
  • Locate source/destination pixels (search for blue-ish pixels via b>threshold && r,g<some limit).
  • Run BFS on the pixel grid to get the shortest pixel-count path and store predecessors to reconstruct the path.
  • Paint the reconstructed path back onto the BufferedImage and write out with ImageIO.write(...).

Example skeleton (compact):

public static void main(String[] args) throws Exception {
  ReadImg app = new ReadImg("view.jpg");
  Point s = app.findBlueEndpoint();
  Point t = app.findBlueEndpointOther();
  List<Point> path = app.findPathBFS(s,t);
  app.paintPath(path, 0xFFFF0000); // ARGB red
  ImageIO.write(app.img,"png", new File("out.png"));
}

BFS notes: use a boolean[][] seen, a Queue<Point>, and a Map<Point,Point> to store predecessors; check all 8 neighbors but skip out-of-bounds; stop when target reached and reconstruct by following predecessors.

Cautions: initialize collections, avoid direct ARGB integer comparisons, handle alpha and signed ints via masking, and consider thinning/skeletonizing the roads (e.g., Zhang–Suen) if multi-pixel-wide roads yield ambiguous centerlines. This fixes the structural Java issues flagged by and builds on ’s BufferedImage/threshold suggestion.

Recommended Answers

All 7 Replies

First you'll want to get a BufferedImage object for the input image. Example code for this here:

BufferedImage will allow you to access the grid of pixels (). 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: )

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;
  k=k+2;
}
 }
}
}




System.out.print("Source coordinates: ");
for(l=0;l<src.length;l++)
{
System.out.println(src[l]);
}
	
}}

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

Could someone please help me out?

Thanks

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.