在OpenCV的C#图像转换 [英] Image conversion in OpenCV C#
问题描述
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)));
使得程序崩溃。我想设置一个像素在 WeightedImg
根据双击
价值..这可能吗?
makes the program crash .. I want to set a pixel in a WeightedImg
according to a double
value .. Is that possible?
或可以转换从图片< BGR,字节>
到位图
或双击[,]
?
谢谢大家..
推荐答案
确定还有你的代码的工作,当我尝试它周围有这种替代品然而
Ok well your code works when I try it there are alternatives around this however.
这代码不工作自然色彩提供* N< 255(产生不同的错误):
This Code does work naturally providing colour*N < 255 (which produces a different error):
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)));
替代方式,你可以尝试同样的操作是将值直接分配注意数据属性如果没有记错我是正确的[高度,宽度,深度]格式,所以:
Alternative way you could attempt the same operation are to assign the value directly note the Data property if memory serves me correct is [height,width,depth] format so:
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)));
所有这些方法工作的我已经测试。
All of these methods work an I have been tested.
至于你的其他问题是你可以将图像转换为位图
As for your other questions yes you can convert an image to a Bitmap
Bitmap x = img.ToBitmap();
您可以没有明确的数据转换为双[,,](不是双[,] )无需通过每个像素读,从图像获取数据到这个阵列是我所不愿意或者以下格式的罚款建议:
You can't explicitly convert the data to a double [,,] (not double[,]) without reading through every pixel and taking the data from the image to this array which I wouldn't recommend alternatively the following formats are fine:
Image<Bgr,Double> img = new Image<Bgr,Double>(10,10); //or (filename) of course
和转换
Image<Bgr,Double> img_double = img.Convert<Bgr,Double>();
但要记住,你不能在时间转换多个项目一个不能直接转化为它必须这样做:
but remember you can't convert more than one item at a time a cannot be directly converted to it must be done like this:
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>();
我希望这回答了你的问题。
I hope this answers your question
干杯
克里斯
这篇关于在OpenCV的C#图像转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!