Log in
updated 11:29 AM UTC, May 4, 2016

Find adjacent colors in an image using JavaCV

  • Written by Madhu V Rao
  • Published in Core Java

Here is a small program to find adjacent colours in an image using JAVA CV based on OpenCV

 

import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_core.cvGet2D;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.bytedeco.javacpp.opencv_core.CvScalar;
import org.bytedeco.javacpp.opencv_core.IplImage;

public class ImageProcessing {
	
	class RGB {
	

		double red, green, blue;
		
		public RGB(double red, double green, double blue) {
			this.red = red;
			this.green = green;
			this.blue = blue;
		}
		
		public double red() {
			return red;
		}
		
		public double green() {
			return green;
		}
		
		public double blue() {
			return blue;
		}

		private ImageProcessing getOuterType() {
			return ImageProcessing.this;
		}
		
		


		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			long temp;
			temp = Double.doubleToLongBits(blue);
			result = prime * result + (int) (temp ^ (temp >>> 32));
			temp = Double.doubleToLongBits(green);
			result = prime * result + (int) (temp ^ (temp >>> 32));
			temp = Double.doubleToLongBits(red);
			result = prime * result + (int) (temp ^ (temp >>> 32));
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			RGB other = (RGB) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (Double.doubleToLongBits(blue) != Double
					.doubleToLongBits(other.blue))
				return false;
			if (Double.doubleToLongBits(green) != Double
					.doubleToLongBits(other.green))
				return false;
			if (Double.doubleToLongBits(red) != Double
					.doubleToLongBits(other.red))
				return false;
			return true;
		}
		
		@Override
		public String toString() {
			
			return "[ R: " + red() + " G: " + green() + " B: " + blue() + " ] ";
			
		}
	}
	
	Map<RGB, List<RGB>> adjacentColors = new HashedMap();
	
	
	
	
	 public static void main(String[] args) throws Exception {
		 
		 
		new ImageProcessing().processImages();
		 
	 }




	private void processImages() {
		IplImage rawImage  = cvLoadImage("amend.png");
		 ByteBuffer buffer = rawImage.getByteBuffer();
		 
		 
		 
		 for(int h = 0; h < rawImage.height();h++) {
			 for(int w = 0; w<rawImage.width();w++) {

				 CvScalar s= cvGet2D(rawImage,h,w);
				 int nextEleOnSameLevel = w + 1;
				 int nextEleOnLowerLevel = h +1;
				 
				 RGB rgb = new RGB(s.val(2),s.val(1),s.val(0));
				 
				 if(nextEleOnSameLevel < rawImage.width()) {
					 s= cvGet2D(rawImage,h,nextEleOnSameLevel);
					 RGB nextRGB = new RGB(s.val(2),s.val(1),s.val(0));
					 if(!rgb.equals(nextRGB)) {
						 addToAdjacentColors(rgb, nextRGB);
					 }
				 }
				 
				 if(nextEleOnLowerLevel < rawImage.height()) {
					 s= cvGet2D(rawImage,nextEleOnLowerLevel,w);
					 RGB lowerRGB = new RGB(s.val(2),s.val(1),s.val(0));
					 if(!rgb.equals(lowerRGB)) {
						 addToAdjacentColors(rgb, lowerRGB);
					 }
				 }
				 
			 }
		 }
		 
		 for (Map.Entry<RGB, List<RGB>> entry : adjacentColors.entrySet())
		 {
		     System.out.println(entry.getKey() + " / " + entry.getValue());
		 }
	}




	private void addToAdjacentColors(RGB rgb, RGB nextRGB) {
		List<RGB> colors = adjacentColors.get(rgb);

		if (colors != null) {
			addColorIfNotExists(colors, nextRGB);
		} else {
			List<RGB> rgbColors = new ArrayList<ImageProcessing.RGB>();
			rgbColors.add(nextRGB);
			adjacentColors.put(rgb,rgbColors);
		}
	}


	private void addColorIfNotExists(List<RGB> colors, RGB nextRGB) {
		for(RGB rgb:colors) {
			if(rgb.equals(nextRGB)) {
				return;
			}
		}
		colors.add(nextRGB);
	}
}


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest