Cvtcolor

The operator converts the input picture from one color space to another, supporting the conversion of color channels, formats and depths, often to convert the picture format.

Operator Effect

Input ImageParameterOutput Image
VP5input_rgb-VP5out_cvtcolor

Principle

There are a variety of formulas to choose from for the color space conversion process, and one method is listed as belows:

RGB to Gray

Y=0.299R+0.587G+0.114BY=0.299*R+0.587*G+0.114*B

RGB/BGR \Leftrightarrow YUV420

Y=16+0.257R+0.504G+0.098BY=16+0.257*R+0.504*G+0.098*B

Cb=1280.148R0.291G+0.439BCb=128-0.148*R-0.291*G+0.439*B

Cr=128+0.439R0.368G0.071BCr=128+0.439*R-0.368*G-0.071*B

R=1.164(Y16)+1.596(Cr128)R=1.164*(Y-16)+1.596*(Cr-128)

G=1.164(Y16)0.392(Cb128)0.812(Cr128)G=1.164*(Y-16)-0.392*(Cb-128)-0.812*(Cr-128)

B=1.164(Y16)+2.016(Cb128)B=1.164*(Y-16)+2.016*(Cb-128)

API Interface

int32_t hbVPCvtColor(hbUCPTaskHandle_t *taskHandle, hbVPImage *dstImg, hbVPImage const *srcImg);

For detailed interface information, please refer to hbVPCvtColor.

Usage

// Include the header #include "hobot/hb_ucp.h" #include "hobot/vp/hb_vp.h" #include "hobot/vp/hb_vp_cvt_color.h" // init Image, allocate memory for image data hbUCPSysMem src_mem; hbUCPMallocCached(&src_mem, src_stride * src_height, 0); hbVPImage src_img{HB_VP_IMAGE_FORMAT_RGB, HB_VP_IMAGE_TYPE_U8C3, src_width, src_height, src_stride, src_mem.virAddr, src_mem.phyAddr, nullptr, 0, 0}; hbUCPSysMem dst_mem; hbUCPMallocCached(&dst_mem, dst_stride * dst_height, 0); hbVPImage dst_img{HB_VP_IMAGE_FORMAT_Y, HB_VP_IMAGE_TYPE_U8C1, dst_width, dst_height, dst_stride, dst_mem.virAddr, dst_mem.phyAddr, nullptr, 0, 0}; // init task handle and schedule param hbUCPTaskHandle_t task_handle{nullptr}; hbUCPSchedParam sched_param; HB_UCP_INITIALIZE_SCHED_PARAM(&sched_param); sched_param.backend = HB_UCP_DSP_CORE_0; // create task hbVPCvtColor(&task_handle, &dst_img, &src_img); // submit task hbUCPSubmitTask(task_handle, &sched_param); // wait for task done hbUCPWaitTaskDone(task_handle, 0); // release task handle hbUCPReleaseTask(task_handle); // release memory hbUCPFree(&src_mem); hbUCPFree(&dst_mem);