Created
March 10, 2010 08:31
-
-
Save LouisStAmour/327683 to your computer and use it in GitHub Desktop.
Quick try at matching wave whitecaps with OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "highgui.h" | |
#include "cv.h" | |
// A quick proof-of-concept by Louis St-Amour to scan for the white bits of waves | |
// in specific video files. Rather slow; each frame takes about 7 seconds (!) | |
// on a Core 2 Duo Unibody MacBook Pro. | |
IplImage* doPyrDown(IplImage* in, int filter) { | |
// Best to make sure input image is divisible by two. | |
assert( in->width%2 == 0 && in->height%2 == 0); | |
IplImage* out = cvCreateImage( | |
cvSize(in->width/2, in->height/2), | |
in->depth, in->nChannels); | |
cvPyrDown(in, out, filter); | |
return out; | |
} | |
IplImage* cvtHSV(IplImage* in) { | |
IplImage* out = cvCreateImage( | |
cvSize(in->width, in->height), | |
in->depth, in->nChannels); | |
cvCvtColor(in,out, CV_BGR2HLS); | |
return out; | |
} | |
int main (int argc, const char * argv[]) { | |
//cvNamedWindow("H", CV_WINDOW_AUTOSIZE); | |
cvNamedWindow("Preview", CV_WINDOW_AUTOSIZE); | |
//cvNamedWindow("S", CV_WINDOW_AUTOSIZE); | |
CvCapture* capture = cvCaptureFromFile(argv[1]); | |
if (!capture) { | |
return -1; | |
} | |
IplImage* frame = cvQueryFrame(capture); // Init the video read | |
double total = cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); | |
//CvVideoWriter *writer = cvCreateVideoWriter( | |
// argv[2], | |
// CV_FOURCC('A','V','C','1'), | |
// fps, cvGetSize(frame), 1); | |
IplImage* hImage = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
IplImage* sImage = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
IplImage* addedImage = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
IplImage* binaryImage = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
IplImage* eroded = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
IplConvKernel* kernel = cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_ELLIPSE, NULL); | |
IplImage* dilated = cvCreateImage(cvSize(frame->width, frame->height), | |
frame->depth, 1); | |
// IplImage* output = cvCreateImage( | |
// cvSize(frame->width, frame->height), | |
// frame->depth, frame->nChannels); | |
// IplImage* highlight = cvCreateImage( | |
// cvSize(frame->width, frame->height), | |
// frame->depth, frame->nChannels); | |
unsigned int count = 0; | |
do { | |
//cvSplit(hsvImage, hImage, sImage, vImage, NULL); | |
IplImage* hsvImage = cvtHSV(frame); | |
cvSetImageCOI(hsvImage, 1); | |
cvCopy(hsvImage, hImage, NULL); | |
cvSetImageCOI(hsvImage, 2); | |
cvCopy(hsvImage, sImage, NULL); | |
cvAddWeighted(hImage, 0.3, sImage, 0.7, 0.0, addedImage); | |
cvThreshold(addedImage, binaryImage, 105, 255, CV_THRESH_BINARY); | |
cvErode(binaryImage, eroded, kernel, 3); | |
cvDilate(eroded, dilated, kernel, 1); | |
//cvSetZero(highlight); | |
// cvSetImageCOI(highlight, 1); | |
// cvCopy(dilated, highlight, NULL); | |
// cvSetImageCOI(highlight, 2); | |
// cvCopy(dilated, highlight, NULL); | |
// cvSetImageCOI(highlight, 3); | |
// cvCopy(dilated, highlight, NULL); | |
// cvSetImageCOI(highlight, 0); | |
// cvAddWeighted(highlight, 1.0, frame, 0.0, 0, output); | |
//cvWriteFrame(writer, output); | |
//IplImage* sResized = doPyrDown(output, IPL_GAUSSIAN_5x5); | |
cvShowImage("Preview", dilated); | |
printf("%5u/%f\n", count, total); | |
#define BUFFER_SIZE 50 | |
char filename[BUFFER_SIZE]; | |
int n = snprintf(filename, BUFFER_SIZE, "export/%05u.png", count++); | |
if (n < BUFFER_SIZE) { | |
cvSaveImage(filename, dilated, NULL); | |
} | |
//cvReleaseImage(&sResized); | |
cvReleaseImage(&hsvImage); | |
// | |
// char c = cvWaitKey(33); | |
// if (c==27) { | |
// break; | |
// } | |
// | |
// IplImage* vImage = cvCreateImage(cvSize(hsvImage->width, hsvImage->height), | |
// hsvImage->depth, 1); | |
// cvSetImageCOI(hsvImage, 3); | |
// cvCopy(hsvImage, vImage, NULL); | |
// IplImage* vResized = doPyrDown(vImage, IPL_GAUSSIAN_5x5); | |
// cvShowImage("S", vResized); | |
// cvReleaseImage(&vImage); | |
// cvReleaseImage(&vResized); | |
// | |
// | |
// cvReleaseImage(&hsvImage); | |
// | |
char c = cvWaitKey(33); | |
if (c==27) { | |
break; | |
} | |
} while ((frame = cvQueryFrame(capture)) != NULL); | |
//cvReleaseVideoWriter(&writer); | |
cvReleaseImage(&sImage); | |
cvReleaseImage(&eroded); | |
cvReleaseImage(&dilated); | |
//cvReleaseImage(&output); | |
cvReleaseImage(&binaryImage); | |
cvReleaseImage(&addedImage); | |
//cvReleaseImage(&highlight); | |
cvReleaseImage(&hImage); | |
cvReleaseCapture(&capture); | |
//cvDestroyWindow("H"); | |
cvDestroyWindow("Preview"); | |
//cvDestroyWindow("S"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment