복붙노트

[PYTHON] OpenCV 도트 타겟 탐지가 모든 타겟을 찾지 못하고, 발견 된 원이 상쇄됩니다.

PYTHON

OpenCV 도트 타겟 탐지가 모든 타겟을 찾지 못하고, 발견 된 원이 상쇄됩니다.

나는이 그림에서와 같이 흑점 / 백색 점 대상의 중심을 탐지하려고 노력하고있다. 나는 cv2.HoughCircles 메서드를 사용하려고 시도했지만 1, 2 ~ 3 개의 대상 만 탐지 할 수 있으며, 2, 찾은 원을 이미지 위에 다시 그릴 때 항상 약간 오프셋됩니다.

잘못된 방법을 사용하고 있습니까? findContours 또는 완전히 다른 것을 사용해야합니까?

여기 내 코드가 있습니다 :

import cv2
from cv2 import cv
import os
import numpy as np

def showme(pic):
    cv2.imshow('window',pic)
    cv2.waitKey()
    cv2.destroyAllWindows()


im=cv2.imread('small_test.jpg')

gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

#I've tried blur,bw,tr...  all give me poor results.

blur = cv2.GaussianBlur(gray,(3,3),0)
n,bw = cv2.threshold(blur,120,255,cv2.THRESH_BINARY)
tr=cv2.adaptiveThreshold(blur,255,0,1,11,2)

circles = cv2.HoughCircles(gray, cv.CV_HOUGH_GRADIENT, 3, 100, None, 200, 100, 5, 16)

try:
    n = np.shape(circles)
    circles=np.reshape(circles,(n[1],n[2]))
    print circles
    for circle in circles:
        cv2.circle(im,(circle[0],circle[1]),circle[2],(0,0,255))
    showme(im)
except:
    print "no cicles found"

그리고 이것은 나의 현재 결과입니다 :

해결법

  1. ==============================

    1.다른 게시물에서 작성한 코드를 실행하면 약간 더 나은 결과를 얻을 수있었습니다.

    다른 게시물에서 작성한 코드를 실행하면 약간 더 나은 결과를 얻을 수있었습니다.

    그것은 모든 매개 변수에 관한 것입니다. 항상 그렇습니다.

    cvSmooth (), cvCanny () 및 cvHoughCircles ()와 같이 실험해야하는이 프로그램에서 호출되는 세 가지 중요한 기능이 있습니다. 각각은 결과를 대폭 변경할 가능성이 있습니다.

    다음은 C 코드입니다.

    IplImage* img = NULL;
    if ((img = cvLoadImage(argv[1]))== 0)
    {
        printf("cvLoadImage failed\n");
    }
    
    IplImage* gray = cvCreateImage(cvGetSize(img), IPL_DEPTH_8U, 1);
    CvMemStorage* storage = cvCreateMemStorage(0);
    
    cvCvtColor(img, gray, CV_BGR2GRAY);
    
    // This is done so as to prevent a lot of false circles from being detected
    cvSmooth(gray, gray, CV_GAUSSIAN, 7, 9);
    
    IplImage* canny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
    IplImage* rgbcanny = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3);
    cvCanny(gray, canny, 40, 240, 3);
    
    CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 2, gray->height/8, 120, 10, 2, 25);
    cvCvtColor(canny, rgbcanny, CV_GRAY2BGR);
    
    for (size_t i = 0; i < circles->total; i++)
    {
         // round the floats to an int
         float* p = (float*)cvGetSeqElem(circles, i);
         cv::Point center(cvRound(p[0]), cvRound(p[1]));
         int radius = cvRound(p[2]);
    
         // draw the circle center
         cvCircle(rgbcanny, center, 3, CV_RGB(0,255,0), -1, 8, 0 );
    
         // draw the circle outline
         cvCircle(rgbcanny, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );
    
         printf("x: %d y: %d r: %d\n",center.x,center.y, radius);
    }
    
    cvNamedWindow("circles", 1);
    cvShowImage("circles", rgbcanny);
    
    cvSaveImage("out.png", rgbcanny);
    cvWaitKey(0);
    

    이 파일을 파이썬에 포팅하는 기술이 있다고 믿습니다.

  2. ==============================

    2.그 원형 패턴이 고정되어 있고 객체와 잘 구분되기 때문에, 간단한 템플릿 매칭은 cvMatchTemplate을 체크하면 합리적으로 잘 작동합니다. 보다 복잡한 조건 (객체 모양 또는 뷰 형상으로 인한 뒤틀림)을 위해 SIFT 또는 SURF (cvExtractSURF)와 같은 강력한 기능을 시도 할 수 있습니다.

    그 원형 패턴이 고정되어 있고 객체와 잘 구분되기 때문에, 간단한 템플릿 매칭은 cvMatchTemplate을 체크하면 합리적으로 잘 작동합니다. 보다 복잡한 조건 (객체 모양 또는 뷰 형상으로 인한 뒤틀림)을 위해 SIFT 또는 SURF (cvExtractSURF)와 같은 강력한 기능을 시도 할 수 있습니다.

  3. ==============================

    3.대부분의 파이썬 코드를 사용하여 서클 감지

    대부분의 파이썬 코드를 사용하여 서클 감지

    import cv2
    import numpy as np
    
    img = cv2.imread('coin.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(7,9),6)
    cimg = cv2.cvtColor(blur,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(blur,cv2.HOUGH_GRADIENT,1,50,
                                param1=120,param2=10,minRadius=2,maxRadius=30)
    
    
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    
    cv2.imshow('detected circles',cimg)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
  4. from https://stackoverflow.com/questions/10404062/opencv-dot-target-detection-not-finding-all-targets-and-found-circles-are-offse by cc-by-sa and MIT license