参考:10 12 14bit图像存储格式,利用Opencv显示10bit Raw数据,并根据鼠标的移动显示对应位置的灰度值。其他bit位数的Raw数据方法类似。
代码实现:
#include<opencv2/opencv.hpp>
#include<iostream>
#include<opencv/highgui.h>
#include"types.h"using namespace std;
using namespace cv;
#define WIDTH 1920
#define HEIGHT 1080
Mat srcImg, Raw16_Src, Raw8_Src, Raw16_Src_normal;
Point pre_pt(-1,-1);void MouseHandle(int event, int x, int y, int flags, void* param)
{Mat image = *(Mat*)param;Mat Gray2color_img = Mat(image.rows, image.cols, CV_8UC3, Scalar(0));char temp[50];int value;if (event == EVENT_MOUSEMOVE){cvtColor(image, Gray2color_img,COLOR_GRAY2BGR);pre_pt = Point(x, y);value = Raw16_Src.at<ushort>(y, x);sprintf(temp, "(x:%d,y:%d,value:%d)", x, y, value);putText(Gray2color_img, temp, pre_pt, FONT_HERSHEY_COMPLEX, 1.3, Scalar(0, 0, 255), 1, 8);imshow("Show Raw Image", Gray2color_img);}if (event == EVENT_LBUTTONDOWN){pre_pt = Point(x, y);cout << "坐标为" << pre_pt << endl;imshow("Show Raw Image", Gray2color_img);}
}
int main(void)
{int i;int bitdepth = 10;T_U8 *Raw_10bit = (T_U8*)malloc(WIDTH*HEIGHT * 10 / 8 * sizeof(T_U8));T_U16 *Raw_16bit = (T_U16*)malloc(WIDTH*HEIGHT * 16 / 8 * sizeof(T_U8));FILE *in_fp = fopen("RAW_2063_1920X1080_10bit_RGrGbB_2023_09_26,18,56,51.raw", "rb");FILE *output = fopen("RAW_2063_1920X1080_16bit.raw", "wb");if (Raw_16bit == NULL || Raw_10bit == NULL)printf("Can.'t malloc enough memory!\n");if (!in_fp){cout << "can't open file." << endl;return -1;}memset(Raw_10bit, 0, WIDTH*HEIGHT * 10 / 8 * sizeof(T_U8));memset(Raw_16bit, 0, WIDTH*HEIGHT * 16 / 8 * sizeof(T_U8));Raw16_Src.create(Size(WIDTH, HEIGHT), CV_16UC1);//Opencv Size 定义时是先列后行Raw16_Src_normal.create(Size(WIDTH, HEIGHT), CV_16UC1);//Opencv Size 定义时是先列后行Raw8_Src.create(Size(WIDTH, HEIGHT), CV_8UC1);fread(Raw_10bit, WIDTH*HEIGHT * 10 / 8, 1, in_fp);for (i = 0; i < WIDTH*HEIGHT / 4; i++){
#if 0Raw_16bit[4 * i] = (Raw_10bit[5 * i] | ((Raw_10bit[5 * i + 1] & 0x03) << 8))<<2;Raw_16bit[4 * i + 1] = (((Raw_10bit[5 * i + 1] & 0xfc) >> 2) | ((Raw_10bit[5 * i + 2] & 0x0f) << 6))<<2;Raw_16bit[4 * i + 2] = (((Raw_10bit[5 * i + 2] & 0xf0) >> 4) | ((Raw_10bit[5 * i + 3] & 0x3f) << 4))<<2;Raw_16bit[4 * i + 3] = (((Raw_10bit[5 * i + 3] & 0xc0) >> 6) | (Raw_10bit[5 * i + 4] << 2))<<2;
#endifRaw_16bit[4 * i] = (Raw_10bit[5 * i] | ((Raw_10bit[5 * i + 1] & 0x03) << 8));Raw_16bit[4 * i + 1] = (((Raw_10bit[5 * i + 1] & 0xfc) >> 2) | ((Raw_10bit[5 * i + 2] & 0x0f) << 6));Raw_16bit[4 * i + 2] = (((Raw_10bit[5 * i + 2] & 0xf0) >> 4) | ((Raw_10bit[5 * i + 3] & 0x3f) << 4));Raw_16bit[4 * i + 3] = (((Raw_10bit[5 * i + 3] & 0xc0) >> 6) | (Raw_10bit[5 * i + 4] << 2));}memcpy(Raw16_Src.data, Raw_16bit, (size_t)WIDTH*HEIGHT*sizeof(T_U16));normalize(Raw16_Src, Raw16_Src_normal, 0, 255, NORM_MINMAX);Raw16_Src_normal.convertTo(Raw8_Src, CV_8UC1);imwrite("Raw16.png", Raw8_Src);cvNamedWindow("Show Raw Image", CV_WINDOW_NORMAL);setMouseCallback("Show Raw Image", MouseHandle, (void*)(&Raw8_Src));imshow("Show Raw Image", Raw8_Src);fwrite(Raw_16bit, WIDTH*HEIGHT*sizeof(T_U16), 1, output);waitKey(0);destroyAllWindows();return 0;
}