【问题标题】:OpenCV - Java - How to remove some pixels around a clusterOpenCV - Java - 如何删除集群周围的一些像素
【发布时间】:2023-04-02 06:56:01
【问题描述】:

我正在做一个项目,我需要识别图像的某些区域。在处理图像并删除所有不必要的东西后,我终于得到了我需要的区域,如图所示(绿色圆圈内的区域)。

我无法使用 OpenCV 在该区域周围画一个圆圈。我目前正在使用 Java 版本的 OpenCV。如果有人可以为我指出如何在图像上实现绿色圆圈的正确方向,那将非常有帮助。

我试图检测该区域的事情。 斑点检测器 - 没有取得多大成就。 Cluster - 与 blob 检测器相同。 HoughCircles - 在图像中绘制不必要的圆圈。 FindContour - 没有绘制任何东西,因为它不是一个完美的圆形、椭圆或任何其他众所周知的多边形。

感谢您的帮助。

【问题讨论】:

    标签: java opencv image-processing


    【解决方案1】:

    这里有一个解决方案:

    1. 打开以清除所有细长/细长图案中的图像。
    2. 连接组件标签以计算剩余模式
    3. 每个剩余图案的大小计数
    4. 最大的图案是您要圈出的图案。

    注意:如果要完美保留图案,可以通过重建(侵蚀+测地线重建)将开口替换为开口。

    【讨论】:

    • 感谢您的回复。我可以做第一步。我尝试了第 2 步,如果连接的组件是 2 次通过,则需要花费大量时间来处理。你有一些例子的链接,我可以让它更快吗?我会很感激的。
    • 这里是用 Java 开发的 Union-Find 算法的法文页面的链接,但是用 C++ 重新实现它真的很容易:developpez.net/forums/d480842/general-developpement/…
    • 我将在我的项目中实现这一点,并让您知道会发生什么。谢谢。
    【解决方案2】:

    我终于找到了解决问题的方法。我使用了 OpenCV 库中的特征检测器,并为检测器提供了正确的阈值。这对我有用。 Java 中的代码如下所示。

    public static void main(String[] args){
        try{
            //Validation whether a file name is passed to the function
            if(args.length == 0){
                System.out.println("here...");
                log.error("No file was passed to the function");
                throw new IOException();
            }
    
            //Read the image from the input
            Mat inputMat = Highgui.imread(args[0],Highgui.CV_LOAD_IMAGE_GRAYSCALE);
    
            //Create a feature detector. In this case we are using SURF (Speeded-Up Robust Features) detector. 
            MatOfKeyPoint objectKeyPoints = new MatOfKeyPoint();
            FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.SURF);
    
            //A temporary file is created to input Hessian Threshold to the SURF detector
            File tempFile = File.createTempFile("config", ".yml");
            String settings = "%YAML:1.0\nhessianThreshold: 7000.\noctaves: 3\noctaveLayers: 4\nupright: 0\n";
            FileWriter writer = new FileWriter(tempFile, false);
            writer.write(settings);
            writer.close();
    
            //Read the configuration from the temporary file to assign the threshold for the detector
            featureDetector.read(tempFile.getPath());
    
            //Detect the features in the image provided
            featureDetector.detect(inputMat, objectKeyPoints);
    
            //Iterate through the list of key points detected in the previous step and find the Key Point with the largest size
            List<KeyPoint> objectKeyPointList = objectKeyPoints.toList();
            KeyPoint impKeyPoint = new KeyPoint();
    
            for(int i=0; i<objectKeyPointList.size(); i++){
                if(impKeyPoint == null){
                    impKeyPoint = objectKeyPointList.get(i);
                }
                else if(impKeyPoint.size < objectKeyPointList.get(i).size){
                    impKeyPoint = objectKeyPointList.get(i);
                }
            }
    
            //If the size of the Key Point is greater than 120 then reduce the size to 120 and if the size is less than 120 then increase to 120 
            if(impKeyPoint.size > 120){
                KeyPoint tempKeyPoint = new KeyPoint();
                tempKeyPoint = impKeyPoint;
                tempKeyPoint.size = 120;
                impKeyPoint = tempKeyPoint;
            }
            else if(impKeyPoint.size < 120){
                KeyPoint tempKeyPoint = new KeyPoint();
                tempKeyPoint = impKeyPoint;
                tempKeyPoint.size = 120;
                impKeyPoint = tempKeyPoint;
            }
    
            //Convert the Key Point to MatOfKeyPoint since drawKeyPoints accepts only MatOfKeyPoint
            MatOfKeyPoint impMatOfKeyPoint = new MatOfKeyPoint(impKeyPoint);
    
            //Mat for drawing the circle in the image
            Mat outputImage = new Mat(inputMat.rows(), inputMat.cols(), Highgui.CV_LOAD_IMAGE_COLOR);
    
            //Green color for the circle
            Scalar greenCircle = new Scalar(0, 255, 0);
    
            //Draw the circle around the optic nerve when detected
            Features2d.drawKeypoints(inputMat, impMatOfKeyPoint, outputImage, greenCircle, Features2d.DRAW_RICH_KEYPOINTS);
    
            //Write the image to a file
            Highgui.imwrite("surf_keypoints.png", outputImage);
        }catch(Exception e){
            log.fatal(e.getMessage());
        }
    
    }
    

    希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2017-10-28
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多