Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package opnctest;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferByte;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import javax.imageio.ImageIO;
- import org.opencv.core.Core;
- import org.opencv.core.CvType;
- import org.opencv.core.Mat;
- import org.opencv.core.MatOfByte;
- import org.opencv.core.Point;
- import org.opencv.core.Scalar;
- import org.opencv.core.Size;
- import org.opencv.imgcodecs.Imgcodecs;
- import org.opencv.imgproc.Imgproc;
- public class houghCircle {
- private static String circleimage = "C:\\Users\\User\\Desktop\\ball.jpg";
- public houghCircle() {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- }
- public static BufferedImage toBufferedImage(File circleimage) throws IOException {
- Image img = ImageIO.read(circleimage);
- BufferedImage bimage = new BufferedImage(400, 200, BufferedImage.TYPE_3BYTE_BGR);
- Graphics2D bGr = bimage.createGraphics();
- bGr.drawImage(img, 0, 0, null);
- bGr.dispose();
- return bimage;
- }
- public Mat bufferedImageToMat(BufferedImage bi) {
- byte[] pixels = ((DataBufferByte) bi.getRaster().getDataBuffer()).getData();
- Mat mat = new Mat(bi.getWidth(), bi.getHeight(), CvType.CV_8UC3);
- mat.put(0, 0, pixels);
- return mat;
- }
- public BufferedImage detectCircle(BufferedImage bi) {
- Mat source = Imgcodecs.imread(circleimage, Imgcodecs.CV_LOAD_IMAGE_COLOR);
- //Mat source = bufferedImageToMat(bi);
- Mat destination = new Mat(source.rows(), source.cols(), source.type());
- System.out.println("mat x" + source.width() + "mat " + source);
- Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
- Imgproc.GaussianBlur(destination, destination, new Size(3, 3), 0, 0);
- Mat circles = new Mat();
- Imgproc.HoughCircles(destination, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 20, 10, 20, 10, 20);
- int radius;
- Point pt;
- for (int x = 0; x < circles.cols(); x++) {
- double vCircle[] = circles.get(0, x);
- if (vCircle == null) {
- break;
- }
- pt = new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
- radius = (int) Math.round(vCircle[2]);
- System.out.println(radius);
- Imgproc.circle(destination, pt, radius, new Scalar(0, 255, 255), 3);
- Imgproc.circle(destination, pt, 3, new Scalar(255, 255, 255), 3);
- }
- MatOfByte matOfByte = new MatOfByte();
- Imgcodecs.imencode(".jpg", destination, matOfByte);
- byte[] byteArray = matOfByte.toArray();
- BufferedImage bufImage = null;
- try {
- InputStream in = new ByteArrayInputStream(byteArray);
- bufImage = ImageIO.read(in);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- Imgcodecs.imwrite("C:\\Users\\User\\Desktop\\ballfound.jpg", destination);
- return bufImage;
- }
- public static void main(String[] args) {
- try {
- new houghCircle().detectCircle(toBufferedImage(new File(circleimage)));
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment