2017-12-21 10 views
1

イメージ内のすべての円を識別しようとしています。コントラストを強調した後、しきい値を使用して、賢いエッジを使用して、私はすべての輪郭を見つけ出し、それらをループします。結果イメージには面積> 0があります。結果は良くありません。これはこれは輪郭領域に関連するすべての領域が表示されない

This is the result when showing all contours

すべての輪郭を示すとき、元の画像

This is the original image

はこれが結果です...

#include <opencv2/opencv.hpp> 

using namespace cv; 
using namespace std; 
void changeGain(cv::Mat&,double,int); 


int main(int argc, char** argv) 
{ 

// Load the original image and make a duplication 
cv::Mat rawImage = cv::imread("..\\3.png"); 
cv::Mat duplicateImage= rawImage.clone(); 

// Add contrast 
changeGain(duplicateImage,1.9 ,-240); 


// Apply thershold 
cv::threshold(duplicateImage, duplicateImage, 150, 200, cv::THRESH_BINARY); 


// Use canny edges 
cv::Mat img_canny; 
cv::Canny(duplicateImage,img_canny,150,200); 

// Find all the contours from the canny image 
vector<vector<cv::Point>> contours; 
findContours(img_canny, contours, RETR_TREE, CHAIN_APPROX_SIMPLE); 

// Approximate contours to polygons + get circles 
vector<vector<Point> > contours_poly; 

for(int i = 0; i < contours.size(); i++) 
{ 
    double area = contourArea(contours[i],true); 
    if (area > 0) 
     contours_poly.push_back(contours[i]); 
} 

// Draw the circles on the image 
drawContours(rawImage, contours_poly, -1, Scalar(rand() & 255, rand() & 255, rand() & 255)); 

// Show result 
cv::imshow("Final Result", rawImage); 
cv::waitKey(0); 
imwrite("..\\contour_result.jpg",rawImage); 

} 

void changeGain(cv::Mat& image,double alpha, int beta) 
{ 
//cv::Mat new_image = cv::Mat::zeros(image.size(), image.type()); 

/// Do the operation new_image(i,j) = alpha*image(i,j) + beta 
for(int y = 0; y < image.rows; y++) 
{ 
    for(int x = 0; x < image.cols; x++) 
    { 
     for(int c = 0; c < 3; c++) 
      image.at<cv::Vec3b>(y,x)[c] = 
        cv::saturate_cast<uchar>(alpha*(image.at<cv::Vec3b>(y,x)[c]) + beta); 

    } 
} 

return; 

}

を支援してください結果whエリア> 0

This is the result when showing only contours with area >0

でのみ輪郭画像を示すエンthershold

An image after thershold

Image after canny

+0

スレッショルド設定された画像への参照を保存できますか?これはデバッグに最も役立ちます –

+0

オリジナルの投稿にthersholdの後に画像を追加しました。 @PatrickRoberts –

+0

スレッショルドを見ると、エッジが出ているブロブが輪郭を完全に閉じることができないように見えます。あなたはあれが見えますか? –

答えて

0

まず後、しきい値処理、その後クリーンアップするために、形態(https://docs.opencv.org/trunk/d9/d61/tutorial_py_morphological_ops.html)を用いて検討画像。開閉してノイズを取り除き、閾値処理された画像の隙間を埋めるようなものを使用します。

第2に、Cannyのエッジを検出する必要はありません。私は現在のプロジェクトでやっていることです。

第3に、円を探しているとします。この場合、minEnclosingCirclehttps://docs.opencv.org/3.3.1/d3/dc0/group__imgproc__shape.html#ga8ce13c24081bbc7151e9326f412190f1)を使用できます。形は円であるため円だけを囲む必要があります。輪郭のcontourAreaと半径がminEnclosingCircleである円の面積を比較することで決定できます。あなたの輪郭が確かに円の場合、この比率は1に近くなるはずです(これは私が現在使っている技術です)。

+0

ありがとう、オスカー。私はそれがどうなるか試してみる –

関連する問題