Boost GIL image constructors - c++

I'm currently trying to figure out how to use the Generic Image Library included in Boost. Right now, I just want to use the library to store pixel data and use the Image IO to write PNGs. I'm having trouble understanding just how to set up the object however.
The hpp says
image(const point_t& dimensions,
std::size_t alignment=1) : _memory(0), _align(alignment) {
allocate_and_default_construct(dimensions);
}
but I cannot find any references to point_t except a type_def for view_t::point_t to point_t.
Also, the tutorial found with the GIL seems to only include writing filters and generic algorithms, and thus each function example they provide has a source image view, from which they take the dimensions.
Am I going about this the wrong way? Or is there something I've missed completely?
Thanks in advance
Edit: I don't know if anyone cares, or has read this, but for the record, I just used the boost interleaved image function to create a PNG. It's not exactly the same solution, but it works for my applications.

it sounds like you solved your problem in the meantime, but just for the record... here are some pointers to information about your problem:
First of all you may have missed the second constructor of boost::gil::image, which offers explicit access to the horizontal and vertical dimensions without the need of the point_t:
image(x_coord_t width, y_coord_t height,
std::size_t alignment=0,
const Alloc alloc_in = Alloc()) : _memory(0), _align_in_bytes(alignment), _alloc(alloc_in) {
allocate_and_default_construct(point_t(width,height));
}
point_t will most likely refer to the point2 class template defined in boost/gil/utilities.hpp.
In general you should check the complete documentation of Boost GIL for all questions not mentioned in the tutorial. For a deeper understanding of the library it is absolutely necessary to get familiar with the Design Guide and the Doxygen Documentation.

Related

Custom submodules in pytorch / libtorch C++

