跳到主要內容

A Java De-flicker Class based on Donald Graft's idea

Since QuickTime for Java has been depreciated in year 2009, and JMF (Java Media Framework) has to call for external library, there is a need to search alternative way assembling Jpeg images into a QuickTime movie.  I know Time Lapse Assembler has done a excellent job at this under Mac OS X, and it is free.  But Time Lapse Assembler fails to deal with the flickering problem commonly seen in Time-Lapse videos.  As a result, I have to find a pure-java way to write QuickTime file, and to deal with the flickering problem under Mac OS X.

Werner Randelshofer's Blog has a good solution writing QuickTime movies.  All I need to do myself is de-flickering.  Again, open-source resources help a lot.  Donald Graft kindly provides his source code of Deflicker Filter for VirtualDub.  We can learn his algorithm from the code. Graft's deflicker was written in C++.  However, what I need is Java.  So I re-write part of Graft's deflicker.  (Sorry, I still have no idea about the soften phase….)

The basic idea is simply.  As Graft wrote inside his code, "The adjustment factor is determined from a moving average of the luminance's of the past frames in the window."  Thus, what I need to do is quite simple, calculating the luminance (sum of each pixel's r+g+b divided by width*height*3) of each frame.  And then, just following Graft's idea.  Here is my deflicker filter code.



package org.sustudio.deflicker;

import java.awt.image.BufferedImage;

public class DonaldGraftDeflicker {

	private int window;
	private int scene_change_threshold;
	private int lumnData[];
	
	public DonaldGraftDeflicker(int window, int threshold) {
		this.window = window;
		this.scene_change_threshold = threshold;
		this.lumnData = new int[window];
		
		// first frame infication.
		this.lumnData[0] = 256;
	}	
			
	public BufferedImage deflicker(BufferedImage img) {
		BufferedImage image = img;
		int w = image.getWidth();
		int h = image.getHeight();
		int lum_sum = 0;
		
		// Calculate the luminance of the current frame.
		for (int y=0; y> 16) & 0xff;
				int green = (pixel >> 8) & 0xff;
				int blue = (pixel >> 0) & 0xff;
				//lum_sum += red + green + blue;
				
				lum_sum += (int) Math.ceil((0.299 * red) + (0.587 * green) + (0.114 * blue));
			}
		}
		//lum_sum /= (w * h * 3.0);
		lum_sum /= (w * h);
		System.out.print(lum_sum + "\t");
		
		// Do scene change processing.
		//boolean scene_change = false;
		if (this.scene_change_threshold < 256 &&
			this.lumnData[0] != 256 &&
			Math.abs((int)lum_sum - (int)this.lumnData[window-1]) >= this.scene_change_threshold)
		{
			this.lumnData[0] = 256;
			//scene_change = true;
		}
		
		
		// Calculate the adjustment factor for the current frame.
		// The adjustment factor is determined from a moving average
		// of the luminances of the past frames in the window.
		double scale = 1.0;
		if (this.lumnData[0] > 255) {
			for (int i=0; i 0) {
				scale = 1.0 / (double) lum_sum;
				double filt = 0.0;
				for (int i=0; i> 16) & 0xff;
				int green = (pixel >> 8) & 0xff;
				int blue = (pixel >> 0) & 0xff;
				int max = Math.max(Math.max(red, blue), green); 
				if (scale * max > 255.0) scale = 255.0 / (double)max;
				red = (int) (scale * red);
				green = (int) (scale * green);
				blue = (int) (scale * blue);
				pixel = (red & 0xff) << 16 | (green & 0xff) << 8 | (blue & 0xff) << 0;
				image.setRGB(x, y, pixel);
				lum_sum += (int) Math.ceil((0.299 * red) + (0.587 * green) + (0.114 * blue));
			}
		}
		lum_sum /= (w * h);
		System.out.println(lum_sum);
		
		return image;
		
	}
		
	private int getPixelGrayValue(int rgb) {
		int red = (rgb >> 16) & 0xff;
		int green = (rgb >> 8) & 0xff;
		int blue = (rgb >> 0) & 0xff;
		
		//return (int) Math.ceil((0.2126 * red) + (0.7152 * green) + (0.0722 * blue));
		return (int) Math.ceil((0.299 * red) + (0.587 * green) + (0.114 * blue));
	}
	
}

