Beispiel #1
0
 /** get the segmentation using active contour with initial mask. Note that the initalization
   has to be done before calling this function (for an unknown reason)
   * @param aSrc         the source image
   * @param aInitMask    the initial mask for the segementation
   * @param aMaxIts      maximum number of iteration
   * @param aDisp        if intermediate results is shown (1-0)
   */
 cv::Mat GTVideo::segmentByActiveContour(const cv::Mat& aSrc, const cv::Mat& aInitMask, int aMaxIts, bool aDisp)
 {
     if (NULL == aSrc.data)
     {
         std::cerr << "Could not load the image."
                   <<  std::endl;
         return cv::Mat();
     }

     const int rows = aSrc.rows;
     const int cols = aSrc.cols;

     // set input parameters to the shared object libseg.so
     // source image (Note: matlab is column-major while C++ is row-major
     cv::Mat trImage;
     cv::transpose(aSrc, trImage);
     mwArray I(rows, cols, mxUINT8_CLASS);
     I.SetData(trImage.data, rows*cols);

     // maximum iteration number
     int maxits[1] = {aMaxIts};
     mwArray max_its(1,1,  mxINT32_CLASS);//mxUINT8_CLASS);
     max_its.SetData(maxits, 1);

     // initial mask of segmentation
     cv::Mat initmask = aInitMask.clone();
     cv::transpose(initmask, initmask);
     mwArray init_mask(rows, cols, mxUINT8_CLASS);
     init_mask.SetData(initmask.data, rows*cols);

     // parameter alpha to the active contour method
     double alph[1] = {0.2};
     mwArray alpha(1,1, mxDOUBLE_CLASS);
     alpha.SetData(alph, 1);

     // display intermediate results or not
     uchar disp[1] = {aDisp?1:0};
     mwArray display(1,1, mxUINT8_CLASS);
     display.SetData(disp, 1);

     // return value container
     mwArray imOutput(rows, cols, mxUINT8_CLASS);
     region_seg(1, imOutput, I, init_mask, max_its, alpha, display);

     // retrieve result to cv::Mat format
     uchar result[rows*cols];
     imOutput.GetData(result, rows*cols);
     cv::Mat retImage(rows, cols, CV_8UC1);
     for (int j=0; j<cols; j++)
     {
         for (int i=0; i<rows; i++)
         {
             retImage.at<uchar>(i,j) = result[j*rows+i];
         }
     }

     return retImage;
 }
Beispiel #2
0
int main(int argc, char** argv)
{
    IplImage *img = 0, *mask=0,*mask2=0, *seg=0;

    if(argc < 3)
    {
        fprintf(stderr,"Usage: ma‭in <Image> <Mask>");
    }


    img = cvLoadImage( argv[1] );

    cvNamedWindow( "segmented", CV_WINDOW_NORMAL );
    cvMoveWindow( "segmented", 100, 100);
    cvShowImage( "segmented", img);
    cvWaitKey(0);

    mask = cvLoadImage( argv[2] );

    cvShowImage( "segmented", mask);
    cvWaitKey(0);

    mask2 = cvCreateImage(cvGetSize(mask),img->depth,CV_8UC1);
    cvCvtColor(mask, mask2, CV_RGB2GRAY);
    cvReleaseImage( &mask);



    cvShowImage( "segmented", mask2);
    cvWaitKey(0);

    fprintf(stderr, "hM:%d, wM:%d, dM:%d, hI:%d, wI:%d", mask2->height, mask2->width, mask2->depth, img->height,img->width );

    seg = region_seg( img, mask2, iterations, alpha, 1 );

    cvSaveImage( "Segmented.tiff", seg );

    cvNamedWindow( "segmented", CV_WINDOW_NORMAL );
    cvMoveWindow( "segmented", 100, 100);
    cvShowImage( "segmented", seg);
    cvWaitKey(0);


    return 0;
}