Full disclosure, I asked this same question on the PyTorch forums about a few days ago and got no reply, so this is technically a repost, but I believe it's still a good question, because I've been unable to find an answer anywhere online. Here goes:
Can you show an example of using register_module with a custom module?
The only examples I’ve found online are registering linear layers or convolutional layers as the submodules.
I tried to write my own module and register it with another module and I couldn’t get it to work.
My IDE is telling me no instance of overloaded function "MyModel::register_module" matches the argument list -- argument types are: (const char [14], TreeEmbedding)
(TreeEmbedding is the name of another struct I made which extends torch::nn::Module.)
Am I missing something? An example of this would be very helpful.
Edit: Additional context follows below.
I have a header file "model.h" which contains the following:
struct TreeEmbedding : torch::nn::Module {
TreeEmbedding();
torch::Tensor forward(Graph tree);
};
struct MyModel : torch::nn::Module{
size_t embeddingSize;
TreeEmbedding treeEmbedding;
MyModel(size_t embeddingSize=10);
torch::Tensor forward(std::vector<Graph> clauses, std::vector<Graph> contexts);
};
I also have a cpp file "model.cpp" which contains the following:
MyModel::MyModel(size_t embeddingSize) :
embeddingSize(embeddingSize)
{
treeEmbedding = register_module("treeEmbedding", TreeEmbedding{});
}
This setup still has the same error as above. The code in the documentation does work (using built-in components like linear layers), but using a custom module does not. After tracking down torch::nn::Linear, it looks as though that is a ModuleHolder (Whatever that is...)
Thanks,
Jack
I will accept a better answer if anyone can provide more details, but just in case anyone's wondering, I thought I would put up the little information I was able to find:
register_module takes in a string as its first argument and its second argument can either be a ModuleHolder (I don't know what this is...) or alternatively it can be a shared_ptr to your module. So here's my example:
treeEmbedding = register_module<TreeEmbedding>("treeEmbedding", make_shared<TreeEmbedding>());
This seemed to work for me so far.

Drawing custom 3d shapes

I am attempting to create a custom object using Cinder C++ on windows with Visual Studio. I am hoping to find a solution that allows you to point to a object with BatchRef object, and which can be used in the same way as any other BatchRef.
I have already tried searching through the official websites tutorials and documentation, and while it does an excellent job of listing all the classes, functions, etc, it does a exceedingly slim job of covering the usage of most of those, with only the return type and arguments listed.
Ideally, we could call the custom shape something like myShape and it could be used in the following manner in my App::draw() override: (with mShader already defined someplace else)
gl::BatchRef bRef;
gl::pushModelMatrix();
bRef = gl::Batch::create( myShape() , mShader );
bRef -> draw();
gl::popModelMatrix();
If the documentation has instructions for this, feel free to point me that way. I was unable to find it, but that does not mean that it does not exist.

Migrating to vtk6: Is it not necessary to Update() (anymore)?

Migrating some code from VTK 5.10 to 6.1, I have several code pieces like this:
vtkSmartPointer<vtkImageData> img = vtkSmartPointer<vtkImageData>::New();
// ... initialize img somehow, e.g. copy from other image:
img->DeepCopy(otherImg);
img->SetInformation(otherImg->getInformation());
// the problematical statement:
img->Update();
At the call to Update(), the compiler now complains that there isn't such a function (anymore).
The migration site from VTK doesn't really tell me too much about that - I believe this falls into the section Removal of Data Objects’ Dependency on the Pipeline , but as it's no Algorithm which is filling my image, I can't call update on an algorithm.
Similar goes for custom-filled vtkPolyData objects.
My question now is: Is the call to Update not necessary (anymore?), can I just remove it? Or by what would I need to replace it?
I have to say I'm relatively new to vtk, so if there's something fundamentally simple that I'm missing I'd be glad if you could point it out to me!
I think you've been meaning to call Modified() on your image rather than Update().
Apparently they already answered your question on VTK:
http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Removal_of_Update

How do you control a player character in Bullet Physics?

I am not sure how you are supposed to control a player character in Bullet. The methods that I read were to use the provided btKinematicCharacterController. I also saw methods that use btDynamicCharacterController from the demos. However, in the manual it is stated that kinematic controller has several outstanding issues. Is this still the preferred path? If so, are there any tutorials or documentations for this? All I found are snippets of code from the demo, and the usage of controllers with Ogre, which I do not use.
If this is not the path that should be tread, then someone point me to the correct solution. I am new to bullet and would like a straightforward, easy solution. What I currently have is hacked together bits of a btKinematicCharacterController.
This is the code I used to set up the controller:
playerShape = new btCapsuleShape(0.25, 1);
ghostObject= new btPairCachingGhostObject();
ghostObject->setWorldTransform(btTransform(btQuaternion(0,0,0,1),btVector3(0,20,0)));
physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
ghostObject->setCollisionShape(playerShape);
ghostObject->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT);
controller = new btKinematicCharacterController(ghostObject,playerShape,0.5);
physics.getWorld()->addCollisionObject(ghostObject,btBroadphaseProxy::CharacterFilter, btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter);
physics.getWorld()->addAction(controller);
This is the code I use to access the controller's position:
trans = controller->getGhostObject()->getWorldTransform();
camPosition.z = trans.getOrigin().z();
camPosition.y = trans.getOrigin().y()+0.5;
camPosition.x = trans.getOrigin().x();
The way I control it is through setWalkDirection() and jump() (if canJump() is true).
The issue right now is that the character spazzes out a little, then drops through the static floor. Clearly this is not intended. Is this due to the lack of a rigid body? How does one integrate that?
Actually, now it just falls as it should, but then slowly sinks through the floor.
I have moved this line to be right after the dynamic world is created
physics.getWorld()->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
It is now this:
broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
I am also using a .bullet file imported from blender, if that is relevant.
The issue was with the bullet file, which has since been fixed(the collision boxes weren't working). However, I still experience jitteryness, unable to step up occasionally, instant step down from to high a height, and other issues.
My answer to this question here tells you what worked well for me and apparently also for the person who asked.
Avoid ground collision with Bullet
The character controller implementations in bullet are very "basic" unfortunately.
To get good character controller, you'll need to invest this much.

What is the correct way of creating a small list of cv::Mat and iterating over it?

I am new to C++ and I am trying to create a list of cv::Mat.
This could allocate quite a lot of memory, but I only have around ten small Mat's to load in the list.
I made this code, but not sure why it is not working.
void Utils::getFramesFromVideo(std::string file,std::list<cv::Mat>& listMatOutput) {
cv::VideoCapture* videoCapture = new cv::VideoCapture(file);
bool hasImage = false;
cvNamedWindow("WhileReading", 1);
do {
cv::Mat frame;
hasImage = videoCapture->read(frame);
if (hasImage) {
listMatOutput.push_back(frame);
imshow("WhileReading", frame);
waitKey(0);//give some time to see the frame (debug)
}
} while (hasImage);
cvNamedWindow("AfterReading", 2);
for (std::list<Mat>::const_iterator iterator = listMatOutput.begin();
iterator != listMatOutput.end(); iterator++) {
imshow("AfterReading", *iterator);
waitKey(0);//give some time to see the frame (debug)
}
videoCapture->release();
}
The first loading is displaying the frames correctly, but in the second window (AfterReading) the image is black with red stripes.
Could someone please give some advice?
The list format is an STL container, meaning that you've got some things to keep in mind to work with it. push_back() is the preferred method of adding instances to the container, much like using an iterator is the preferred method of accessing the elements of the list. If you try to directly set an element of the list to a cv::Mat() you're working on, then a) you need to know exactly what sort of wrapping the list is doing to each instance, so you can do it properly yourself, and b) you're defeating the purpose of using an STL container in the first place, which is to abstract away the details of the list.
You don't necessarily need to use the frame.clone() call in your code, as this creates a deep copy of the image and takes up precious resources. I know that I've used std::vector<cv::Mat> in the past without having to make deep copies for each element in the vector, so I assume that you should be able to pass the Mat itself to a list. Consult the C++ documentation on STL lists.
Another thing you might consider, if low memory usage and speed of access through the list is a concern, and your number of images is low, is a list of image pointers. Keep your images stored as individual cv::Mat instances, and make your list of type std::list<cv::Mat*>, passing the handle of the image to the push_back() call. Of course, with this method, your images will not be 'thread safe' because they will be stored in one place, but called and worked on from another. I hope this shed a little light on your inquiry.
I tried all sort of things, like changing to pointers or vectors and checking for code optimizations on GCC. Then after trying to clone cv::Mat it worked:
listMatOutput.push_back(frame.clone());
Would be glad if anyone could tell me why and suggest a better way of doing, so I can choose a better answer then my own.