How to detect arc using opencv java [closed]
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); FileChooser fileChooser = new FileChooser(); configureFileChooser(fileChooser); file = fileChooser.showOpenDialog(find_arc.getScene().getWindow()); uploadInitialProcessing();
Mat src = resultMat;
// Check if image is loaded fine
if (src.empty()) {
System.out.println("Error opening image!");
System.exit(-1);
}
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.medianBlur(gray, gray, 5);
double perimeter = Imgproc.arcLength((MatOfPoint2f) gray, true);
System.out.println(String.valueOf(perimeter));
I want to find the arc length. At the arcLength function , the first parameter must be a MatOfPoint2f one. So here i am going to convert a Mat into MatOfPoint2f. I know it is wrong(in this code.cannot cast like that) and it cant be possible. I want to find MatOfPoint2f points of Mat gray.How can i achieve that.
arclength needs a contour, not an image.
use findContours, choose the one you wanted from the list of contours, convert it to MatOfPoint2f, and try again.
hierarchyOutputVector = new Mat();
Thank You. Now this is working and returns perimeter(value). I further want to highlight arc of a given image.That means i want to draw a line on the arc to verify that is identified. How to do that @berak.
iterate over the contour, and draw a small Imgproc.circle() for each point , i guess. (or a line() from prev to next point)
if you want it in color, do that on the src image.
Imgproc.circle ( gray,
cnt.toArray(),
100,
new Scalar(0, 0, 255),
10
);
Second parameter must be org.opencv.core.point . Isn't?. I am not able to get that parameter from a cnt. Please guide me @berak.
you need to iterate over cnt.toArray(), and draw a circle for each point in the array.
also, Scalar(0, 0, 255) will be black, if you draw into the grayscale img, use the color/src one instead !
for (Point cntt : cnt.toArray()) { //Drawing a Circle Imgproc.circle( src, cntt, 100, new Scalar(0, 0, 255), 10 ); }
I did it. Now it is working. But output is like this. I cant find what is the problem .@berak please guide me further. output_image
you can cross-check with Imgproc.drawContours() (all of them), but i have no idea, what you expected to see there. the outline of the green arc ?
Yes @berak outline of the green arc.
put the original image here, so we can try
@berak. Thank You for your support. Here I am sending the original image. Images