bitmap convert to RGB565 display

本文介绍如何在RGB888格式与RGB565格式之间进行颜色空间的转换。详细展示了通过位操作实现颜色通道的提取和放大,包括从RGB565到RGB888以及从RGB888到RGB565的转换方法。

bitmap图片是一个RGB888,每个像素由3个字节组成,R->8bit,G->8bit,B->8bit;

  RGB565 的每个pixels是由2字节组成,R->5bit,G->6bit,B->5bit。

转换的思路是取出原图的点,对没个采样进行运算。

  1. #define RGB565_MASK_RED        0xF800   
  2. #define RGB565_MASK_GREEN                         0x07E0   
  3. #define RGB565_MASK_BLUE                         0x001F   
  4.   
  5. void  rgb565_2_rgb24( BYTE  *rgb24,  WORD  rgb565)  
  6. {   
  7.  //extract RGB   
  8.  rgb24[2] = (rgb565 & RGB565_MASK_RED) >> 11;     
  9.  rgb24[1] = (rgb565 & RGB565_MASK_GREEN) >> 5;  
  10.  rgb24[0] = (rgb565 & RGB565_MASK_BLUE);  
  11.   
  12.  //amplify the image   
  13.  rgb24[2] <<= 3;  
  14.  rgb24[1] <<= 2;  
  15.  rgb24[0] <<= 3;  
  16. }   

  1. USHORT  rgb_24_2_565( int  r,  int  g,  int  b)  
  2. {  
  3.     return  ( USHORT )(((unsigned(r) << 8) & 0xF800) |   
  4.             ((unsigned(g) << 3) & 0x7E0)  |  
  5.             ((unsigned(b) >> 3)));  
  6. }  

  1. USHORT  rgb_24_2_555( int  r,  int  g,  int  b)  
  2. {  
  3.     return  ( USHORT )(((unsigned(r) << 7) & 0x7C00) |   
  4.             ((unsigned(g) << 2) & 0x3E0)  |  
  5.             ((unsigned(b) >> 3)));  
  6. }  
  7.   
  8. COLORREF  rgb_555_2_24( int  rgb555)  
  9. {  
  10.     unsigned r = ((rgb555 >> 7) & 0xF8);  
  11.     unsigned g = ((rgb555 >> 2) & 0xF8);  
  12.     unsigned b = ((rgb555 << 3) & 0xF8);  
  13.     return  RGB(r,g,b);  
  14. }  
  15.   
  16. void  rgb_555_2_bgr24( BYTE * p,  int  rgb555)  
  17. {  
  18.     p[0] = ((rgb555 << 3) & 0xF8);  
  19.     p[1] = ((rgb555 >> 2) & 0xF8);  
  20.     p[2] = ((rgb555 >> 7) & 0xF8);  
  21. }  



