[Robotvision-commit] SF.net SVN: robotvision:[16] vision/src/findBuoy.c
Brought to you by:
phildavidson
From: <bre...@us...> - 2010-05-30 02:07:15
|
Revision: 16 http://robotvision.svn.sourceforge.net/robotvision/?rev=16&view=rev Author: brendasegal Date: 2010-05-30 02:07:09 +0000 (Sun, 30 May 2010) Log Message: ----------- This is stuff I did yesterday (before I found out that I could use Phil's findPipe function to find the buoy using thresholding). Either way, I haven't done the ellipse part yet (or averaging the points). I'm going to integrate this with main.cpp at some point with ellipse fitting, probably on Monday. Added Paths: ----------- vision/src/findBuoy.c Added: vision/src/findBuoy.c =================================================================== --- vision/src/findBuoy.c (rev 0) +++ vision/src/findBuoy.c 2010-05-30 02:07:09 UTC (rev 16) @@ -0,0 +1,279 @@ +#include "stdlib.h" +#include "stdio.h" +#include "cv.h" +#include "highgui.h" + +typedef struct retValue { + + double x; + double y; + +}point; + +int findBuoyVideo (char* origVideo, char* newVideo){ + + int sthreshold=180;//194 + double hlower=178; + double hupper=3; + + CvCapture* capture = 0; + capture = cvCreateFileCapture( origVideo ); + if(!capture){ + return -1; + } + IplImage *frame=cvQueryFrame(capture);//Init the video read + double fps = cvGetCaptureProperty ( + capture, + CV_CAP_PROP_FPS + ); + + CvSize size1 = cvSize( + (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH), + (int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT) + ); + CvVideoWriter *writer = cvCreateVideoWriter( + newVideo, + -1, + fps, + size1,1); + + int i; int j; + + int height; int width; int step; int channels; + int heightmono; int widthmono; int stepmono; int nChannelsmono; + uchar *data; uchar *datamono; + height = frame->height; + + width = frame->width; + step = frame->widthStep; + + channels = frame->nChannels; + stepmono = step; + nChannelsmono = channels; + + data = (uchar *)frame->imageData; + + + IplImage* logpolar_frame = cvCreateImage( + size1, + IPL_DEPTH_8U, + 3 + ); + + datamono = (uchar *)logpolar_frame->imageData; + while( (frame=cvQueryFrame(capture)) != NULL ) { + + for( i = 0 ; i < height ; i++ ) { + + for( j = 0 ; j < width ; j++ ) { + + if((data[i*step + j*channels+2]>=hlower) && ( data[i*step + j*channels + 2]>=hupper)){ + if( ( data[i*stepmono + j*nChannelsmono + 2] ) >sthreshold){ + + datamono[i*stepmono + j*nChannelsmono + 0] = 255; + datamono[i*stepmono + j*nChannelsmono + 1] = 255; + + datamono[i*stepmono + j*nChannelsmono + 2] = 255; + + } + else{ + + datamono[i*stepmono + j*nChannelsmono + 0] = 0; + + datamono[i*stepmono + j*nChannelsmono + 1] = 0; + + datamono[i*stepmono + j*nChannelsmono + 2] = 0; + + } + + + } + else{ + + datamono[i*stepmono + j*nChannelsmono + 0] = 0; + + datamono[i*stepmono + j*nChannelsmono + 1] = 0; + + datamono[i*stepmono + j*nChannelsmono + 2] = 0; + + } + + + + }//end inner for loop + + }//end outer for loop + + + cvShowImage("original frame", frame); + + cvShowImage("monoimage", logpolar_frame); + + cvWriteFrame( writer, logpolar_frame ); + + }//end while + + cvReleaseVideoWriter( &writer ); + cvReleaseImage( &logpolar_frame ); + cvReleaseCapture( &capture ); + return(0); + + } + +//For the findBuoy function, the center of mass is returned as (x,y) coordinates +//center of mass will either be found by averaging white pixels in binary image +//or by fitting an ellipse around the buoy +point findBuoy(IplImage* img){ + + point p; + p.x = 0.0; + p.y = 0.0; + + + //these are the threshold values + int sthreshold=194;//210; + double hlower=178; + double hupper=3; + + //create the windows: + //this is for the original image + cvNamedWindow("orig", CV_WINDOW_AUTOSIZE); + //this is for the smoothed image + cvNamedWindow("smooth", CV_WINDOW_AUTOSIZE); + //this is for the binary image (thresholding) + cvNamedWindow("binary", CV_WINDOW_AUTOSIZE); + + //display the original image + cvShowImage("orig", img); + + //we smooth the image and store the smooth img back in img + cvSmooth(img, img, CV_GAUSSIAN, 3, 3, 0, 0); + + //display the smoothed image + cvShowImage("smooth", img); + + //these are the properties of the image + //we will use them to loop through the data array (the image buffer) + int height = img->height; + int width = img->width; + int step = img->widthStep; + int nChannels = img->nChannels; + + //this is the image buffer + uchar *data = ( uchar* )img->imageData; + + int i; int j; + + for( i = 0 ; i < height ; i++ ) { + + for( j = 0 ; j < width ; j++ ) { + + if((data[i*step + j*nChannels+2]>=hlower) && ( data[i*step + j*nChannels + 2]>=hupper)){ + + + if( ( data[i*step + j*nChannels + 2] ) >sthreshold){ + + data[i*step + j*nChannels + 0] = 255; + + data[i*step + j*nChannels + 1] = 255; + + data[i*step + j*nChannels + 2] = 255; + + } + else{ + data[i*step + j*nChannels + 0] = 0; + + data[i*step + j*nChannels + 1] = 0; + + data[i*step + j*nChannels + 2] = 0; + + } + + + } + else{ + + data[i*step + j*nChannels + 0] = 0; + + data[i*step + j*nChannels + 1] = 0; + + data[i*step + j*nChannels + 2] = 0; + + } + + + + }//end inner for loop + +}//end outer for loop + + cvErode(img,img,0,6); + cvDilate(img, img,0,10); + + //display the binary image + cvShowImage("binary", img); + + cvWaitKey(0); + + cvDestroyWindow("orig"); + cvDestroyWindow("smooth"); + cvDestroyWindow("binary"); + + + + + cvWaitKey(0); + + return p; + +} + +int dmain(int argc, char** argv){ + if (argc <= 1){ + + printf("not enough arguments\n"); + return 1; + + } + + if(strcmp(argv[1], "-v")==0){ + + if (argc <= 3){ + + printf("not enough arguments\n"); + return 1; + + } + findBuoyVideo(argv[2],argv[3]); + } + + + + //testing now findBuoy which so far only smooths the image it is + //given. I'm opening a bunch of windows with the effects produced + else if(strcmp(argv[1], "-i")==0){ + + if (argc <= 2){ + + printf("not enough arguments\n"); + return 1; + + } + + IplImage *img = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR ); + + findBuoy(img); + } + + + + return 0; +} + + + + + + + + Property changes on: vision/src/findBuoy.c ___________________________________________________________________ Added: svn:mime-type + text/plain This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |