Deinterlace - opencv

hi I needed a quick deinterlace.

this isn’t really completed, and it’s a bit cheap, but honestly it works, averaging ever second line as the mix of the one above it and below it…

posting it here in case it’s helpful to someone… if I make it better, I’ll post updates

take care!

void ofCvGrayscaleImage::ghettoDeinterlace(ofCvGrayscaleImage & mom){  
	// to do:   
	// didn't do i = 0 or i = momHeight-1;  
	// could be memcopies...  
// sorry, my incoming image was black background,   
//so this is a cheap quick (and inefficient) hack   
	for (int i = 1; i < mom.height-1; i++){  
		char * momspixa = mom.cvImage->imageData + ((i-1) * mom.cvImage->widthStep);  
		char * momspixb = mom.cvImage->imageData + ((i) * mom.cvImage->widthStep);  
		char * momspixc = mom.cvImage->imageData + ((i+1) * mom.cvImage->widthStep);  
		char * mepix = cvImage->imageData + (i * cvImage->widthStep);  
		for (int j = 0; j < mom.width; j++){  
			if (i % 2 == 0) mepix[j] = momspixb[j];  // should be memcopy!!  
				int pix = (int)(0.5f * (unsigned char)momspixa[j]) + (0.5f * (unsigned char)momspixc[j]);  
			  mepix[j] = pix;  

hi zach …

i’ve tried to “copy” this function to make it work with a ofCvColorImage.
basically i pasted the same function and changed grayscale to color image.
i’m not getting good results and now several questions arise …

* it’s hard to me to understand how this code works and i’m not sure if my easy approach of “copy” and rename it’s just a silly way … or if there’s a chance to make it work ?¿

* sorry but … a basic question : the idea of this function is to be called from an ofCvGrayscaleImage (the destination) and the parameter is another ofCvGrayscale image, let’s say the source ?

* in the example i’m trying to make run i need to Deinterlace a DV camera which is outputting via analogic video signal, which is captured trough a USB analogic capture board … is it possible to do so ? i mean, does the image to be deinterlaced need to be grabbed from a DV device or it’s possible to de-interlace a DV signal captured with an analogic board (at 720x576 capture resolution) ?

thanks a lot :wink:


it’s written for ofCvGrayscaleImage, so it’s not going to work right away w/ color.

yes, you call ghettoDeinterlace(ofCvGrayscaleImage & mom) so it deinterlaces mom into the image you call it in. it could easily be done in place.

the basic idea is to make every other line (i % 2 != 0) a mixture of the line above it and below it, ie:

0 0 0 0 0 0
2 2 2 2 2 2
2 2 2 2 2 2
4 4 4 4 4 4
7 7 7 7 7 7

we alter every other line so that is a mix of the pixel below it and above it:

0 0 0 0 0 0
1 1 1 1 1 1
3 3 3 3 3 3
5 5 5 5 5 5
7 7 7 7 7 7

does that description make sense?

worked for me on deinterlacing off a 640x480 capture card and a security camera. it’s pretty simple, I guess you’d get good results. I was amazed at how easy it was.

take care!

ps: looking at the code I think there is a small bug in to top line and bottom line, they must come out black, or something else – I didn’t notice because my source was on a black background. I’ll probably revise the code when I get a free moment to fix that.

hi zach :wink:

thanks for your explanation … i do understand the concept of the algorithm but i’m not so familiar with CvImages in ofCv, and less in color space to know how to translate it to color…

i’ll try to revise it and see if i can get a ofCvColor solution…
another chance would be to implement that on a GLSL shader, maybe it will be easier for me to work it out … i’ll let u know



Thanks for that code, just what i needed. Optimized a bit and made it in place.

I realized that if the image has even number of lines, which i think it usually has, the last line will be left false. Thats the reason for the last if statement which just duplicates the next last line. The last line is not visible when you draw the image (due to some border pixel problems mentioned in ofTexture?), but i think this generates a good image for tracking.

void ofxCvGrayscaleImage::deinterlace(){  
 char* linea;  
 char* lineb;  
 char* linec;  
 for (int i = 1; i < height-1; i+=2){  
  linea = cvImage->imageData + ((i-1) * cvImage->widthStep);  
  lineb = cvImage->imageData + ((i) * cvImage->widthStep);  
  linec = cvImage->imageData + ((i+1) * cvImage->widthStep);  
  for (int j = 0; j < width; j++){  
   lineb[j] =   
    (0.5f*(unsigned char)linea[j]) + (0.5f*(unsigned char)linec[j]);  
 if (height > 1 && height % 2 == 0){  
  linea = cvImage->imageData + ((height-2) * cvImage->widthStep);  
  lineb = cvImage->imageData + ((height-1) * cvImage->widthStep);  
  memcpy(lineb, linea, width);  

Im trying to deinterlace a security camera.
When i put the above code into the opencv example i get:

error: no ‘void ofxCvGrayscaleImage::deinterlace()’ member function declared in class ‘ofxCvGrayscaleImage’

what do i need to do?

of 0.06 FAT mac os x 10.5.8


By the way there’s also some deinterlace code in the Linux DVgrabber addon if you’re willing to dig into the code:…

Hi Daniel,

That’s because the deinterlace function is not part of the original ofxCvGrayscaleImage. You need to add it based on the code i provided above. You can either extend ofxCvGrayscaleImage as myCvGrayscaleImage or do the ugly version of just adding it to the original .h and .cpp files.

hope that helps

I have a simple line averaging GLSL and Core Image based deinterlacer that works pretty good, at HD (1080i) resolutions in realtime on any decent hardware…

Check this thread out:…-ght=#16326

The GLSL is in a post above the Core Image one. Maybe that is helpful?

Thanks grimus, cj and vade, i will check it out as soon as i have my hands on the interlacing camera again.

all the best

I am new to C++. I wanted a deinterlacing algorithm to combat motion blur. But I do not know how to use the above code as deinterlace is not a function in the class ofxCvGrayscaleImage. I think I will also have to include the relevant header files of “ofxCvGrayscaleImage”.

How do I do it?

Thanks in advance!

The example code is in a custom function added by @zach, it isn’t in the code openFrameworks code. You can create the custom function in your ofApp or anywhere else you like.

Yes. I understand that it is a custom made function. But I do not understand the internal structure of the function to use it according to my needs. As far as I have seen there is no function named cvImage in the class ofxCvGrayscaleImage to access it via the object mom. I am still confused. Kindly help.

Thank you!