2015-12-16 36 views
-3

我需要數條數(標註)在下面的照片: enter image description here計數感興趣區域的圖片

我有幾百張照片,我需要分析和我很好奇,如果有自動隔離感興趣區域併爲每張照片執行簡單計數的方法。我幾乎沒有圖像分析的經驗,任何建議讓我開始將不勝感激。

+0

據我所知,圖像識別是一個非常棘手的問題。並開始用SO來解決它,幫助你需要自己開始 - 發佈一些你已經完成的代碼,有些人會幫助你! –

+0

有人(據說你)問幾乎[ImageJ論壇上的同一個問題](http://forum.imagej.net/t/isolating-analysis-area/534)。將不同資源上的交叉帖子鏈接起來以允許其他人找到可能僅存在於其中一個地方的有價值信息是一種很好的做法。 –

+0

對不起,@JanEglinger,我從現在開始養成這種習慣。 – BillyBoy

回答

1

請運行下面的代碼,我已經爲你工作。它大致足夠接近並調整它。祝你好運..!

#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 
#include <iostream> 
#include "tchar.h" 
using namespace cv; 
using namespace std; 

#define INPUT_FILE    "u.jpg" 
#define OUTPUT_FOLDER_PATH  string("") 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    Mat large = imread(INPUT_FILE); 
    Mat rgb; 
    // downsample and use it for processing 
    pyrDown(large, rgb); 
    Mat small; 
    cvtColor(rgb, small, CV_BGR2GRAY); 
    // morphological gradient 
    Mat grad; 
    Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2)); 
    Mat morphKernel1 = getStructuringElement(MORPH_ELLIPSE, Size(1, 1)); 
    morphologyEx(small, grad, MORPH_GRADIENT, morphKernel); 
    // binarize 
    Mat bw; 
    threshold(grad, bw, 5.0, 50.0, THRESH_BINARY | THRESH_OTSU); 
    // connect horizontally oriented regions 
    Mat connected; 
    morphKernel = getStructuringElement(MORPH_RECT, Size(5, 1)); 
    morphologyEx(bw, connected, MORPH_CLOSE, morphKernel); 
    morphologyEx(bw, connected, MORPH_OPEN, morphKernel1); 
    // find contours 
    Mat mask = Mat::zeros(bw.size(), CV_8UC1); 
    vector<vector<Point>> contours; 
    vector<Vec4i> hierarchy; 
    findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 
    // filter contours 
    int y=0; 
    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0]) 
    { 
     Rect rect = boundingRect(contours[idx]); 
     Mat maskROI(mask, rect); 
     maskROI = Scalar(0, 0, 0); 
     // fill the contour 
     drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED); 

     double a=contourArea(contours[idx],false); 

      if(a> 75) 

     { 
      rectangle(rgb, rect, Scalar(0, 255, 0), 2); 
      y++; 
     } 
     imshow("Result1",rgb); 
    } 
    cout<<" The number of elements"<<y<< endl; 
    imshow("Result",mask); 
    imwrite(OUTPUT_FOLDER_PATH + string("rgb.jpg"), rgb); 
    waitKey(0); 
    return 0; 
} 

enter image description here

+0

謝謝你這麼棒的回覆@Arjun,這正是我想要做的。我現在將弄清楚如何對指定目錄中的所有文件重複此操作並計算感興趣的區域。歡呼 – BillyBoy

+0

我很高興它幫助你@BillyBoy好運..! – Arjun