【问题标题】:How to filter contour by brightness如何按亮度过滤轮廓
【发布时间】:2016-03-09 18:30:03
【问题描述】:

几周前我开始使用 opencv。我想知道是否有一个功能可以从轮廓列表中找出最亮的轮廓并绘制最亮的轮廓。到目前为止,我设法转换灰度、阈值图像并使用 findContour 函数找到图像中的所有整个轮廓。

尝试使用 minMax 函数,但找不到它在 java 中的使用方式。

 public void process(Mat rgbaImage) {    
        Imgproc.threshold(rgbaImage,rgbaImage,230,255,Imgproc.THRESH_BINARY);    
        Imgproc.findContours(rgbaImage,contours,mHierarchy,Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);

       /* for(int id = 0; id < contours.size();id++) {

            double area = Imgproc.contourArea(contours.get(id));
            if (area > 8000){
                Log.i(TAG1, "contents founds at id" + id);
            }    

        } */   

    }`

【问题讨论】:

    标签: java opencv android-studio


    【解决方案1】:

    如果您的“最亮”表示最亮的平均颜色,则可以使用 cv::mean(Mat src, Mat mask)。
    遗憾的是我只知道 C++ OpenCV 实现,但我认为 Java 版本与 C++ 几乎相同。

    C++ 示例:

    Mat src;  // This is your src image
    vector<vector<Point>> contours;   // This is your array of contours
    
    findContours(src.clone(), contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
    
    int brightestIdx = -1;
    int brightestColor = -1;
    for(int i=0; i<contours.size(); i++)
    {
        // First, make a mask image of each contour
        Mat mask(src.cols, src.rows, CV_8U, Scalar(0));
        drawContours(mask, contours, i, Scalar(255), CV_FILLED);
    
        // Second, calculate average brightness with mask
        Scalar m = mean(src, mask);
    
        // Finally, compare current average with previous one
        if(m[0] > brightestColor)
        {
            brightestColor = m[0];
            brightestIdx = i;
        }
    }
    
    // Now you've found the brightest index.
    // Do whatever you want.
    
    Mat brightest_only(src.cols, src.rows, CV_8U, Scalar(0));
    drawContours(brightest_only, contours, brightestIdx, Scalar(255), 1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 2022-12-18
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      • 2020-09-22
      • 2015-06-09
      • 2017-05-14
      相关资源
      最近更新 更多