2011-08-22 91 views
3
Image<bgr,byte> WeightedImg; 
. 
. 

double color; 

for (int i = 0; i < dataArray.Length; i++){ 
    color = dataArray[i, 2]; 
    WeightedImg.Bitmap.SetPixel(x, y,Color.FromArgb((int)Math.Ceiling(color * R), (int)Math.Ceiling(color * G),(int)Math.Ceiling(color * B))); 
} 

這一行:圖像轉換

WeightedImg.Bitmap.SetPixel(x, y,Color.FromArgb((int)Math.Ceiling(color * R), 
    (int)Math.Ceiling(color * G),(int)Math.Ceiling(color * B))); 

使得程序崩潰..我想根據double值在WeightedImg設置像素..這可能嗎?

或者我能否將Image<bgr,byte>轉換成Bitmapdouble[,]

+0

你得到什麼異常? –

+0

無法識別或不支持的陣列類型 –

回答

0

如果我不得不猜測,而不知道例外情況,你是Math.Ceiling調用正在返回一個小於0或大於255的值。也許在將值發送到Color.FromArgb之前限制它的值範圍。另外,請仔細檢查x和y值是否在圖像中。很難說清楚上面的代碼。

+0

例外是 無法識別或不支持的陣列類型 –

1

好的,你的代碼在我嘗試的時候有效,但是這裏有替代方案。

此代碼自然工作提供色彩* N < 255(產生不同的錯誤):

Image<Bgr, byte> img = new Image<Bgr,byte>(10,10); 


double color = 5; 
double R = 20, B = 20, G = 20; 
img.Bitmap.SetPixel(0,0,Color.FromArgb((int)Math.Ceiling(color * R),(int)Math.Ceiling(color * G), (int)Math.Ceiling(color * B))); 

你可以嘗試同樣的操作替代方法是將值直接分配注意數據屬性如果內存供應我糾正爲[高度,寬度,深度]格式,以便:

img.Data[y,x, 0] = (byte)Math.Ceiling(color * R); //Red 
img.Data[y,x, 1] = (byte)Math.Ceiling(color * G); //Green 
img.Data[y,x, 2] = (byte)Math.Ceiling(color * B); //Blue 

或者更直接,你可以使用:

img[0, 0] = new Bgr(Color.FromArgb((int)Math.Ceiling(color * R), (int)Math.Ceiling(color * G), (int)Math.Ceiling(color * B))); 

所有這些方法的工作我已經過測試。

至於你的其他問題,是的,你可以將圖像轉換爲位圖

Bitmap x = img.ToBitmap(); 

不能明確的數據轉換爲雙[,,](不是雙[,]),而無需通過閱讀每一個像素,並採取將數據從圖像到該陣列我不會替代地推薦以下格式細:

Image<Bgr,Double> img = new Image<Bgr,Double>(10,10); //or (filename) of course 

和轉換

Image<Bgr,Double> img_double = img.Convert<Bgr,Double>(); 

但要記住,你不能在一個時間不能直接轉換爲轉換一個以上的項目,必須這樣做:

Image<Gray,Double> img_gray = img.Convert<Gray,Byte>().Convert<Gray,Double>(); 
//or alternatively  
Image<Gray,Double> img_gray = img.Convert<Bgr,Double>().Convert<Gray,Double>();