[英]Convert MONO-16bit image with 2bytes per pixel to MONO-16bit with 1 byte per pixel in C#
I am working in C# and I have a monochromatic camera returning to me two successive images. 我在C#工作,我有一个单色相机给我两个连续的图像。 I want to subtract these images. 我想减去这些图像。
The camera is set to MONO-16 with resolution 1980x960. 相机设置为MONO-16,分辨率为1980x960。
The camera returns to me a vector of 2457600 elements, because each pixel is represented by two bytes so {1980x960 = 1228800} * 2 bytes / pixel = 2457600
. 相机向我返回2457600个元素的向量,因为每个像素由两个字节表示,因此{1980x960 = 1228800} * 2 bytes / pixel = 2457600
。
The problem is that I have to convert the two 2457600 element vectors to two 1228800 vectors in order to do the subtraction. 问题是我必须将两个2457600元素向量转换为两个1228800向量才能进行减法。
So I need to combine two successive elements of the 2457600 into one element so that I end up with a vector of 1228800 elements, where its element have 16bit range (0-65536). 所以我需要将2457600的两个连续元素组合成一个元素,这样我最终得到一个1228800元素的向量,其元素有16位范围(0-65536)。
I will probably have to create a loop to iterate 1228800 times and in each iteration take two elements from the 2457600 vector: 我可能需要创建一个迭代1228800次的循环,并在每次迭代中从2457600向量中获取两个元素:
for(int i = 0 ; i < 2457600 ; i = i + 2)
{
byte BYTE1 = newGrabResultInternal2.ImageData.Buffer[i];
byte BYTE2 = newGrabResultInternal2.ImageData.Buffer[i+1];
// What to do next to combine the 2 bytes into one???
}
When I have the two bytes which represent 1 pixel, how do I combine them together to give me an intensity value of 16bit depth (0-65536)? 当我有两个代表1个像素的字节时,我如何将它们组合在一起给我一个16位深度的强度值(0-65536)?
Also, if you can suggest a different approach it would be helpful. 此外,如果您可以提出不同的方法,那将会有所帮助。
You can use bit operations for that: 您可以使用位操作:
for(int i=0;i<2457600;i=i+2)
{
byte BYTE1 = newGrabResultInternal2.ImageData.Buffer[i];
byte BYTE2 = newGrabResultInternal2.ImageData.Buffer[i+1];
int pixel = ((int)BYTE1 << 8) | ((int)BYTE2);
}
This will shift BYTE1
8 bits left and then combine it with BYTE2
. 这将使BYTE1
8位,然后将其与BYTE2
组合。
This will make BYTE1
the higher byte and BYTE2
the lower. 这将使BYTE1
为较高字节, BYTE2
为较低字节。 If you want it the other way around you need to swap them. 如果你想要它反过来需要交换它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.