Opencv is an indispensable library in the field of visual analysis, so in the process of using the details and easily confused parts of the slowly recorded. Opencv is a huge library, in the use of the process of slowly explore it. The following is a reference to when the code is available, but direct compilation needs to be perfected.
YUV turn RGB
- Read YUV files
int WIDTH, HEIGHT; unsigned char buff[WIDTH*HEIGHT*3/2]; // read yuv file FILE *yuv_file = fopen(fileName,"rb"); int i = 0; while( ! feof(yuv_file) ) { unsigned char pixel= getc(yuv_file); // printf("%u ",pixel); buff[i++] = pixel; }Copy the code
- Yuv turn RGB
cv::Mat mYUV(HEIGHT + HEIGHT/2, WIDTH, CV_8UC1, (void*) buff);
cv::Mat mBGR(HEIGHT, WIDTH, CV_8UC3);
cvtColor(mYUV, mBGR, COLOR_YUV2RGB_I420, 3);
Copy the code
- opencv resize
cv::Mat dstBGR(HEIGHT, WIDTH, CV_8UC3);
cv::resize(mBGR, dstBGR, cv::Size(200, 200));
Copy the code
- Turn the floating-point
MBGR. ConvertTo (mfloat_BGR CV_32F, 1.0/255); // Convert the mRGB RGB value type to floatCopy the code
- Separated RGB channel
CV ::Mat (RGB) is RGBRGBRGBRGB RGBRGBRGBRGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RGBRGBRGGB RRRRRRRRRR GGGGGGGGGG BBBBBBBBBBBBB At this point you can use the CV ::split() function.
cv::Mat mat(HEIGHT, WIDTH, CV_8UC3); cv::Mat rgbChannnels[3]; split(mfloat_BGR,rgbChannnels); int singleChannelSize = 500* 500; float* yolo_rgb_plane = (float *)malloc(sizeof(float) * 500* 500* 3); for(int i = 0; i< 3; i++) { float *singleChannel = (float *)rgbChannnels[i].data; memcpy(yolo_rgb_plane+singleChannelSize*i, singleChannel, singleChannelSize * sizeof(float)); }Copy the code