2014-01-16 20 views
0

我已經開始在RI上學習圖像處理,想要改變圖像的亮度,使所有的圖像同樣亮麗。任何人都可以通過建議某種方式來幫助我實現這一點.. 可以提取圖像的RGB分量幫助我解決上述任務? 謝謝圖像處理在R

+1

請幫助我們爲您提供一個可重現的例子(即代碼和示例數據),請參閱http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example獲取詳細信息。 –

回答

3

與前面的答案一致,但值得注意的是,像png這樣的軟件包可以爲您提供R中圖像的原始數據,然後您可以使用矢量數學操作或以常規方式應用函數。

下面是一個簡單的例子,通過移動整個地圖來扭曲當前Google Doodle的亮度(主觀術語!),但是如果你想做一些非常複雜的事情(或者與結果可想而知!)

require(png) 

img<-readPNG(source="gdood.png") 

enter image description here

writePNG(img^0.5,target="bright.png") 

enter image description here

writePNG(img^2,target="dark.png") 

enter image description here

以及使其50%(或x%)更亮的代碼是(主觀地!)

writePNG(img+(1-img)*0.5,target="50percent.png") 

enter image description here

或 「兩次」 亮

writePNG(ifelse(img*2>1,1,img*2),target="twiceas.png") 

enter image description here

顯然也可以通過查看每個矩陣修改/訪問僅將R/G/B切片(1-R,2-G,3-B)

所以要改變紅色:

imgr<-img 
imgr[,,1]<-img[,,1]+(1-img[,,1])*0.5 
writePNG(imgr,target="redshift.png") 

enter image description here

計算IMAGES

按照要求的相對亮度,這裏是從網上下載了幾個類似的圖像的一個例子:

# get the images  
a.1<-readPNG(source="apple1-orig.png") 
a.2<-readPNG(source="apple2-orig.png") 

enter image description here enter image description here

注意:我之前通過使用圖像(塗鴉)在沒有透明度的示例中簡化了一些事情。爲了完整起見,我應該說明readPNG()函數返回一個RGBA對象,紅色,綠色,藍色和Alpha(透明度)的矩陣。如果矩陣中的Alpha = 1,上面的例子可以正常工作,但調整alpha不會像亮度一樣工作,所以如果你沒有一致的alpha值,你可能會得到奇怪的結果。這裏(下面)是去掉了透明度的圖片 - 你可以看到它們看起來出乎意料的不同。

# change the alpha (channel 4) to 1 (no transparency)  
a.1[,,4]<-a.1[,,4]+(1-a.1[,,4]) 
a.2[,,4]<-a.2[,,4]+(1-a.2[,,4]) 

writePNG(a.1,target="apple1.png") 
writePNG(a.2,target="apple2.png") 

enter image description here enter image description here

現在我們要調整亮度,而且很明顯,我們需要定義它。這裏我使用最簡單的定義(R + B + G)/ 3。有多少個定義;相當不錯的紙在這裏:http://www.kweii.com/site/color_theory/2007_LV/BrightnessCalculation.pdf

如果您需要一個特定的計算幫助,讓我知道,但這裏的基礎上,基本RGB計算簡單的一個:

# work out the brightness (~sum(R,G,B)/3) 
a.1.b<-sum(a.1[,,1:3])  # RGB brightness value a.1 
a.1.max<-length(a.1[,,1:3]) # max brightness value a.1 
b.1<-a.1.b/a.1.max     # brightness ratio 

a.2.b<-sum(a.2[,,1:3])  # RGB brightness value a.2 
a.2.max<-length(a.2[,,1:3]) # max brightness value a.2 
b.2<-a.2.b/a.2.max     # brightness ratio 

paste("a1 brightness:",b.1,"a2 brightness:",b.2) 
# here's the results 
[1] "a1 brightness: 0.151682883986928 a2 brightness: 0.483427459150327" 

#make a copy of a1 
a.1.new<-a.1 

#work out how much brighter to make it 
incr<-1-((1-b.2)/(1-b.1)) # % increase 

# then brighten it 
a.1.new[,,1:3]<-a.1[,,1:3]+(1-a.1[,,1:3])*incr 

# check equivalence between a2 and a1.new (rounding to avoid calc errors) 
round(sum(a.2),0)==round(sum(a.1.new),0) 
[1] TRUE 

writePNG(a.1.new,target="apple1-new.png") 

enter image description here

比較原稿:

enter image description here enter image description here

+0

謝謝你的解釋,但要使兩個圖像同樣增亮首先我必須比較兩個圖像的亮度,然後根據差異我必須執行矢量操作。我可以通過首先添加rgb找到兩個圖像之間的亮度差異兩個圖像的值分開,然後比較它們? – zzzz

+0

是的 - 我會調整以提供一些簡單的例子 – Troy

+0

它真的會幫助我..嘖嘖:) – zzzz

2

撇開「亮度」可以有幾個主觀的翻譯的事實,我強烈建議不要重新發明車輪。根據您的平臺和預算,GraphicConverter,GIMP,Photoshop,ImageJ等工具是專門爲圖像處理而設計的,並且會比R中的重寫算法更有效地完成工作。但是,在你真正開始做數字處理之前,你需要花時間學習「亮度」,「飽和度」,「色度」和其他顏色感知值之間的差異。