madhawaseeeee

Untitled

Feb 12th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package opnctest;
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.DataBufferByte;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import javax.imageio.ImageIO;
  12. import org.opencv.core.Core;
  13. import org.opencv.core.CvType;
  14. import org.opencv.core.Mat;
  15. import org.opencv.core.MatOfByte;
  16. import org.opencv.core.Point;
  17. import org.opencv.core.Scalar;
  18. import org.opencv.core.Size;
  19. import org.opencv.imgcodecs.Imgcodecs;
  20. import org.opencv.imgproc.Imgproc;
  21.  
  22. public class houghCircle {
  23.  
  24.     private static String circleimage = "C:\\Users\\User\\Desktop\\ball.jpg";
  25.  
  26.     public houghCircle() {
  27.         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  28.     }
  29.  
  30.     public static BufferedImage toBufferedImage(File circleimage) throws IOException {
  31.         Image img = ImageIO.read(circleimage);
  32.         BufferedImage bimage = new BufferedImage(400, 200, BufferedImage.TYPE_3BYTE_BGR);
  33.         Graphics2D bGr = bimage.createGraphics();
  34.         bGr.drawImage(img, 0, 0, null);
  35.         bGr.dispose();
  36.         return bimage;
  37.     }
  38.  
  39.     public Mat bufferedImageToMat(BufferedImage bi) {
  40.         byte[] pixels = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
  41.         Mat mat = new Mat(bi.getWidth(), bi.getHeight(), CvType.CV_8UC3);
  42.         mat.put(0, 0, pixels);
  43.         return mat;
  44.     }
  45.  
  46.     public BufferedImage detectCircle(BufferedImage bi) {
  47.         Mat source = Imgcodecs.imread(circleimage, Imgcodecs.CV_LOAD_IMAGE_COLOR);
  48.         //Mat source = bufferedImageToMat(bi);
  49.         Mat destination = new Mat(source.rows(), source.cols(), source.type());
  50.         System.out.println("mat x" + source.width() + "mat " + source);
  51.         Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
  52.         Imgproc.GaussianBlur(destination, destination, new Size(3, 3), 0, 0);
  53.  
  54.         Mat circles = new Mat();
  55.         Imgproc.HoughCircles(destination, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 20, 10, 20, 10, 20);
  56.  
  57.         int radius;
  58.         Point pt;
  59.         for (int x = 0; x < circles.cols(); x++) {
  60.             double vCircle[] = circles.get(0, x);
  61.  
  62.             if (vCircle == null) {
  63.                 break;
  64.             }
  65.  
  66.             pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
  67.             radius = (int) Math.round(vCircle[2]);
  68.             System.out.println(radius);
  69.             Imgproc.circle(destination, pt, radius, new Scalar(0, 255, 255), 3);
  70.             Imgproc.circle(destination, pt, 3, new Scalar(255, 255, 255), 3);
  71.  
  72.         }
  73.         MatOfByte matOfByte = new MatOfByte();
  74.         Imgcodecs.imencode(".jpg", destination, matOfByte);
  75.         byte[] byteArray = matOfByte.toArray();
  76.         BufferedImage bufImage = null;
  77.         try {
  78.             InputStream in = new ByteArrayInputStream(byteArray);
  79.             bufImage = ImageIO.read(in);
  80.         } catch (Exception ex) {
  81.             ex.printStackTrace();
  82.         }
  83.  
  84.         Imgcodecs.imwrite("C:\\Users\\User\\Desktop\\ballfound.jpg", destination);
  85.         return bufImage;
  86.     }
  87.  
  88.     public static void main(String[] args) {
  89.         try {
  90.             new houghCircle().detectCircle(toBufferedImage(new File(circleimage)));
  91.         } catch (IOException ex) {
  92.             ex.printStackTrace();
  93.         }
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment