Extracting pixel values from videos using OpenCV

I have been associated with an image processing group for some time that is doing research on background/foreground segmentation using neural networks. I might soon add a publication (depending if we get it done on time) on pixel-wise based segmentation. Here’s a function which takes an array of video files, and exports their pixels, one each line, into a file with a given separator. It is interesting to apply unsupervised learning alghoritms on the resulting set. Link to source. Written in C.

#include <stdlib.h>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>

 * Function extracts pixel values from video, frame by frame and writes them into a file
 * videos - array of file names to be read
 * outputFilename - output file
 * separator - value separator
 * normalize - if 1 normalize the RGB values to [0, 1] interval else values stay in [0, 255]
int extractPixelsFromVideo(char** videos, char* outputFilename,
		char* separator, int normalize) {

	// Open the file for writing the pixel values into
	FILE *ofp;
	char mode[] = "w";
	ofp = fopen(outputFilename, mode);
	if (ofp == NULL) {
		printf("Can't open output file %s!\n", outputFilename);

	// Normalize the pixel values to the [0, 1] interval
	int divisor;
	if (normalize == 1) {
		divisor = 255;
	} else {
		divisor = 1;

	// Declare the structure that holds the video
	CvCapture* capture = 0;
	// Declare the structure that holds the frames
	IplImage* img = 0;

	// Number of elements in the video array
	int videoCount = sizeof(videos) / sizeof(char*);

	// Pixel counter, and for iterator
	int count = 0;

	// Loop through videos and get the pixel values
	for (int i = 0; i < videoCount; i++) {
		// Open video
		CvCapture* capture = cvCaptureFromAVI(videos[i]);
		printf("%s\n", videos[i]);

		// Grab the frames 1 by 1
		while (cvGrabFrame(capture) != 0) {

			// Retrieve the captured frame
			img = cvRetrieveFrame(capture);

			// String that keeps the pixel values to be written
			char special[200];

			// Iterate through the image and pick the RGB values of each pixel
			for (int j = 0; j < img -> height; j++) {
				for (int k = 0; k < img -> width; k++) {
					// Get the (i,j) pixel value
					CvScalar s;
					s = cvGet2D(img, j, k);

					// Format the string to be written
					sprintf(special, "%f%s%f%s%f\n", s.val[0] / divisor,
							separator, s.val[1] / divisor, separator,
							s.val[2] / divisor);

					// Write the pixel values and count it
					fprintf(ofp, "%s", special);

	// Free resources

	// This part only works on UNIX, and it writes the total number of pixels
	// at the beggining of the file (uncomment if needed)
	 char systemRun[200];
	 sprintf(systemRun, "sed -i  '1i %d' %s", count, outputFilename);

	return 0;

Tagged , , , ,

One thought on “Extracting pixel values from videos using OpenCV

  1. Kira says:

    I enjoy, lead to I discovered just what I used to be looking for.
    You’ve ended my 4 day lengthy hunt! God Bless you man. Have a nice day. Bye

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.