CBitmap转Mat
时间: 2023-10-27 10:55:35 浏览: 254
要将CBitmap对象转换为Mat对象,可以使用以下方法:
```cpp
bool CBitmapToMat(CBitmap& bitmap, cv::Mat& mat) {
BITMAP bmp;
bitmap.GetBitmap(&bmp);
int nChannels = bmp.bmBitsPixel == 1 ? 1 : bmp.bmBitsPixel / 8;
int depth = bmp.bmBitsPixel == 1 ? IPL_DEPTH_1U : IPL_DEPTH_8U;
mat.create(cv::Size(bmp.bmWidth, bmp.bmHeight), CV_MAKETYPE(CV_8U, nChannels));
bitmap.GetBitmapBits(bmp.bmHeight * bmp.bmWidth * nChannels, mat.data);
return true;
}
```
这个函数将CBitmap对象转换为BITMAP结构体,然后根据位图的像素位数和通道数创建一个Mat对象,并使用GetBitmapBits函数将位图数据复制到Mat对象中。这样就完成了CBitmap到Mat的转换。
#### 引用[.reference_title]
- *1* *3* [Mat转HBITMAP CBitmap](https://blog.csdn.net/qq_34901073/article/details/105419940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [位图转mat](https://blog.csdn.net/biersibao/article/details/107380675)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文