java - opencv和python——激光曲线检测

标签 java python c++ opencv

我正在尝试获取位于这条曲线中间的一组点。 我找到了这个脚本,但我的激光图像不起作用...

im_gray = cv2.imread(img, cv2.CV_LOAD_IMAGE_GRAYSCALE)

        im_gray = cv2.Canny(im_gray,50,150,apertureSize = 3)

        ret, im_bw = cv2.threshold(im_gray, 0, 255, cv2.THRESH_BINARY)

        #(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

        #thresh = 127
        #im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]

        #ret, bw = cv2.threshold(im_bw, 0, 255, cv2.THRESH_BINARY)

        cv2.imwrite('resultpoint_bw.png',im_bw)

        # find contours of the binarized image
        contours, heirarchy = cv2.findContours(im_bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        # curves
        curves = np.zeros((im_bw.shape[0], im_bw.shape[1], 3), np.uint8)

        cv2.imwrite('resultpoint_bw_2.png',im_bw)


        for i in range(len(contours)):

            # for each contour, draw the filled contour
            draw = np.zeros((im_bw.shape[0], im_bw.shape[1]), np.uint8)
            cv2.drawContours(draw, contours, i, (255,255,255), -1)
            # for each column, calculate the centroid
            for col in range(draw.shape[0]):
                M = cv2.moments(draw[:, col])
                if M['m00'] != 0:
                    x = col
                    y = int(M['m01']/M['m00'])
                    curves[y, x, :] = (0, 0, 255)


        cv2.imwrite('resultpoint_0.png',curves)

在结果图像中点是错误的,因为它是一个轮廓并且不需要轮廓但中间的单个点...

有没有可能做这个?

最佳答案

您可以应用这些简单的步骤来获得这条中心线。

  1. 阈值二进制反转
  2. 申请Thinning algorithm以减少厚度。
  3. 在二值图像中找到非零像素。

   void thinningIteration(Mat& im, int iter)
    {
        Mat marker = Mat::zeros(im.size(), CV_8UC1);
        for (int i = 1; i < im.rows-1; i++)
        {
            for (int j = 1; j < im.cols-1; j++)
            {
                uchar p2 = im.at<uchar>(i-1, j);
                uchar p3 = im.at<uchar>(i-1, j+1);
                uchar p4 = im.at<uchar>(i, j+1);
                uchar p5 = im.at<uchar>(i+1, j+1);
                uchar p6 = im.at<uchar>(i+1, j);
                uchar p7 = im.at<uchar>(i+1, j-1);
                uchar p8 = im.at<uchar>(i, j-1);
                uchar p9 = im.at<uchar>(i-1, j-1);

                int A  = (p2 == 0 && p3 == 1) + (p3 == 0 && p4 == 1) + 
                         (p4 == 0 && p5 == 1) + (p5 == 0 && p6 == 1) + 
                         (p6 == 0 && p7 == 1) + (p7 == 0 && p8 == 1) +
                         (p8 == 0 && p9 == 1) + (p9 == 0 && p2 == 1);
                int B  = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
                int m1 = iter == 0 ? (p2 * p4 * p6) : (p2 * p4 * p8);
                int m2 = iter == 0 ? (p4 * p6 * p8) : (p2 * p6 * p8);

                if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0)
                    marker.at<uchar>(i,j) = 1;
            }
        }
        im &= ~marker;
    }

    void thinning(Mat& im)
    {
        im /= 255;
        Mat prev = Mat::zeros(im.size(), CV_8UC1);
        Mat diff;
        do 
        {
            thinningIteration(im, 0);
            thinningIteration(im, 1);
            absdiff(im, prev, diff);
            im.copyTo(prev);
        } 
        while (countNonZero(diff) > 0);

        im *= 255;
    }

    void main()
    {
        Mat mSource_Bgr,mSource_Gray,mThreshold,mThinning;
        mSource_Bgr= imread(FileName_S.c_str(),IMREAD_COLOR);
        mSource_Gray= imread(FileName_S.c_str(),0);

        threshold(mSource_Gray,mThreshold,50,255,THRESH_BINARY);

        mThinning= mThreshold.clone();

        thinning(mThinning);

        imshow("mThinning",mThinning);

enter image description here

        vector<Point2i> locations;   // output, locations of non-zero pixels 
        findNonZero(mThinning, locations);

        for (int i = 0; i < locations.size(); i++)
        {
            circle(mSource_Bgr,locations[i],2,Scalar(0,255,0),1);
        }

        imshow("mResult",mSource_Bgr);

enter image description here

    }

关于java - opencv和python——激光曲线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30478040/

相关文章:

c++ - 在内存中存储对象的正确决策(复合模式)

python - 如何让 boost.python 教程与 Visual Studio 2010 和 python 3.4 一起工作?

java - Spring,渲染列表为 <c :forEach> gives error

jvm - 为什么不是所有的 java 字节码最初都解释为机器码?

java - Wildfly 的 JAXWS 实现似乎忽略了 bindingProvider 属性 com.sun.xml.ws.transport.https.client.SSLSocketFactory

python - 我如何格式化命令参数,它们本身是一组带有 Python 子进程模块的命令?

python - 如何读取Python中的特定行并将数据打印到文本文件或控制台窗口?

python - 如何找到 pandas 时间序列的最后一个局部最大值?

java - 尝试让 HttpPost 将两个 JSON 对象发送到服务器。传递 1 个对象有效。 2 不

c++ - 将变量与音频文件同步