How to detect arc using opencv java [closed]

asked 2017-11-15 01:49:20 -0600

ChukZ gravatar image

updated 2017-11-15 03:52:28 -0600

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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-14 08:42:37.688416

Comments

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.

berak gravatar imageberak ( 2017-11-15 04:04:14 -0600 )edit

hierarchyOutputVector = new Mat();

List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(
        gray,
        contours,
        hierarchyOutputVector,
        Imgproc.RETR_EXTERNAL,
        Imgproc.CHAIN_APPROX_SIMPLE
);
for (MatOfPoint cnt : contours) {
    MatOfPoint2f newPoint = new MatOfPoint2f(cnt.toArray());
    double perimeter = Imgproc.arcLength(newPoint, true);
    System.out.println(String.valueOf(perimeter));

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.

ChukZ gravatar imageChukZ ( 2017-11-15 04:55:02 -0600 )edit

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.

berak gravatar imageberak ( 2017-11-15 04:58:18 -0600 )edit

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.

ChukZ gravatar imageChukZ ( 2017-11-15 05:32:11 -0600 )edit

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 !

berak gravatar imageberak ( 2017-11-15 05:33:32 -0600 )edit

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

ChukZ gravatar imageChukZ ( 2017-11-15 05:48:55 -0600 )edit

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 ?

berak gravatar imageberak ( 2017-11-15 05:54:35 -0600 )edit

Yes @berak outline of the green arc.

ChukZ gravatar imageChukZ ( 2017-11-15 05:57:10 -0600 )edit

put the original image here, so we can try

berak gravatar imageberak ( 2017-11-15 06:00:43 -0600 )edit

@berak. Thank You for your support. Here I am sending the original image. Images

ChukZ gravatar imageChukZ ( 2017-11-15 06:05:37 -0600 )edit