留言

熱門文章

差不多食譜實驗:小烤箱烤長茄子? Oven-roasted Long Aubergine?

發佈了「 差不多食譜:小烤箱烤茄子 Oven-roasted Aubergine 」之後,有位朋友在YouTube上留言問說,這個方法也可以用在市場上比較常看到的那種長茄子嗎?當時我只能回答不知道,因為沒試過。現在我有答案了!結果是一半一半!

差不多食譜:香煎南瓜片 Pan-fried Pumpkin

托爺爺的福,在萬聖節那碗「 南瓜盅飯 」後,我還有個老家的南瓜可以用。但要怎麼好好運用這顆南瓜,卻是個不小問題。差不多食譜曾經試過「 醋漬南瓜 」、「 牛奶南瓜 」,還有「 南瓜濃湯 」幾種運用新鮮南瓜的料理,難度雖然都不高,看起來卻都不像平時會出現在餐桌的家常菜。這次我們不妨搞得家常一點,用廚房裡都會有菜刀和煎鍋 (除非你家不開伙) ,來弄個超級家常的「香煎南瓜片」。

上車睡覺、下車尿尿

台灣人對於旅遊的形容,往往是「上車睡覺、下車尿尿」。這是因為行程被極度壓縮,試圖要在最短的時間裡面看最多的東西,於是每個景點都只是蜻蜓點水般匆匆帶過,停留的時間往往等同於排隊上廁所的時間。 這個紫南宮的行程已在「人山人海的紫南宮」當中提到過,只是當初沒寫到為什麼要去參觀紫南宮。事實上,紫南宮最富盛名的是可以借錢的土地公。然而,這點對我們家人而言一點吸引力都沒有,反倒是那號稱七星級的廁所,才是吸引我們前往參拜的重點。或許你會說:「有沒有搞錯,開一個鐘頭的車去上廁所!」沒錯,行程就是這麼規劃的。 在過年期間要到紫南宮上廁所還不是件容易的事。首先,你必須先開車到紫南宮,這大概是最容易的部份。接著,你就得面臨搶停車位窘境。 過年期間,不論哪個景點都是人山人海,免費停車位更是難找。紫南宮雖然提供一大片停車場,在過年期間卻也不敷使用,每台進入停車場的車都虎視眈眈,只要哪邊出現一個車位,起碼就有四、五台車等著。 好不容易停好車,接下來就得穿越那群要去借錢、還錢、拜拜的人群。在那個地方,怎麼前進的都不知道,反正總會有人把你向前推進。 好不容易到達造價上億的廁所,人山人海的盛況依舊。還好這群並非全部都來上廁所,要不然廁所早就被塞爆了。聽老爸老媽說,上次他們來的時候廁所裡面都沒人,還可以在裡面拍照、欣賞噴水。但這次裡面卻塞了不少人,我也不好意思拿著那麼大的一台相機拍人家上廁所的模樣,只能拍拍外觀。 由於是在盛產竹筍的竹山鎮,廁所的外觀也用竹筍作為造型,每個竹筍下方都是一間造價昂貴的廁所。一個令我覺得很貼心的設計在於,廁所不僅僅只是男廁/女廁,還有一個殘障廁所,方便行動不便的人。 雖然招牌寫著「金筍迎客」,但是看起來那個筍子明明就是不鏽鋼的,除非把它理解成金屬。我還特地爬上廁所的樓頂,想說能不能看到些什麼特別的風景。可是過年期間,看到的除了人群還是人群。 上完廁所後,簡單徒手祭拜了提供廁所的土地公,我們又再次穿越那重重的人群。不過,我還是偷偷跑去滾了一下那個象徵「財源滾滾」的金元寶。最後,又踏上那個停車戰場,從容地離去。 我不奢望有賺大錢的機會,只要生活能過得去,偶爾還能買些奢侈品犒賞自己就夠了。不過這個願望似乎也不簡單!