2013-06-07 75 views
10

Điều tôi định làm là như sau:theo dõi đối tượng màu trong openCV giúp phát hiện da

Sửa một đối tượng màu. Theo dõi nó trên các khung hình video bằng cách sử dụng phương pháp tiếp cận biểu đồ-backprojection với camshift. Tôi sử dụng mã sau và nó luôn luôn kết thúc phát hiện da. Tôi hiểu rằng tôi đang mắc phải một số sai lầm khủng khiếp đơn giản. Sẽ rất hữu ích nếu ai đó có thể chỉ ra.

//I have included only the integral parts of code. There are no compilation errors. 

    int lowerH =80, upperH =100, lowerS =80, upperS =255, lowerV =80, upperV =255; 

    CvScalar output_min =cvScalar(lowerH, lowerS, lowerV, 0); //Color Track 
    CvScalar output_max =cvScalar(upperH, upperS, upperV, 0); 

    CvScalar output_min2 =cvScalar(0, lowerS, lowerV, 0); //Color Track 
    CvScalar output_max2 =cvScalar(180, upperS, upperV, 0); 

    while(true){ 
     frame =cvQueryFrame(capture); 

     cvCvtColor(frame, output, CV_BGR2HSV); 
     cvInRangeS(output, output_min, output_max, output_mask); 

     blobs =CBlobResult(output_mask, NULL, 0); 
     blobs.Filter(blobs, B_EXCLUDE, CBlobGetArea(), B_LESS, 35); 

     int num_blobs =blobs.GetNumBlobs(); 
     for(int i=0; i<num_blobs;++i){ 
      currentBlob = blobs.GetBlob(i);     
      sortedBlobs.push_back(currentBlob); 
     } 

     if(num_blobs){ 
      sort(sortedBlobs.begin(), sortedBlobs.end(), local::sortBlobs); 
      CvRect blobRect =sortedBlobs[0].GetBoundingBox(); 

      initX =blobRect.x; 
      initY =blobRect.y; 
      initWidth =blobRect.width; 
      initHeight =blobRect.height; 
      initFrame =cvCloneImage(frame); 
     } 

      int c=cvWaitKey(40); 
     if((char)c ==27)break; 
    } 

    CvRect selection; 
    selection.x = initX; 
    selection.y = initY; 
    selection.width = initWidth; 
    selection.height = initHeight; 

    CvHistogram *hist; 
    int hist_bins = 30;   
    float hist_range[] = {0, 180}; 
    float* range = hist_range; 
    hist = cvCreateHist(1, &hist_bins, CV_HIST_ARRAY, &range, 1);   

    cvCvtColor(initFrame, output, CV_BGR2HSV); 
    cvInRangeS(output, output_min2, output_max2, output_mask); 
    cvSplit(output, hue, 0, 0, 0); 

    cvSetImageROI(hue, selection); 
    cvSetImageROI(output_mask, selection); 

    cvCalcHist(&hue, hist, 0, output_mask); 
    float max_val = 0.f; 
    cvGetMinMaxHistValue(hist, 0, &max_val, 0, 0); 
    cvConvertScale(hist->bins, hist->bins, 
       max_val ? 255.0/max_val : 0, 0); 

    cvResetImageROI(hue); 
    cvResetImageROI(output_mask); 


    CvBox2D curr_box; 
    CvRect prev_rect =selection; 
    CvConnectedComp components; 
    bool rectFlag =false; 
    CvPoint Pt =cvPoint(0,0), prevPt =cvPoint(0,0); 
    int clearCounter =0; 
    while(true){ 
     frame =cvQueryFrame(capture); 
     if(!frame)break; 

     cvCvtColor(frame, output, CV_BGR2HSV); 
     cvInRangeS(output, output_min2, output_max2, output_mask); 
     cvSplit(output, hue, 0, 0, 0); 

     cvCalcBackProject(&hue, prob, hist); 
     cvAnd(prob, output_mask, prob, 0); 

     cvCamShift(prob, prev_rect, cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 20, 1), &components, &curr_box); 

     prev_rect = components.rect; 
     curr_box.angle = -curr_box.angle; 

     cvEllipseBox(frame, curr_box, CV_RGB(255,0,0), 3, CV_AA, 0); 

     int c=cvWaitKey(40); 
     if((char)c ==27)break; 
    } 

EDIT:

Đừng đánh dấu vào phần sửa mã trong đó khi tạo biểu đồ tôi đang sử dụng giá trị màu sắc cho mặt nạ trong khoảng 0-180. Nếu tôi sử dụng phạm vi giảm 80-100, tôi nhận được các giá trị rác trong các thành phần và curr_box.

UPDATE: Output hình ảnh

Đây là blob xanh được phát hiện ban đầu và cần được theo dõi suốt buổi ghi hình.

This is the green blob detected initially and should be tracked throughout the recording

Đây là những gì sẽ xảy ra. Các đốm màu xanh lá cây là hoàn toàn bôi đen trong hình ảnh mặt nạ và thay vào đó là da được theo dõi.

This is what happens. The green blob is completely blacked out in the mask image and instead the skin is tracked

+0

Bạn không sửa màu để theo dõi màu da? Nó có vẻ như thế. – Nallath

+0

Bạn có thể cung cấp một số mẫu không? Ví dụ: video đầu vào của bạn và kết quả theo dõi. – cyriel

+0

@Nallath: Tôi cho rằng màu da có giá trị H trong khoảng 0-20 (đúng nếu tôi sai). –

Trả lời

6

Trước hết, xin lỗi cho tất cả các sự nhầm lẫn. Có một lỗi rất ngớ ngẩn trong mã. Tôi nhân bản khung hình ban đầu sau khi đặt một hình chữ nhật màu đỏ đầy lên nó.

CvPoint pt1, pt2; 
    pt1.x = blobRect.x; 
    pt1.y = blobRect.y; 
    pt2.x = blobRect.x + blobRect.width; 
    pt2.y = blobRect.y + blobRect.height; 

    cvRectangle(frame, pt1, pt2, cvScalar(0, 0, 255, 0), CV_FILLED, 8, 0); 

    initX =blobRect.x; 
    initY =blobRect.y; 
    initWidth =blobRect.width; 
    initHeight =blobRect.height; 
    initFrame =cvCloneImage(frame); 

Do đó, biểu đồ được tạo luôn luôn là màu đỏ. Giải pháp là tầm thường.

Các vấn đề liên quan