Java opencv和python-激光曲线检测 我试图得到一组在这个曲线中间的点。 我找到了这个脚本,但我的激光图像不起作用 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) 在结果图像中,POIT是一个轮廓,不需要等高线,而中间需要Sigle点……/P>

Java opencv和python-激光曲线检测 我试图得到一组在这个曲线中间的点。 我找到了这个脚本,但我的激光图像不起作用 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) 在结果图像中,POIT是一个轮廓,不需要等高线,而中间需要Sigle点……/P>,java,python,c++,opencv,Java,Python,C++,Opencv,有没有可能做到这一点?您可以应用这些简单的步骤来获得这条中心线 阈值二进制反转 适用于减少厚度 在二值图像中查找非零像素 我在python中找到了解决方案: import cv2 import numpy as np import glob import json, io from matplotlib import pyplot as plt from PIL import Image img = cv2.imread(fname,

有没有可能做到这一点?

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

  • 阈值二进制反转
  • 适用于减少厚度
  • 在二值图像中查找非零像素


  • 我在python中找到了解决方案:

        import cv2
        import numpy as np
        import glob
        import json, io
        from matplotlib import pyplot as plt
        from PIL import Image
    
        img = cv2.imread(fname, 0);
    
        i = Image.fromarray(self.__imgremapped_bw)
    
        pixels = i.load() # this is not a list
    
        self.__pointsData = [];
    
        find = 0
    
        for y in range(self.__top,self.__bottom):
            row_averages = []
            for x in range(self.__top,self.__bottom):
                cur_pixel = pixels[x, y]
                if cur_pixel >= self.__thresholdColor:
                    row_averages.append(x)
                    find = 1
                elif find == 1:
                    pointSum = 0
                    for idx, val in enumerate(row_averages):
                        pointSum += row_averages[idx];
    
                    xf = pointSum/len(row_averages)
                    # 0.5 correzione pixel al centro
                    self.__pointsData.append([[y+0.5,xf+0.5]])
                    row_averages = []
                    find = 0
    
        #self.__drawPoint(self.__imgremapped_bw)
    
        return self.__pointsData
    
    self.\u顶部、self.\u底部和self.\u顶部、self.\u底部是用于优化提取点的裁剪区域

    self.\uu pointsData.append([[y+0.5,xf+0.5]]

    +0.5是具有中心像素的固定值

    在这种情况下,可能会有更多的线路,因为这些线路:

                if cur_pixel >= self.__thresholdColor:
                    row_averages.append(x)
                    find = 1
                elif find == 1:
                    pointSum = 0
                    for idx, val in enumerate(row_averages):
                        pointSum += row_averages[idx];
    
                    xf = pointSum/len(row_averages)
                    # 0.5 correzione pixel al centro
                    self.__pointsData.append([[y+0.5,xf+0.5]])
                    row_averages = []
                    find = 0
    
    有一个具有颜色范围的媒体点计算

    我希望这会有帮助


    谢谢

    为什么这个标签是代码> java < /C>和<代码> C++ >代码>?因为我错了:我需要Pyton、C++和OpenCV TAG你可以添加你的图像样本吗?在这里你可以找到IMG你解决了你的问题吗?如果这对您有帮助,请标记为答案!非常感谢你!现在我需要用python转换您的脚本。有可能提取坐标吗?我把它调高了,但要注意,激光散斑通常意味着最亮的像素不会在中心。您需要首先进行大量的模糊/平滑处理,或者使用加权平均值来获得精确的激光线中心。
        }
    
        import cv2
        import numpy as np
        import glob
        import json, io
        from matplotlib import pyplot as plt
        from PIL import Image
    
        img = cv2.imread(fname, 0);
    
        i = Image.fromarray(self.__imgremapped_bw)
    
        pixels = i.load() # this is not a list
    
        self.__pointsData = [];
    
        find = 0
    
        for y in range(self.__top,self.__bottom):
            row_averages = []
            for x in range(self.__top,self.__bottom):
                cur_pixel = pixels[x, y]
                if cur_pixel >= self.__thresholdColor:
                    row_averages.append(x)
                    find = 1
                elif find == 1:
                    pointSum = 0
                    for idx, val in enumerate(row_averages):
                        pointSum += row_averages[idx];
    
                    xf = pointSum/len(row_averages)
                    # 0.5 correzione pixel al centro
                    self.__pointsData.append([[y+0.5,xf+0.5]])
                    row_averages = []
                    find = 0
    
        #self.__drawPoint(self.__imgremapped_bw)
    
        return self.__pointsData
    
                if cur_pixel >= self.__thresholdColor:
                    row_averages.append(x)
                    find = 1
                elif find == 1:
                    pointSum = 0
                    for idx, val in enumerate(row_averages):
                        pointSum += row_averages[idx];
    
                    xf = pointSum/len(row_averages)
                    # 0.5 correzione pixel al centro
                    self.__pointsData.append([[y+0.5,xf+0.5]])
                    row_averages = []
                    find = 0