• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            OpenCV detect partial circle with noise

            https://stackoverflow.com/questions/26222525/opencv-detect-partial-circle-with-noise

            using this as input (your own median filtered image (I've just cropped it):

            enter image description here

            First I "normalize" the image. I just stretch values, that smallest val is 0 and biggest val is 255, leading to this result: (maybe some real contrast enhancement is better)

            enter image description here

            after that I compute the threshold of that image with some fixed threshold (you might need to edit that and find a way to choose the threshold dynamically! a better contrast enhancement might help there)

            enter image description here

            from this image, I use some simple RANSAC circle detection(very similar to my answer in the linked semi-circle detection question), giving you this result as a best semi-sircle:

            enter image description here

            int main()


            {

                //cv::Mat color = cv::imread("../inputData/semi_circle_contrast.png");

                cv::Mat color = cv::imread("../inputData/semi_circle_median.png");

                cv::Mat gray;

             

                // convert to grayscale

                cv::cvtColor(color, gray, CV_BGR2GRAY);

             

                // now map brightest pixel to 255 and smalles pixel val to 0. this is for easier finding of threshold

                double min, max;

                cv::minMaxLoc(gray,&min,&max);

                float sub = min;

                float mult = 255.0f/(float)(max-sub);

                cv::Mat normalized = gray - sub;

                normalized = mult * normalized;

                cv::imshow("normalized" , normalized);

                //--------------------------------

             

             

                // now compute threshold

                // TODO: this might ne a tricky task if noise differs...

                cv::Mat mask;

                //cv::threshold(input, mask, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

                cv::threshold(normalized, mask, 100, 255, CV_THRESH_BINARY);

             

             

             

                std::vector<cv::Point2f> edgePositions;

                edgePositions = getPointPositions(mask);

             

                // create distance transform to efficiently evaluate distance to nearest edge

                cv::Mat dt;

                cv::distanceTransform(255-mask, dt,CV_DIST_L1, 3);

             

                //TODO: maybe seed random variable for real random numbers.

             

                unsigned int nIterations = 0;

             

                cv::Point2f bestCircleCenter;

                float bestCircleRadius;

                float bestCirclePercentage = 0;

                float minRadius = 50;   // TODO: ADJUST THIS PARAMETER TO YOUR NEEDS, otherwise smaller circles wont be detected or "small noise circles" will have a high percentage of completion

             

                //float minCirclePercentage = 0.2f;

                float minCirclePercentage = 0.05f;  // at least 5% of a circle must be present? maybe more...

             

                int maxNrOfIterations = edgePositions.size();   // TODO: adjust this parameter or include some real ransac criteria with inlier/outlier percentages to decide when to stop

             

                for(unsigned int its=0; its< maxNrOfIterations; ++its)

                {

                    //RANSAC: randomly choose 3 point and create a circle:

                    //TODO: choose randomly but more intelligent,

                    //so that it is more likely to choose three points of a circle.

                    //For example if there are many small circles, it is unlikely to randomly choose 3 points of the same circle.

                    unsigned int idx1 = rand()%edgePositions.size();

                    unsigned int idx2 = rand()%edgePositions.size();

                    unsigned int idx3 = rand()%edgePositions.size();

             

                    // we need 3 different samples:

                    if(idx1 == idx2) continue;

                    if(idx1 == idx3) continue;

                    if(idx3 == idx2) continue;

             

                    // create circle from 3 points:

                    cv::Point2f center; float radius;

                    getCircle(edgePositions[idx1],edgePositions[idx2],edgePositions[idx3],center,radius);

             

                    // inlier set unused at the moment but could be used to approximate a (more robust) circle from alle inlier

                    std::vector<cv::Point2f> inlierSet;

             

                    //verify or falsify the circle by inlier counting:

                    float cPerc = verifyCircle(dt,center,radius, inlierSet);

             

                    // update best circle information if necessary

                    if(cPerc >= bestCirclePercentage)

                        if(radius >= minRadius)

                    {

                        bestCirclePercentage = cPerc;

                        bestCircleRadius = radius;

                        bestCircleCenter = center;

                    }

             

                }

             

                // draw if good circle was found

                if(bestCirclePercentage >= minCirclePercentage)

                    if(bestCircleRadius >= minRadius);

                    cv::circle(color, bestCircleCenter,bestCircleRadius, cv::Scalar(255,255,0),1);

             

             

                    cv::imshow("output",color);

                    cv::imshow("mask",mask);

                    cv::waitKey(0);

             

                    return 0;

                }

             

            float verifyCircle(cv::Mat dt, cv::Point2f center, float radius, std::vector<cv::Point2f> & inlierSet)
            {
             unsigned int counter = 0;
             unsigned int inlier = 0;
             float minInlierDist = 2.0f;
             float maxInlierDistMax = 100.0f;
             float maxInlierDist = radius/25.0f;
             if(maxInlierDist<minInlierDist) maxInlierDist = minInlierDist;
             if(maxInlierDist>maxInlierDistMax) maxInlierDist = maxInlierDistMax;
             
             // choose samples along the circle and count inlier percentage
             for(float t =0; t<2*3.14159265359f; t+= 0.05f)
             {
                 counter++;
                 float cX = radius*cos(t) + center.x;
                 float cY = radius*sin(t) + center.y;
             
                 if(cX < dt.cols)
                 if(cX >= 0)
                 if(cY < dt.rows)
                 if(cY >= 0)
                 if(dt.at<float>(cY,cX) < maxInlierDist)
                 {
                    inlier++;
                    inlierSet.push_back(cv::Point2f(cX,cY));
                 }
             }
             
             return (float)inlier/float(counter);
            }
             
             
            inline void getCircle(cv::Point2f& p1,cv::Point2f& p2,cv::Point2f& p3, cv::Point2f& center, float& radius)
            {
              float x1 = p1.x;
              float x2 = p2.x;
              float x3 = p3.x;
             
              float y1 = p1.y;
              float y2 = p2.y;
              float y3 = p3.y;
             
              // PLEASE CHECK FOR TYPOS IN THE FORMULA :)
              center.x = (x1*x1+y1*y1)*(y2-y3) + (x2*x2+y2*y2)*(y3-y1) + (x3*x3+y3*y3)*(y1-y2);
              center.x /= ( 2*(x1*(y2-y3) - y1*(x2-x3) + x2*y3 - x3*y2) );
             
              center.y = (x1*x1 + y1*y1)*(x3-x2) + (x2*x2+y2*y2)*(x1-x3) + (x3*x3 + y3*y3)*(x2-x1);
              center.y /= ( 2*(x1*(y2-y3) - y1*(x2-x3) + x2*y3 - x3*y2) );
             
              radius = sqrt((center.x-x1)*(center.x-x1) + (center.y-y1)*(center.y-y1));
            }
             
             
             
            std::vector<cv::Point2f> getPointPositions(cv::Mat binaryImage)
            {
             std::vector<cv::Point2f> pointPositions;
             
             for(unsigned int y=0; y<binaryImage.rows; ++y)
             {
                 //unsigned char* rowPtr = binaryImage.ptr<unsigned char>(y);
                 for(unsigned int x=0; x<binaryImage.cols; ++x)
                 {
                     //if(rowPtr[x] > 0) pointPositions.push_back(cv::Point2i(x,y));
                     if(binaryImage.at<unsigned char>(y,x) > 0) pointPositions.push_back(cv::Point2f(x,y));
                 }
             }
             
             return pointPositions;
            }

             

            posted on 2017-10-17 13:39 zmj 閱讀(899) 評論(0)  編輯 收藏 引用

            欧美日韩精品久久久久| 99久久精品国产麻豆| 模特私拍国产精品久久| 国产一区二区三区久久| 一本色道久久88—综合亚洲精品 | 国产成人精品白浆久久69| 色欲综合久久躁天天躁| 国产精品亚洲美女久久久| 国产亚洲综合久久系列| 精品久久久久久国产牛牛app| 日本道色综合久久影院| 国产精品视频久久| 国产精品九九久久免费视频| 久久99精品久久久久婷婷| 欧美国产精品久久高清| 中文字幕亚洲综合久久菠萝蜜| 久久久精品无码专区不卡| 一本色道久久88加勒比—综合| 国产成人精品久久二区二区| 亚洲欧美久久久久9999 | 久久大香萑太香蕉av| AA级片免费看视频久久| 99久久免费国产精精品| 中文字幕热久久久久久久| 亚洲乱码日产精品a级毛片久久| 人人狠狠综合久久亚洲88| 国产成人精品久久免费动漫| 婷婷久久久亚洲欧洲日产国码AV| 久久丫精品国产亚洲av不卡| 日产精品久久久久久久| 久久久亚洲AV波多野结衣| 草草久久久无码国产专区| 粉嫩小泬无遮挡久久久久久| 精品国产乱码久久久久久1区2区| 伊人久久大香线蕉av不变影院| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 91精品国产91热久久久久福利| 丰满少妇高潮惨叫久久久| 国产一级持黄大片99久久| 99久久精品国产毛片| 日本国产精品久久|