Обнаружение различных типов капель на карте Используя OpenCV findContours - PullRequest
0 голосов
/ 29 января 2020

Я пытаюсь определить капли на чувствительной к воде карте, как вы можете видеть на рисунке ниже, помимо капель есть риски для воды, которые я не хочу учитывать. Я использую функцию findContours OpenCV, чтобы обнаружить эти контуры, вопрос: могу ли я отделить настоящие капли от капель воды на карте? Вот выдержка из моего кода.

enter image description here

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

Mat src; Mat src_gray; Mat binary_image, goTo;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

cv::Scalar min_color_scanner = Scalar(0,0,0);
cv::Scalar max_color_scanner = Scalar(255,175,210);


int main(int argc, char** argv){
    cv::Mat image, gray, thresh;

    // MARK:- Load image, grayscale, Otsu's threshold
    image = imread("/Users/user/Documents/Developer/Desktop/OpenCV-Teste3.3.1/normal1.png");


    Mat circles_detect;
    cvtColor( image, circles_detect, CV_BGR2GRAY );
    GaussianBlur( circles_detect, circles_detect, Size(9, 9), 2, 2 );
    //END CIRCLES
    cvtColor(image, gray, CV_BGR2GRAY);

    threshold(gray, thresh, 0, 255, THRESH_BINARY_INV + THRESH_OTSU);
    Mat mask(image.rows, image.cols, CV_8UC3, Scalar(255,255,255));


    cv::Mat bgr_image, inRangeImage;
    cv::cvtColor(image, bgr_image, CV_RGB2BGR);
    cv::inRange(bgr_image, min_color_scanner, max_color_scanner, binary_image);


    //Find contours and filter using contour area
    vector<vector<Point>> contours;
    cv::findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);


    // MARK:- data from image
    double largest_area=0.0;
    int largest_contour_index=0;
    double smallest_area=0.0;
    int smallest_contour_index=0;
    int drop_derive=0;
    Rect boundig_rect;
    for(int i=0;i<contours.size();i++){
        double  area = contourArea(contours[i]);
        if(area > largest_area){
            largest_area=area;
            largest_contour_index = i;
            //boundig_rect = boundingRect(contourArea(contours[i]));
        }
    }

    smallest_area = largest_area;
    for(int i=0;i<contours.size();i++){
        double area = contourArea(contours[i]);
        if(area < smallest_area){
            smallest_area=area;
            smallest_contour_index = i;

            //boundig_rect = boundingRect(contourArea(contours[i]));
        }


        if (area < 4){
            drop_derive++;
            cv::drawContours(image, contours, i, Scalar(255,0,0));
        }
    } 

//show datas and images..
return(0);
}

...