2016-01-09 64 views
1

我目前正在嘗試使用OpenCV模板匹配函數來檢測給定圖像中使用模板中的所有相似對象。但是,即使它們非常相似和相同,我也沒有得到所有的物體(即血細胞)。我一直在互聯網上尋找解決方案,但沒有得到任何答案。OpenCV模板匹配類似的對象

以下是我的代碼:

cv::Mat ref = cv::imread("c:\\image.jpg"); 
cv::Mat tpl = cv::imread("c:\\template.jpg"); 

cv::Mat gref, gtpl; 
cv::cvtColor(ref, gref, CV_BGR2GRAY); 
cv::cvtColor(tpl, gtpl, CV_BGR2GRAY); 

cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1); 
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED); 
cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO); 

while (true) 
{ 
    double minval, maxval, threshold = 0.8; 
    cv::Point minloc, maxloc; 
    cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc); 

    if (maxval >= threshold) 
    { 
     cv::rectangle(
      ref, 
      maxloc, 
      cv::Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows), 
      CV_RGB(0,255,0), 2 
     ); 
     cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.)); 
    } 
    else 
     break; 
} 

cv::imshow("reference", ref); 

這些是用於結果和圖像:

給定圖像

Given Image

模板

Template

結果與較高的閾值設置(0.8/0.8)

Result

結果具有較低的閾值設置(0.6/0.3)

enter image description here

我非常新的模板匹配,是有沒有辦法讓圖像中的所有對象都被檢測到?

我需要模板匹配來檢測一些更復雜的圖像中的單元格。

enter image description here

+1

更改閾值? – nbsrujan

+0

我試圖降低閾值,但仍然沒有檢測到所有對象 – Woody

+0

您可以上傳res圖像 – nbsrujan

回答

2

在特定情況下,你不需要使用模板匹配。您可以僅使用紅色組件來檢測斑點。如果您使用OpenCV 3.0+,則可以使用cv::SimpleBlobDetector

無論如何,你可以使用cv::thresholdcv::findContours來實現簡單的檢測器。我嘗試下面的代碼:

int main() 
{ 
    const int threshVal = 30; 
    const int minArea = 15 * 15; 
    const int maxArea = 100 * 100; 
    cv::Mat img = cv::imread("input.jpg"); 

    cv::Mat bgr[3]; 
    cv::split(img, bgr); 

    cv::Mat red_img = bgr[2]; 
    cv::threshold(red_img, red_img, threshVal, 255, cv::THRESH_BINARY); 

    vector<vector<Point>> contours; 
    vector<Vec4i> hierarchy; 
    cv::findContours(red_img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 

    for (int i = 0; i < contours.size(); i++) 
    { 
    int area = cv::contourArea(contours[i]); 
    if (area < minArea || area > maxArea) 
     continue; 

    cv::Rect roi = cv::boundingRect(contours[i]); 
    cv::rectangle(img, roi, cv::Scalar(0, 255, 0), 2); 
    } 

    cv::imshow("result", img); 
    cv::waitKey(0); 
    return 0; 
} 

而且這個代碼檢測所有的血細胞:

enter image description here

當然,你可能需要調整的三個常量的值(threshValminAreamaxArea),以獲得所有樣品都有更好的結果。

+0

對於延遲抱歉,我以前使用過這種方法,但在這種圖像[鏈接](http://i.imgur.com/D0cHxkV.jpg)中效果不佳。大多數打包在一起的單元格僅計爲1個單元格 – Woody

+0

@Woody由於確切原因,您有minArea和maxArea參數。 –