AprilRobotics / apriltag

AprilTag is a visual fiducial system popular for robotics research.
https://april.eecs.umich.edu/software/apriltag
Other
1.56k stars 535 forks source link

coredump when apriltag detection #347

Closed biubiubiu554 closed 4 weeks ago

biubiubiu554 commented 1 month ago

Describe the bug coredump occured when some pictures was used to detect apriltag. the stack information as follows:

 0# std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) at /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ostream:561
 1# 0x00007F1BECE48F10 in /lib/x86_64-linux-gnu/libc.so.6
 2# pthread_mutex_lock in /lib/x86_64-linux-gnu/libpthread.so.0
 3# 0x00007F1B25C18FA0 in /usr/local/libtest.so
 4# workerpool_run(workerpool*) in /usr/local/[libtest.so]
 5# apriltag_detector_detect(apriltag_detector*, image_u8*) in /usr/local/libtest.so]

by debug the program,the error occurs in function :

void fit_line(struct line_fit_pt *lfps, int sz, int i0, int i1, double *lineparm, double *err, double *mse)

when the line length is 0, the line param is nan, the coredump will occured in the next matrix copute.

i can fix the problem by nan judgement, but i do not know why the line length would be zero and why the coredump in the pthread_mutex_lock .

christian-rauch commented 1 month ago

Can you provide a reproducible example (preferably via apriltag_demo) and the original image that you used?

biubiubiu554 commented 4 weeks ago

I use the old version (V3.1.5), the test code as follow:

#include <iostream>
#include <iomanip>

#include "opencv2/opencv.hpp"

extern "C" {
#include "apriltag.h"
#include "tag36h11.h"
#include "tag25h9.h"
#include "tag16h5.h"
#include "tagCircle21h7.h"
#include "tagCircle49h12.h"
#include "tagCustom48h12.h"
#include "tagStandard41h12.h"
#include "tagStandard52h13.h"
#include "common/getopt.h"
}

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    cv::Mat image = imread("../data/CameraDatas_2024-09-02_13-36-07.799_rgb.png");
    std::cout << "image channels: " << image.channels() << std::endl;
    if(image.channels() == 3)
    {
        cv::cvtColor(image, image, COLOR_BGR2GRAY);
    }
    cv::imshow("origin", image);

    apriltag_family_t *tf = tag36h11_create();
    apriltag_detector_t *td = apriltag_detector_create();
    apriltag_detector_add_family(td, tf);
    td->quad_decimate = 1.0;
    td->qtp.cos_critical_rad = cos(70 * M_PI / 180);

    image_u8_t im = {image.cols, image.rows, image.cols, image.data};
    zarray_t *detections = apriltag_detector_detect(td, &im);

    std::cout << "tag size: " << zarray_size(detections) << std::endl;
    // Draw detection outlines
    for (int i = 0; i < zarray_size(detections); i++) {
        apriltag_detection_t *det;
        zarray_get(detections, i, &det);
        line(image, Point(det->p[0][0], det->p[0][1]),
                    Point(det->p[1][0], det->p[1][1]),
                    Scalar(0, 0xff, 0), 2);
        line(image, Point(det->p[0][0], det->p[0][1]),
                    Point(det->p[3][0], det->p[3][1]),
                    Scalar(0, 0, 0xff), 2);
        line(image, Point(det->p[1][0], det->p[1][1]),
                    Point(det->p[2][0], det->p[2][1]),
                    Scalar(0xff, 0, 0), 2);
        line(image, Point(det->p[2][0], det->p[2][1]),
                    Point(det->p[3][0], det->p[3][1]),
                    Scalar(0xff, 0, 0), 2);

        stringstream ss;
        ss << det->id;
        String text = ss.str();
        int fontface = FONT_HERSHEY_SCRIPT_SIMPLEX;
        double fontscale = 1.0;
        int baseline;
        Size textsize = getTextSize(text, fontface, fontscale, 2,
                                        &baseline);
        putText(image, text, Point(det->c[0]-textsize.width/2,
                                    det->c[1]+textsize.height/2),
                fontface, fontscale, Scalar(0xff, 0x99, 0), 2);
    }
    apriltag_detections_destroy(detections);

    cv::imshow("Tag Detections", image);
    cv::waitKey(0);

}

And the source image in the attach file. The homographic_compute2 function may cause array out of range。 CameraDatas_2024-09-02_13-36-07 799_rgb

I test the latest version will not coredump.

christian-rauch commented 4 weeks ago

I use the old version (V3.1.5)

Is there a particular reason why you are using such an old version? The latest release is v3.4.2.

I cannot reproduce a crash with either version. When I run your code with v3.1.5, I get this result: Bildschirmfoto vom 2024-09-04 21-07-49 and on the terminal, I see:

image channels: 3
QSocketNotifier: Can only be used with threads started with QThread
WRN: Matrix is singular.
WRN: Matrix is singular.
tag size: 3

I test the latest version will not coredump.

I assume that this is related to the WRN: Matrix is singular. issues that have been fixed recently. If the newer releases with these fixes work for you, I strongly suggest switching to a newer version.

biubiubiu554 commented 4 weeks ago

The problem has been resolved.