Hello ,
I am trying to add crop and resize to my application using NvBufferTransform , but it won’t work as expected. I am not sure how to use it correctly.
I am using Xavier Nx with jetpack 4.4.1. and editing nvarguscamerasrc.cpp file.
processV4L2Fd function definition is here.
bool processV4L2Fd(int32_t fd, std::string &filePath, int jpegQuality, int width, int height)
{
unsigned long size = (width * height * 3 / 2);
unsigned char *buffer = new unsigned char[m_OutputBufferSize];
std::ofstream *outputFile = new std::ofstream(filePath.c_str());
if (outputFile)
{
m_JpegEncoder->encodeFromFd(fd, JCS_YCbCr, &buffer, size, jpegQuality);
outputFile->write((char *)buffer, size);
delete outputFile;
std::cout << "Jpg writed as :" << filePath << std::endl;
}
delete buffer;
return true;
}
I added crop_resize and crop .At first try it works correct but second try it is corrupted and all following transformations are corrupted.
Original image size is 4128 x 3008
I want to crop image From left 0 top 500 w 3840 h 2160
I want to resized cropped image From left 0 top 500 w 3840 h 2160 to 640x480
14_crop
14_crop_resize
14_org
767_crop
767_crop_resize
767_org
if (src->frameInfo->fd < 0)
{
src->frameInfo->fd = iNativeBuffer->createNvBuffer(streamSize,
NvBufferColorFormat_YUV420,
NvBufferLayout_BlockLinear,
rotation);
if (!src->silent)
CONSUMER_PRINT("Acquired Frame. %d\n", src->frameInfo->fd);
}
else if (iNativeBuffer->copyToNvBuffer(src->frameInfo->fd, rotation) != STATUS_OK)
{
ORIGINATE_ERROR("IImageNativeBuffer not supported by Image.");
}
if (!src->silent)
CONSUMER_PRINT("Acquired Frame: %llu, time %llu\n",
static_cast<unsigned long long>(iFrame->getNumber()),
static_cast<unsigned long long>(iFrame->getTime()));
src->frameInfo->frameNum = iFrame->getNumber();
src->frameInfo->frameTime = iFrame->getTime();
if (takePhotoFlag)
{
int selectedJpegQuality = src->jpegQuality;
if (jpegQuality != -1)
selectedJpegQuality = jpegQuality;
if (takePhotoWidth != -1 && takePhotoHeight != -1) // If Resize Enable
{
if (src->frameInfoTakePhotoResize->fd < 0)
{
NvBufferCreateParams params;
params.width = 640;
params.height = 480;
params.payloadType = NvBufferPayload_SurfArray;
params.layout = NvBufferLayout_BlockLinear;
params.colorFormat = NvBufferColorFormat_YUV420;
params.nvbuf_tag = NvBufferTag_VIDEO_CONVERT;
NvBufferCreateEx(&src->frameInfoTakePhotoResize->fd, ¶ms);
}
NvBufferRect src_rect, dest_rect;
NvBufferTransformParams transform_params;
memset(&transform_params, 0, sizeof(transform_params));
src_rect.left = 0;
src_rect.top = 500;
src_rect.width = 3840;
src_rect.height = 2160;
transform_params.transform_flag = NVBUFFER_TRANSFORM_CROP_SRC;
transform_params.transform_flip = NvBufferTransform_None;
transform_params.transform_filter = NvBufferTransform_Filter_Smart;
transform_params.src_rect = src_rect;
int ret = NvBufferTransform(src->frameInfo->fd, src->frameInfoTakePhotoResize->fd, &transform_params);
std::string name = "images/" + std::to_string(src->frameInfo->frameNum) + "_crop_resize.jpg";
processV4L2Fd(src->frameInfoTakePhotoResize->fd, name, selectedJpegQuality, 640, 480);
}
if (src->cropEnable)
{
if (src->frameInfoTakePhoto->fd < 0)
{
NvBufferCreateParams params;
params.width = 3840;
params.height = 2160;
params.payloadType = NvBufferPayload_SurfArray;
params.layout = NvBufferLayout_BlockLinear;
params.colorFormat = NvBufferColorFormat_YUV420;
params.nvbuf_tag = NvBufferTag_VIDEO_CONVERT;
NvBufferCreateEx(&src->frameInfoTakePhoto->fd, ¶ms);
}
NvBufferRect src_rect, dest_rect;
NvBufferTransformParams transform_params;
memset(&transform_params, 0, sizeof(transform_params));
src_rect.left = 0;
src_rect.top = 500;
src_rect.width = 3840;
src_rect.height = 2160;
transform_params.transform_flag = NVBUFFER_TRANSFORM_CROP_SRC;
transform_params.transform_flip = NvBufferTransform_None;
transform_params.transform_filter = NvBufferTransform_Filter_Smart;
transform_params.src_rect = src_rect;
int ret = NvBufferTransform(src->frameInfo->fd, src->frameInfoTakePhoto->fd, &transform_params);
// processV4L2FdStore(src->frameInfoTakePhoto->fd, selectedJpegQuality);
std::string name = "images/" + std::to_string(src->frameInfo->frameNum) + "_crop.jpg";
processV4L2Fd(src->frameInfoTakePhoto->fd, name, selectedJpegQuality, 3840, 2160);
}
std::string name = "images/" + std::to_string(src->frameInfo->frameNum) + "_org.jpg";
processV4L2Fd(src->frameInfo->fd, name, selectedJpegQuality, src->width, src->height);
takePhotoFlag.exchange(false);
}```