• OpenCV显示10bit Raw数据


    参考:10 12 14bit图像存储格式,利用Opencv显示10bit Raw数据,并根据鼠标的移动显示对应位置的灰度值。其他bit位数的Raw数据方法类似。

    代码实现:

    1. #include
    2. #include
    3. #include
    4. #include"types.h"
    5. using namespace std;
    6. using namespace cv;
    7. #define WIDTH 1920
    8. #define HEIGHT 1080
    9. Mat srcImg, Raw16_Src, Raw8_Src, Raw16_Src_normal;
    10. Point pre_pt(-1,-1);
    11. void MouseHandle(int event, int x, int y, int flags, void* param)
    12. {
    13. Mat image = *(Mat*)param;
    14. Mat Gray2color_img = Mat(image.rows, image.cols, CV_8UC3, Scalar(0));
    15. char temp[50];
    16. int value;
    17. if (event == EVENT_MOUSEMOVE)
    18. {
    19. cvtColor(image, Gray2color_img,COLOR_GRAY2BGR);
    20. pre_pt = Point(x, y);
    21. value = Raw16_Src.at(y, x);
    22. sprintf(temp, "(x:%d,y:%d,value:%d)", x, y, value);
    23. putText(Gray2color_img, temp, pre_pt, FONT_HERSHEY_COMPLEX, 1.3, Scalar(0, 0, 255), 1, 8);
    24. imshow("Show Raw Image", Gray2color_img);
    25. }
    26. if (event == EVENT_LBUTTONDOWN)
    27. {
    28. pre_pt = Point(x, y);
    29. cout << "坐标为" << pre_pt << endl;
    30. imshow("Show Raw Image", Gray2color_img);
    31. }
    32. }
    33. int main(void)
    34. {
    35. int i;
    36. int bitdepth = 10;
    37. T_U8 *Raw_10bit = (T_U8*)malloc(WIDTH*HEIGHT * 10 / 8 * sizeof(T_U8));
    38. T_U16 *Raw_16bit = (T_U16*)malloc(WIDTH*HEIGHT * 16 / 8 * sizeof(T_U8));
    39. FILE *in_fp = fopen("RAW_2063_1920X1080_10bit_RGrGbB_2023_09_26,18,56,51.raw", "rb");
    40. FILE *output = fopen("RAW_2063_1920X1080_16bit.raw", "wb");
    41. if (Raw_16bit == NULL || Raw_10bit == NULL)
    42. printf("Can.'t malloc enough memory!\n");
    43. if (!in_fp)
    44. {
    45. cout << "can't open file." << endl;
    46. return -1;
    47. }
    48. memset(Raw_10bit, 0, WIDTH*HEIGHT * 10 / 8 * sizeof(T_U8));
    49. memset(Raw_16bit, 0, WIDTH*HEIGHT * 16 / 8 * sizeof(T_U8));
    50. Raw16_Src.create(Size(WIDTH, HEIGHT), CV_16UC1);//Opencv Size 定义时是先列后行
    51. Raw16_Src_normal.create(Size(WIDTH, HEIGHT), CV_16UC1);//Opencv Size 定义时是先列后行
    52. Raw8_Src.create(Size(WIDTH, HEIGHT), CV_8UC1);
    53. fread(Raw_10bit, WIDTH*HEIGHT * 10 / 8, 1, in_fp);
    54. for (i = 0; i < WIDTH*HEIGHT / 4; i++)
    55. {
    56. #if 0
    57. Raw_16bit[4 * i] = (Raw_10bit[5 * i] | ((Raw_10bit[5 * i + 1] & 0x03) << 8))<<2;
    58. Raw_16bit[4 * i + 1] = (((Raw_10bit[5 * i + 1] & 0xfc) >> 2) | ((Raw_10bit[5 * i + 2] & 0x0f) << 6))<<2;
    59. Raw_16bit[4 * i + 2] = (((Raw_10bit[5 * i + 2] & 0xf0) >> 4) | ((Raw_10bit[5 * i + 3] & 0x3f) << 4))<<2;
    60. Raw_16bit[4 * i + 3] = (((Raw_10bit[5 * i + 3] & 0xc0) >> 6) | (Raw_10bit[5 * i + 4] << 2))<<2;
    61. #endif
    62. Raw_16bit[4 * i] = (Raw_10bit[5 * i] | ((Raw_10bit[5 * i + 1] & 0x03) << 8));
    63. Raw_16bit[4 * i + 1] = (((Raw_10bit[5 * i + 1] & 0xfc) >> 2) | ((Raw_10bit[5 * i + 2] & 0x0f) << 6));
    64. Raw_16bit[4 * i + 2] = (((Raw_10bit[5 * i + 2] & 0xf0) >> 4) | ((Raw_10bit[5 * i + 3] & 0x3f) << 4));
    65. Raw_16bit[4 * i + 3] = (((Raw_10bit[5 * i + 3] & 0xc0) >> 6) | (Raw_10bit[5 * i + 4] << 2));
    66. }
    67. memcpy(Raw16_Src.data, Raw_16bit, (size_t)WIDTH*HEIGHT*sizeof(T_U16));
    68. normalize(Raw16_Src, Raw16_Src_normal, 0, 255, NORM_MINMAX);
    69. Raw16_Src_normal.convertTo(Raw8_Src, CV_8UC1);
    70. imwrite("Raw16.png", Raw8_Src);
    71. cvNamedWindow("Show Raw Image", CV_WINDOW_NORMAL);
    72. setMouseCallback("Show Raw Image", MouseHandle, (void*)(&Raw8_Src));
    73. imshow("Show Raw Image", Raw8_Src);
    74. fwrite(Raw_16bit, WIDTH*HEIGHT*sizeof(T_U16), 1, output);
    75. waitKey(0);
    76. destroyAllWindows();
    77. return 0;
    78. }

    结果:

    红色箭头位置处的灰度值
  • 相关阅读:
    【浅学Java】Linux常用命令
    docker常用命令
    centos——记录一次开机启动设置
    数据库的三范式及BC范式
    Linux基础命令
    dreamweaver家乡主题网页设计 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载 HTML5期末大作业
    动态规划:区间动态规划
    jetsonTX2 nx配置yolov5和D435I相机,完整步骤
    研一如何开始大模型微调
    ThreadPool线程池
  • 原文地址:https://blog.csdn.net/lz0499/article/details/133321618