Skip to content

Commit

Permalink
Updated NvTransform to do scale, crop and color transform (#176)
Browse files Browse the repository at this point in the history
* Updated NvTransform to do scale, crop and transform ,
Ability to pass filter as props

* Passing correct value in metadata

---------

Co-authored-by: Mradul Dubey <mradul@apra.in>
  • Loading branch information
yashrajsapra and mraduldubey authored Jul 19, 2023
1 parent 3aaa81c commit 7aa90fe
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
36 changes: 36 additions & 0 deletions base/include/NvTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,56 @@
class NvTransformProps : public ModuleProps
{
public:
enum NvTransformFilter
{
NEAREST=0, // transform filter nearest.
BILINEAR, // transform filter bilinear.
TAP_5, // transform filter 5 tap.
TAP_10, // transform filter 10 tap.
SMART, // transform filter smart.
NICEST // transform filter nicest.
};

NvTransformProps(ImageMetadata::ImageType _imageType) : top(0) , left(0) , width(0) , height(0)
{
imageType = _imageType;
scaleHeight = 1;
scaleWidth = 1;
filterType = NvTransformFilter::SMART;
}
NvTransformProps(ImageMetadata::ImageType _imageType, int _width, int _height) : top(0) , left(0) , width(_width) , height(_height)
{
imageType = _imageType;
scaleHeight = 1;
scaleWidth = 1;
filterType = NvTransformFilter::SMART;
}
NvTransformProps(ImageMetadata::ImageType _imageType, int _width, int _height, float _scaleWidth, float _scaleHeight, NvTransformFilter _filterType) : top(0) , left(0) , width(_width) , height(_height)
{
imageType = _imageType;
filterType = _filterType;
scaleHeight = _scaleHeight;
scaleWidth = _scaleWidth;
}
NvTransformProps(ImageMetadata::ImageType _imageType, int _width, int _height, int _top , int _left) : top(_top) , left(_left) , width(_width) , height(_height)
{
imageType = _imageType;
scaleHeight = 1;
scaleWidth = 1;
filterType = NvTransformFilter::SMART;
}
NvTransformProps(ImageMetadata::ImageType _imageType, int _width, int _height, int _top , int _left, float _scaleWidth, float _scaleHeight, NvTransformFilter _filterType) : top(_top) , left(_left) , width(_width) , height(_height)
{
imageType = _imageType;
filterType = _filterType;
scaleHeight = _scaleHeight;
scaleWidth = _scaleWidth;
}
ImageMetadata::ImageType imageType;
int top,left,width,height;
NvTransformFilter filterType;
float scaleWidth, scaleHeight; // scaleWidth and scaleHeight are factor of width and height ,
//1 means no change 0.5 means half of actual dimension ,2 means twice of actual dimension
};

class NvTransform : public Module
Expand Down
31 changes: 30 additions & 1 deletion base/src/NvTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,30 @@ class NvTransform::Detail
src_rect.height = _props.height;

memset(&transParams, 0, sizeof(transParams));
transParams.transform_filter = NvBufferTransform_Filter_Smart;
switch(_props.filterType)
{
case NvTransformProps::NvTransformFilter::NEAREST:
transParams.transform_filter = NvBufferTransform_Filter_Nearest;
break;
case NvTransformProps::NvTransformFilter::BILINEAR:
transParams.transform_filter = NvBufferTransform_Filter_Bilinear;
break;
case NvTransformProps::NvTransformFilter::TAP_5:
transParams.transform_filter = NvBufferTransform_Filter_5_Tap;
break;
case NvTransformProps::NvTransformFilter::TAP_10:
transParams.transform_filter = NvBufferTransform_Filter_10_Tap;
break;
case NvTransformProps::NvTransformFilter::SMART:
transParams.transform_filter = NvBufferTransform_Filter_Smart;
break;
case NvTransformProps::NvTransformFilter::NICEST:
transParams.transform_filter = NvBufferTransform_Filter_Nicest;
break;
default:
throw AIPException(AIP_FATAL, "Filter Not Supported");
}

if (src_rect.width != 0)
{
transParams.src_rect = src_rect;
Expand Down Expand Up @@ -216,6 +239,12 @@ void NvTransform::setMetadata(framemetadata_sp &metadata)
width = mDetail->props.width;
height = mDetail->props.height;
}
if(mDetail->props.scaleHeight != 0 && mDetail->props.scaleWidth != 0)
{
width = width * mDetail->props.scaleWidth;
height = height * mDetail->props.scaleHeight;
}


DMAAllocator::setMetadata(mDetail->outputMetadata, width, height, mDetail->props.imageType);
}
Expand Down
45 changes: 45 additions & 0 deletions base/test/nvtransform_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "RawImageMetadata.h"
#include "DMAFDWrapper.h"
#include "DMAUtils.h"
#include "NvArgusCamera.h"

#include <chrono>

Expand Down Expand Up @@ -126,4 +127,48 @@ BOOST_AUTO_TEST_CASE(fdtest)
DMAUtils::freeCudaPtr(eglInImage,&pInResource, eglDisplay);
}

BOOST_AUTO_TEST_CASE(crop, *boost::unit_test::disabled())
{
Logger::setLogLevel(boost::log::trivial::severity_level::info);

NvArgusCameraProps sourceProps(1280, 720, 0);
auto source = boost::shared_ptr<Module>(new NvArgusCamera(sourceProps));

auto nv_transform = boost::shared_ptr<Module>(new NvTransform(NvTransformProps(ImageMetadata::RGBA, 400, 400)));
source->setNext(nv_transform);

PipeLine p("test");
p.appendModule(source);
BOOST_TEST(p.init());

p.run_all_threaded();
boost::this_thread::sleep_for(boost::chrono::seconds(10));
p.stop();
p.term();
p.wait_for_all();
}

BOOST_AUTO_TEST_CASE(cropAndScale, *boost::unit_test::disabled())
{
Logger::setLogLevel(boost::log::trivial::severity_level::info);

NvV4L2CameraProps sourceProps(1920, 1080, 10);
auto source = boost::shared_ptr<Module>(new NvV4L2Camera(sourceProps));

float scaleHeight = 1;
float scaleWidth = 0.5;
auto nv_transform = boost::shared_ptr<Module>(new NvTransform(NvTransformProps(ImageMetadata::RGBA, 400, 400, scaleWidth, scaleHeight, NvTransformProps::NvTransformFilter::BILINEAR)));
source->setNext(nv_transform);

PipeLine p("test");
p.appendModule(source);
BOOST_TEST(p.init());

p.run_all_threaded();
boost::this_thread::sleep_for(boost::chrono::seconds(10));
p.stop();
p.term();
p.wait_for_all();
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 7aa90fe

Please sign in to comment.