Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone, in this project I'm trying to convert an image to a matrix of ASCII characters but I got stuck at some point and I would be happy te get your help.
The idea is:
- Load an image as a Mat object
- Iterate through all the pixels and store them in a 2D array
where each cell is going to be an array of 3 variables [R, G, B]
- Iterate through the pixels array and get the average of the RGB values
and store them into an array called brightness
- Iterate through the brightness array and map each double value to a specific char
where the chars are saved in the chars string variable

<pre>public static void main(String[] args) {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
		Imgcodecs imageCodecs = new Imgcodecs();

		// Reading the image which will be returned as Mat obj
		Mat matrix = Imgcodecs.imread("pineapple.jpg");

		Mat c = matrix.clone();
		int pixelsCounter = 0;
		
		System.out.println("width: " + c.width() + ", height: " + c.height());
		ArrayList<ArrayList<Double>> pixels = new ArrayList<>();
		for (int i = 0; i < c.height(); i++) {
			for (int j = 0; j < c.width(); j++) {
				pixelsCounter++;
				ArrayList<Double> tmp = new ArrayList<>();
				tmp.add(c.get(i, j)[0]);
				tmp.add(c.get(i, j)[1]);
				tmp.add(c.get(i, j)[2]);
				pixels.add(tmp);
			}
		}
		
		System.out.println("Pixels: " + pixelsCounter);

		  ArrayList<Double> brightness = new ArrayList<>();
		  for(int i = 0; i < pixels.size(); i++) {
			  double avgBrightness = (pixels.get(i).get(0) + pixels.get(i).get(1) + 
                                                               pixels.get(i).get(2))/3;
			  brightness.add(avgBrightness);			  
		  }
		  
		  Map<Double, Character> map = new HashMap<>();
		  final String chars = "`^\\\",:;Il!i~+_-?][}{1) 
                            (|\\\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
		  for(int i = 0; i < brightness.size(); i++) { 
			  Character c1 = chars.charAt((int) Math.ceil(((chars.length() - 1) * 
                                                           brightness.get(i)/255))); 
			  map.put(brightness.get(i), c1);
		  }
		  
		  map.entrySet().forEach( entry -> {
			    System.out.println( entry.getKey() + " => " + entry.getValue() );
		 });

	}


Till this point my program does all the above steps that I mentioned. The problem is that it I don't know how to print these chars in a way that it will look like a real ascii art figure?

What I have tried:

I've tried so many ways to do this but I can't find anything online related to how to do this in Java using OpenCV library.
Posted
Comments
Richard MacCutchan 6-Feb-22 9:08am    
You need to map the characters in rows that correspond to the rows of the original image.
haytam7 6-Feb-22 19:07pm    
can you explain more on this?
Richard MacCutchan 7-Feb-22 4:13am    
I don't understand what you are asking. An image consists of Y rows of X pixels, which together provide the colours of the picture. So in your conversion you first need to know the values of Y and X, and from that you can create your 2D array. So as you process each pixel you use its location to set the corresponding character in the character array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900