2015-12-10 154 views
5

私は形状を検出し、画像内の各形状の出現を数える必要があります。最初に輪郭を検出し、それらを近似し、それぞれの輪郭の頂点を数えました。このような:opencv-pythonで星形を検出

import cv2 
import numpy as np 
import collections 
import sys 

img = cv2.imread(str(sys.argv[1]),0) 
ret,thresh = cv2.threshold(img,127,255,0) 
contours,hierarchy = cv2.findContours(thresh,1,2) 


no_of_vertices = [] 

i = 0 
mask = np.zeros(img.shape,np.uint8) 
for contour in contours: 

cnt = contour 
area = cv2.contourArea(cnt) 
if area>150: 
    epsilon = 0.02*cv2.arcLength(cnt,True) 
    approx = cv2.approxPolyDP(cnt,epsilon,True) 
    no_of_vertices.append(len(approx)) 



counter = collections.Counter(no_of_vertices) 




a,b = counter.keys(),counter.values() 

i=0 
while i<len(counter): 
    print a[i],b[i] 
    i = i + 1 

このイメージに星を検出するための私のコードのdoesntの仕事:

Image with stars and other basic shapes

私はコード内でどのような変更を加える必要がありますか?

+2

(密度のあるまたは疎な)輪郭から、try matc hShape関数:http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#double%20matchShapes%28InputArray%20contour1,%20InputArray%20Contour2,%20int%20method,%20double%20parameter%29 – Micka

+1

_circularity_を使用して、形状を検出することができます: '(4 * pi * area)/(perimeter^2)'。スターの形状は円形度が約0.25です。例えば、 – Miki

答えて

3

私の仕事は、形状の周囲の面積の平方根を比較したことです。星の約0.145(+/- 0.0015のエッジの一部が完全に出ないため)。六角形の場合は0.255、三角形の場合は.21、四角形の場合は.247、五角形の場合は.250です。

円形もあります(三角形は0.26から.27になります)。同様に、六角形の場合は.83、三角形の場合は.55-56、四角形の場合は.77、四角形の場合は.77です。以下は、五角形のための78)

は(私はここに私のPC上でのpythonを持っていないが、考え方は同じです)、それのためのC++のコードです:

#include "stdafx.h" 
#include <opencv/cxcore.h> 
#include <opencv2\core\mat.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <iostream> 
#include <opencv/cxcore.h> 
#include <opencv/highgui.h> 
#include <opencv/cv.h> 
#include <opencv2/opencv.hpp> 
#include <opencv2/core/core.hpp> 

using namespace cv; 
using namespace std; 

RNG rngee(12345); 

int main() { 
    Mat im = imread("C:/this/is.a/path/image.png", CV_LOAD_IMAGE_COLOR); 
    Mat imgrey = im.clone(); 
    cvtColor(im, imgrey, CV_RGB2GRAY); 
    vector<vector<Point> > imContours; 
    vector<Vec4i> hierarchy; 

    double divMaxSize = 0.175, divMinSize = 0.125; 

    namedWindow("Image", CV_WINDOW_NORMAL| CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED); 

    threshold(imgrey, imgrey, 100, 255, 0); 

    findContours(imgrey, imContours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    for (int i=0; i < imContours.size(); i++) { 
     Scalar color = Scalar(rngee.uniform(0, 255), rngee.uniform(0,255), rngee.uniform(0,255)); 
     cout << "sqrt(Area)/arcLength = " << sqrt(contourArea(imContours[i]))/arcLength(imContours[i], true) << endl; 
     if(sqrt(contourArea(imContours[i]))/arcLength(imContours[i], true) < divMaxSize && sqrt(contourArea(imContours[i]))/arcLength(imContours[i], true) > divMinSize) 
     { 
      drawContours(im, imContours, i, color, 2, 8, hierarchy, 0, Point()); 
      cout << "I'm a star!" << endl; 
     } 
     imshow("Image", im); 
     waitKey(0); 
    } 
    imshow("Image", im); 
    waitKey(0); 

} 

どちらの方法 - 真円度を使用したり、私のいずれかsqrt(area)/ arclengthメソッド - 結果:image with stars highlighted

+1

ありがとう!それは助けてくれた:) –

関連する問題