void ofxCvWatershed::segment() { reset(); int nContours = cvFindContours( iplMarkersTempImg, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); int i, j, compCount = 0; cvZero( iplMarkers32sImg ); for( ; contours != 0; contours = contours->h_next, compCount++ ) { cvDrawContours( iplMarkers32sImg, contours, cvScalarAll(compCount+1), cvScalarAll(compCount+1), -1, -1, 8, cvPoint(0,0) ); } CvRNG rng = cvRNG(-1); colors = cvCreateMat( 1, compCount, CV_8UC3 ); for( i = 0; i < compCount; i++ ) { uchar* ptr = colors->data.ptr + i*3; // no colors for now. ptr[0] = (uchar)0;//(cvRandInt(&rng)%180 + 50); ptr[1] = (uchar)0;//(cvRandInt(&rng)%180 + 50); ptr[2] = (uchar)0;//(cvRandInt(&rng)%180 + 50); } cvWatershed( iplTargetImg, iplMarkers32sImg ); // paint the watershed image for( i = 0; i < iplMarkers32sImg->height; i++ ) { for( j = 0; j < iplMarkers32sImg->width; j++ ) { int idx = CV_IMAGE_ELEM( iplMarkers32sImg, int, i, j ); uchar* dst = &CV_IMAGE_ELEM( iplTargetImg, uchar, i, j*3 ); if( idx == -1 ) { dst[0] = dst[1] = dst[2] = (uchar)255; } else if( idx <= 0 || idx > compCount ){ dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here }else { uchar* ptr = colors->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } } //cvAddWeighted( watershed, 0.5, colorImg.getCvImage(), 0.5, 0, watershed ); watershedImg = iplTargetImg; watershedGrayImg = watershedImg; watershedGrayImg.threshold(140); //watershedGrayImg.invert(); printf("contorus %i", contourFinder.findContours( watershedGrayImg, 10, (watershedImg.width * watershedImg.height)/ 2.f, 20, true)); }
void cv::watershed( const Mat& src, Mat& markers ) { CvMat _src = src, _markers = markers; cvWatershed( &_src, &_markers ); }
void BreakingPointsScreen::doWatershedAndLayers(BreakingPointsImage* theBreakingPointsImage) { cvZero( theBreakingPointsImage->marker_mask ); //zero out the mask to start with int imageWidth = theBreakingPointsImage->theWatershed.width; int imageHeight = theBreakingPointsImage->theWatershed.height; ofxCvGrayscaleImage cvGrayWater; cvGrayWater.allocate(imageWidth, imageHeight); cvGrayWater.setFromPixels(theBreakingPointsImage->theWatershed.getPixels(), imageWidth, imageHeight); cvCopy(cvGrayWater.getCvImage(), theBreakingPointsImage->marker_mask); CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contours = 0; CvMat* color_tab; int i, j, comp_count = 0; cvZero( theBreakingPointsImage->markers ); cvZero( theBreakingPointsImage->wshed ); cvFindContours( theBreakingPointsImage->marker_mask, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); for( ; contours != 0; contours = contours->h_next, comp_count++ ) { cvDrawContours( theBreakingPointsImage->markers, contours, cvScalarAll(comp_count+1), cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) ); } if(comp_count == 0) { cout << "Can't do watershed with no contours! " << endl; return; } color_tab = cvCreateMat( 1, comp_count, CV_8UC3 ); for( i = 0; i < comp_count; i++ ) { uchar* ptr = color_tab->data.ptr + i*3; ptr[0] = (uchar)(cvRandInt(&theBreakingPointsImage->rng)%180 + 50); ptr[1] = (uchar)(cvRandInt(&theBreakingPointsImage->rng)%180 + 50); ptr[2] = (uchar)(cvRandInt(&theBreakingPointsImage->rng)%180 + 50); } // double t = (double)cvGetTickCount(); ofxCvColorImage cvTempImage; cvTempImage.allocate(imageWidth, imageHeight); cvWatershed( cvTempImage.getCvImage(), theBreakingPointsImage->markers ); // t = (double)cvGetTickCount() - t; // printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) ); // paint the watershed image for( i = 0; i < theBreakingPointsImage->markers->height; i++ ) { for( j = 0; j < theBreakingPointsImage->markers->width; j++ ) { int idx = CV_IMAGE_ELEM( theBreakingPointsImage->markers, int, i, j ); uchar* dst = &CV_IMAGE_ELEM( theBreakingPointsImage->wshed, uchar, i, j*3 ); if( idx == -1 ) dst[0] = dst[1] = dst[2] = (uchar)255; else if( idx <= 0 || idx > comp_count ) dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here else { uchar* ptr = color_tab->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } } cvReleaseMemStorage( &storage ); cvReleaseMat( &color_tab ); ofxCvColorImage tempToDrawWith; tempToDrawWith.allocate(imageWidth, imageHeight); ofxCvGrayscaleImage tempToDrawWithGrey; tempToDrawWithGrey.allocate(imageWidth, imageHeight); cvCopy(theBreakingPointsImage->wshed, tempToDrawWith.getCvImage() ); tempToDrawWith.flagImageChanged(); tempToDrawWithGrey = tempToDrawWith;//converting automatically i hope tempToDrawWithGrey.contrastStretch(); //as much contrast as we can get tempToDrawWithGrey.dilate(); //stretch out the white borders tempToDrawWithGrey.invert(); //make them black tempToDrawWithGrey.threshold(1); //make all the grey white theBreakingPointsImage->contourFinder.findContours(tempToDrawWithGrey, 20, 0.9f*(float)(imageWidth * imageHeight), 10, true, true); int numberOfBlobsFound = theBreakingPointsImage->contourFinder.blobs.size(); //cout << contourFinder.blobs.size() << " was the number of blobs" << endl; if(numberOfBlobsFound > 0) { theBreakingPointsImage->layers.clear(); theBreakingPointsImage->layerMasks.clear(); theBreakingPointsImage->layerFades.clear(); theBreakingPointsImage->fadeSpeeds.clear(); theBreakingPointsImage->layers.resize(numberOfBlobsFound); theBreakingPointsImage->layerMasks.resize(numberOfBlobsFound); theBreakingPointsImage->layerFades.resize(numberOfBlobsFound); theBreakingPointsImage->fadeSpeeds.resize(numberOfBlobsFound); for(int i=0; i< numberOfBlobsFound; i++) { theBreakingPointsImage->layers[i].allocate(imageWidth, imageHeight,OF_IMAGE_COLOR_ALPHA); theBreakingPointsImage->layerMasks[i].allocate(imageWidth, imageHeight); theBreakingPointsImage->layerMasks[i].drawBlobIntoMe(theBreakingPointsImage->contourFinder.blobs[i], 255); theBreakingPointsImage->layerMasks[i].flagImageChanged(); unsigned char * pixelsSrc = theBreakingPointsImage->theImage.getPixels(); unsigned char * pixelsMask = theBreakingPointsImage->layerMasks[i].getPixels(); unsigned char * pixelsFinal = new unsigned char[imageWidth*imageHeight*4]; //RGBA so *4 memset(pixelsFinal,0,imageWidth*imageHeight*4); for( int j = 0; j < imageWidth*imageHeight; j++) { if( pixelsMask[j*3] == 255 ) //i.e. if the mask is white at this point { pixelsFinal[j*4] = pixelsSrc[ j*3 ]; pixelsFinal[j*4+1] = pixelsSrc[ j*3+1 ]; pixelsFinal[j*4+2] = pixelsSrc[ j*3+2 ]; pixelsFinal[j*4+3] = 255; } } theBreakingPointsImage->layers[i].setFromPixels(pixelsFinal, imageWidth, imageHeight, OF_IMAGE_COLOR_ALPHA); delete[] pixelsFinal; theBreakingPointsImage->layerFades[i] = 0; //start faded out, nahhhh random, nahh zero theBreakingPointsImage->fadeSpeeds[i] = ofRandomuf(); //ofRandomuf(); //random 0..1 fade speed } } theBreakingPointsImage->watershedDone = true; if(ofRandomuf() > 0.5f) { theBreakingPointsImage->isStrobe = true; } else { theBreakingPointsImage->isStrobe = false; } }
void MyLable::set_scaled_button() { int i, j, comp_count = 0; CvMat* color_tab = 0; CvSeq* contours = 0; cvSave("markes.xml",marker_mask); cvClearMemStorage(storage); cvFindContours( marker_mask, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); cvZero( markes ); for( ; contours != 0; contours = contours->h_next, comp_count++ ) { //cvDrawContours(markers, contours, cvScalarAll(comp_count+1), //cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) ); cvDrawContours(markes, contours, cvScalarAll(comp_count+1), cvScalarAll(comp_count+1), 1, -1, 8, cvPoint(0,0) ); } color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );//创建随机颜色列表 for( i = 0; i < comp_count; i++ ) //不同的整数标记 { uchar* ptr = color_tab->data.ptr + i*3; ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50); } double t = (double)cvGetTickCount(); cvSave("img0.xml",markes); cvWatershed( img0, markes ); cvSave("img1.xml",markes); // cvShowImage("imgo",img0); t = (double)cvGetTickCount() - t; printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) ); /*********/ for( i = 0; i < markes->height; i++ ) for( j = 0; j < markes->width; j++ ) { int idx = CV_IMAGE_ELEM( markes, int, i, j );//markers的数据类型为IPL_DEPTH_32S uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );//BGR三个通道的数是一起的,故要j*3 uchar* t_body = &CV_IMAGE_ELEM( tongue_body, uchar, i, j*3 ); uchar* src_pic = &CV_IMAGE_ELEM( img0, uchar, i, j*3 ); if( idx == -1 ) //输出时若为-1,表示各个部分的边界 { //dst[0] = dst[1] = dst[2] = (uchar)128; dst[2] = (uchar)255; } else if( idx <= 0 || idx > comp_count ) //异常情况 { //dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here } else if( idx == 1 ) //异常情况 { //green first dst[0] = (uchar)255; t_body[0] = src_pic[0]; t_body[1] = src_pic[1]; t_body[2] = src_pic[2]; } else if( idx == 2 ) //异常情况 { //blue second dst[1] = (uchar)255; } else if( idx == 3 ) //异常情况 { //red third dst[2] = (uchar)255; } else //正常情况 { uchar* ptr = color_tab->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } cvShowImage("img_gray",img_gray); cvShowImage("wshed",wshed); cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed ); //wshed.x.y=0.5*wshed.x.y+0.5*img_gray+0加权融合图像 //cvShowImage("swhed",wshed); //cvShowImage("img_gray",img_gray); //cvShowImage( "watershed transform", wshed ); //cvShowImage("img_final",img_final); cvShowImage( "watershed transform", wshed ); img = Function::CjwIplToQImg(tongue_body); image = *img; cvReleaseMat( &color_tab ); update(); clear_list(); }
int main( int argc, char** argv ) { char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg"; CvRNG rng = cvRNG(-1); if( (img0 = cvLoadImage(filename,1)) == 0 ) return 0; printf( "Hot keys: \n" "\tESC - quit the program\n" "\tr - restore the original image\n" "\tw or SPACE - run watershed algorithm\n" "\t\t(before running it, roughly mark the areas on the image)\n" "\t (before that, roughly outline several markers on the image)\n" ); cvNamedWindow( "image", 1 ); cvNamedWindow( "watershed transform", 1 ); img = cvCloneImage( img0 ); img_gray = cvCloneImage( img0 ); wshed = cvCloneImage( img0 ); marker_mask = cvCreateImage( cvGetSize(img), 8, 1 ); markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 ); cvCvtColor( img, marker_mask, CV_BGR2GRAY ); cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR ); cvZero( marker_mask ); cvZero( wshed ); cvShowImage( "image", img ); cvShowImage( "watershed transform", wshed ); cvSetMouseCallback( "image", on_mouse, 0 ); for(;;) { int c = cvWaitKey(0); if( (char)c == 27 ) break; if( (char)c == 'r' ) { cvZero( marker_mask ); cvCopy( img0, img ); cvShowImage( "image", img ); } if( (char)c == 'w' || (char)c == ' ' ) { CvMemStorage* storage = cvCreateMemStorage(0); CvSeq* contours = 0; CvMat* color_tab; int i, j, comp_count = 0; //cvSaveImage( "wshed_mask.png", marker_mask ); //marker_mask = cvLoadImage( "wshed_mask.png", 0 ); cvFindContours( marker_mask, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE ); cvZero( markers ); for( ; contours != 0; contours = contours->h_next, comp_count++ ) { cvDrawContours( markers, contours, cvScalarAll(comp_count+1), cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) ); } color_tab = cvCreateMat( 1, comp_count, CV_8UC3 ); for( i = 0; i < comp_count; i++ ) { uchar* ptr = color_tab->data.ptr + i*3; ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50); ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50); } { double t = (double)cvGetTickCount(); cvWatershed( img0, markers ); t = (double)cvGetTickCount() - t; printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) ); } // paint the watershed image for( i = 0; i < markers->height; i++ ) for( j = 0; j < markers->width; j++ ) { int idx = CV_IMAGE_ELEM( markers, int, i, j ); uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 ); if( idx == -1 ) dst[0] = dst[1] = dst[2] = (uchar)255; else if( idx <= 0 || idx > comp_count ) dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here else { uchar* ptr = color_tab->data.ptr + (idx-1)*3; dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2]; } } cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed ); cvShowImage( "watershed transform", wshed ); cvReleaseMemStorage( &storage ); cvReleaseMat( &color_tab ); } }