ofxcv blob extracting + alpha

i needed code that uses ofxCv to extract a blob from a video.

  • create Mat that has black background and fills the blob’s contour with white
  • create Mat that copies all the pixels inside the blob’s boundingrect
  • create Mat that has black background and displays all RGB pixels that are inside the blob’s contours
  • create RGBA Mat, that uses the contour’s Mat as alpha
  • since opencv does not display alpha images, pass the pixels to an ofImage
  • since i am sending the blob over a network via zmq i combine the rgb and contour pixels in one large Mat
  
  
  
 foundBlobsAmt = contourFinderGreyThresh.size();  
  
    for (int i=0; i<foundBlobsAmt; i++)  
    {  
     
        cv::Rect boundRect = contourFinderGreyThresh.getBoundingRect(i);  
          
        //create new binary Mat that has the blob's contours as a white image  
        cv::Mat contourMat = Mat::zeros(boundRect.height,boundRect.width,CV_8UC3);  
        Scalar color(255,255,255);  
        cv::Point offset(-contourFinderGreyThresh.getBoundingRect(i).x,-contourFinderGreyThresh.getBoundingRect(i).y);  
        drawContours(contourMat, contourFinderGreyThresh.getContours(), i, color, CV_FILLED, 8, noArray(), 0, offset);  
        drawMat(contourMat, boundRect.width + 20, 0);  
          
          
        //create rgb Mat with the content of the blob's bounding rect  
        croppedMat = Mat(boundRect.height,boundRect.width,CV_8UC3);  
        Mat croppedRgbMat(rgbMat,boundRect);  
        resize(croppedRgbMat, croppedMat);  
        drawMat(croppedMat, 0, 0);  
          
        //create RGB Mat that only contains blob's RGB pixles  
        cv::Mat maskedMat = Mat::zeros(boundRect.height,boundRect.width,CV_8UC3);  
        croppedMat.copyTo(maskedMat,contourMat);  
        drawMat(maskedMat, 0, boundRect.height + 20);  
          
     
        //create RGBA Mat that has the contourMat as it's 4th channel (is not yet alpha)  
        cv::Mat maskedRgbaMat(boundRect.height,boundRect.width,CV_8UC4); //,Scalar(1,2,3,4));  
        Mat in[] = { maskedMat, contourMat };  
        // rgb[0] -> rgba[0], rgb[1] -> rgba[1], rgb[2] -> rgba[2], alpha[0] -> rgba[3]  
        int from_to[] = { 0,0, 1,1, 2,2,3,3 };  
        mixChannels( in, 2, &maskedRgbaMat, 1, from_to, 4 );  
        drawMat(maskedRgbaMat, boundRect.width + 20, boundRect.height + 20);  
  
        //create ofImage to make 4th channel the alpha; i.e. transparents  
        ofImage maskedRgbaImg;  
        ofxCv::toOf(maskedRgbaMat, maskedRgbaImg);  
        maskedRgbaImg.setImageType(OF_IMAGE_COLOR_ALPHA);  
          
        ofEnableAlphaBlending();  
        maskedRgbaImg.draw((boundRect.width + 20)*2, boundRect.height + 20);  
        ofDisableAlphaBlending();  
        
          
      //   blur(right_roi, right_roi, edgeBlur);  
          
        //create combine Mat that has rgb blob and contour blob info beside one an other  
        cv::Rect targetRect = cv::Rect(0,0,boundRect.width,boundRect.height);  
        cv::Rect targetRect2 = cv::Rect(boundRect.width,0,boundRect.width,boundRect.height);  
        Mat combine = Mat::zeros(boundRect.height,boundRect.width *2,CV_8UC3);  
        Mat left_roi(combine, targetRect);  
        maskedMat.copyTo(left_roi);  
        Mat right_roi(combine,targetRect2);  
        contourMat.copyTo(right_roi);  
        drawMat(combine,0, (boundRect.height+20)*2);  
          
    }  
  
[code/]  

2 Likes