import org.opencv.core.*; import org.opencv.calib3d.*; import org.opencv.highgui.*; import org.opencv.imgproc.*; import java.util.*; import java.io.*; class VisualMeasureExample { static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } public static double PIXEL_TO_MM = 688.0 / 20.0; public static double MM_TO_PIXEL = 20.0 / 688.0; public static double estimateMM2Pixel(String referenceImgFile) { Mat ref_img = new Mat(); ref_img = Highgui.imread(referenceImgFile, Highgui.CV_LOAD_IMAGE_GRAYSCALE); //Imgproc.equalizeHist(ref_img, ref_img); double thresh_value = 128; Mat binary_img = new Mat(); thresh_value = Imgproc.threshold(ref_img, binary_img, thresh_value, 255, Imgproc.THRESH_OTSU); Highgui.imwrite("ref_gray_histeq.png", binary_img); Mat dist_img = new Mat(); Imgproc.distanceTransform(binary_img, dist_img, Imgproc.CV_DIST_L2, Imgproc.CV_DIST_MASK_PRECISE); float[] dist_vals = new float[dist_img.rows()*dist_img.cols()]; dist_img.get(0,0, dist_vals); float max = 0.0f; for (int i = 0; i < dist_img.rows()*dist_img.cols(); i++) { if ( max < dist_vals[i]) { max = dist_vals[i]; } } System.out.println("max = " + max); Highgui.imwrite("ref_distmap.png", dist_img); return (double) (20.0 / (2.0 * max) ); // square is 20 mm } public static void main(String[] args) { Mat input_img = new Mat(); Mat gray_img = new Mat(); input_img = Highgui.imread("csavaranyaM5_telecentric_WD175mm.bmp"); VisualMeasureExample.MM_TO_PIXEL = VisualMeasureExample.estimateMM2Pixel("reference_sqaure_telecentric_WD175mm.bmp"); Imgproc.cvtColor(input_img, gray_img, Imgproc.COLOR_BGR2GRAY); Highgui.imwrite("csavaranya_gray.png", gray_img); Mat binary_img = new Mat(); double thresh_value = 220; thresh_value = Imgproc.threshold(gray_img, binary_img, thresh_value, 255, Imgproc.THRESH_BINARY_INV); System.out.println("threshold value = " + thresh_value); Mat kernel = new Mat(3,3, CvType.CV_8U); Mat eroded_img = new Mat(); Mat contour_img = new Mat(); Imgproc.erode(binary_img, eroded_img, kernel, new Point(-1,-1), 1, Imgproc.BORDER_CONSTANT, new Scalar(0)); Core.bitwise_xor(binary_img,eroded_img, contour_img); Mat dilated = new Mat(); Imgproc.dilate(contour_img, contour_img, kernel, new Point(-1,-1), 1, Imgproc.BORDER_CONSTANT, new Scalar(0)); Highgui.imwrite("csavaranya_contour.png", contour_img); Mat circles = new Mat(); Imgproc.HoughCircles(contour_img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, gray_img.width()); Mat circle_img = new Mat(gray_img.rows(), gray_img.cols(), CvType.CV_8U); circle_img = gray_img.clone(); System.out.println("circles = " + circles.dump()); float[] circleData = new float[3]; circles.get(0,0, circleData); /* float[] cx = new float[1]; float[] cy = new float[1]; float[] r = new float[1]; circles.get(0,0, cx); circles.get(0,1, cy); circles.get(0,2, r); */ Imgproc.cvtColor(circle_img, circle_img, Imgproc.COLOR_GRAY2BGR); Core.circle(circle_img, new Point(circleData[0], circleData[1]), (int)(circleData[2]), new Scalar(0,0,255), 3); Highgui.imwrite( "circle.png", circle_img ); System.out.println("Radius = " + (circleData[2] * VisualMeasureExample.MM_TO_PIXEL) + "mm"); System.out.println("Diameter = " + (2* circleData[2] * VisualMeasureExample.MM_TO_PIXEL) + "mm"); } }