http://blog.youkuaiyun.com/nitghost/archive/2009/02/23/3925678.aspx
public void FrameGrabThread(object obj)//实时抓图 { IStreamGrabber streamGrabber = (IStreamGrabber)obj; while (Outcamera == 0) { //Thread.Sleep(50); // 阻塞当前线程 * 秒 IFrameOut frameOut;//图像数据和帧信息的接口 // ch:获取图像信息 | en: Get image info nRet = device.StreamGrabber.GetImageBuffer(1000, out frameOut); if (MvError.MV_OK == nRet) { // ch:显示图像 | en: display image // device.ImageRender.DisplayOneFrame(pictureBox1.Handle, frameOut.Image); IImage outImage; // ch:目的像素格式 | en:Dest Pixel type MvGvspPixelType dstPixelType = MvGvspPixelType.PixelType_Gvsp_RGB8_Packed; // ch:像素格式换 | en:Pixel type convert int result = device.PixelTypeConverter.ConvertPixelType(frameOut.Image, out outImage, dstPixelType); if (result == MvError.MV_OK) { Bitmap bitmap = outImage.ToBitmap(); if (bitmap != null) { if(Open == true)//抓拍图片 { /* try { // 强制格式换为 24 位 RGB Bitmap bitmap24 = ConvertTo24bpp(bitmap);//调用强制换方法 matA = BitmapConverter.ToMat(bitmap24); if (matA != null && !matA.Empty()) { AppendLog($"Mat A 换成功,尺寸:{matA.Width}x{matA.Height}"); potoOK = true; } else { AppendLog("换 Mat 失败:返回空矩阵"); matA = null; potoOK = false; } bitmap24.Dispose();//释放资源 } catch (Exception ex) { AppendLog("换 Mat 时出错:" + ex.Message); matA = null; }*/ UpdatePicture2Box(bitmap); matA = BitmapConverter.ToMat(bitmap); // 将 Bitmap 换为 Mat potoOK = true; Open = false; } // 在 UI 线程中处理图像 UpdatePictureBox(bitmap); // 不要在这里调用 bitmap.Dispose(),因为 ToBitmap() 是深拷贝 // 如果 outImage.ToBitmap() 返回的是新图像,可以安全释放 bitmap bitmap.Dispose(); } } // ch:释放图像 | en: free image 没有会造成内存泄漏 outImage.Dispose(); } if (Outcamera == 0) { // ch:释放图像 | en: free image 没有会造成内存泄漏 device.StreamGrabber.FreeImageBuffer(frameOut); } else { break; } } }接上个代码 里面的matA 在此循环中被赋值 在上次的代码中调用显示为之前的图片,不是最新图,第二次调用才是最新的,最初的一次为空
最新发布
08-03
private void UpdatePictureBox(Bitmap bitmap) { if (pictureBox1.InvokeRequired) { // 如果当前线程不是 UI 线程,则通过 Invoke 切换到 UI 线程 pictureBox1.Invoke(new MethodInvoker(() => UpdatePictureBox(bitmap))); } else { // 已在 UI 线程,直接更新 if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); // 避免内存泄漏 } pictureBox1.Image = new Bitmap(bitmap); // 深拷贝 } } public void FrameGrabThread(object obj) { IStreamGrabber streamGrabber = (IStreamGrabber)obj; while (Outcamera == 0) { Thread.Sleep(200); // 阻塞当前线程 * 秒 IFrameOut frameOut;//图像数据和帧信息的接口 // ch:获取图像信息 | en: Get image info nRet = device.StreamGrabber.GetImageBuffer(1000, out frameOut); if (MvError.MV_OK == nRet) { // ch:显示图像 | en: display image // device.ImageRender.DisplayOneFrame(pictureBox1.Handle, frameOut.Image); IImage outImage; // ch:目的像素格式 | en:Dest Pixel type MvGvspPixelType dstPixelType = MvGvspPixelType.PixelType_Gvsp_RGB8_Packed; // ch:像素格式换 | en:Pixel type convert int result = device.PixelTypeConverter.ConvertPixelType(frameOut.Image, out outImage, dstPixelType); if (result == MvError.MV_OK) { Bitmap bitmap = outImage.ToBitmap();//输出图Bitmap 类型 /* if (bitmap != null) { Bitmap copy = new Bitmap(bitmap); // 在采集线程中做深拷贝 bitmap.Dispose(); // 立即释放原始图像 this.Invoke((MethodInvoker)delegate { if (pictureBox1.Image != null) { pictureBox1.Image.Dispose(); // 释放旧图像 } pictureBox1.Image = copy; }); }*/ UpdatePictureBox(bitmap); bitmap.Dispose(); } // pictureBox1.Image = frameOut.Image; // ch:释放图像 | en: free image 没有会造成内存泄漏 outImage.Dispose(); } // ch:释放图像 | en: free image 没有会造成内存泄漏 device.StreamGrabber.FreeImageBuffer(frameOut); } } pictureBox1.Image = new Bitmap(bitmap); // 深拷贝报错 内存不足 应该怎么改
07-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值