Announcing Xojo OpenCVC - OpenCV 4.5 for Xojo

I’ve been playing with this a bit and it’s pretty cool!

I have a question: when feeding the functions it seems to require a FolderItem EG:
images.Add openCV.Codecs.imread(pLogo.NativePath, openCV.ImReadModes.Unchanged)

Is there any way to do this from a picture object in memory instead of writing a file?

Thanks!

1 Like

@Perry_Paolantonio I’m trying to get this going on linux. The linux section of the README is “TO DO”. After getting v4.5.5 installed on by development machine the xojo desktop project is still reporting “Could not load CVCimread from openCv.” I can see the library in the declares is LibOpenCVC that only has a .dylib for mac but not .so for linux.

Is the source code for LibOpenCVC available or could you run a build for linux?

Yes. Though it’s possible it’s not yet implemented in Xojo-OpenCVC. I would need to look, but the code I have for that is on the PC that’s in storage. Hopefully in a couple weeks I can do it. I know it works because before we embarked on Xojo-OpenCVC, we used the OpenCVC library to work with images we had in memory, in order to test some basic scaling speeds. I just need to locate that.

One of the issues with OpenCV is that it uses a lot of overloaded C++ functions, but C doesn’t support that. So we need to take each of those functions and make a duplicate of it with a different name, then expose that to the user. I’m not sure if that’s the issue here, but I can take a look once we get the other machine back up and running with all that code on it.

We didn’t do a linux build at the time we built this. if you’re able to get it up and running, it would be great if you could document the steps and we’ll include it in the project!

1 Like

Thanks! I’ve built C apps before but not C++. Should be able to figure it out. I’m not sure how well Linux will go at embedding opencv in the library if that’s what the mac one does. I’ve created a linux-support branch and will do a pull request with some updated docs once I’m happy with it. :slight_smile:

So OpenCVC is a C app. It includes the C++ code from OpenCV, but you are essentially making a C library that acts as the glue between Xojo and OpenCV. I haven’t tried it and my linux programming experience is limited so couldn’t say for sure but I’d think it would be pretty straightforward. On Windows and Mac, last time I compiled, the IDEs presented more challenges than the actual compiling. It basically just worked.

1 Like

Last night I was thinking about this before I fell asleep. It’s been a while so I’m a bit rusty, but it occured to me that you don’t need an OpenCVC function to do this - it’s built into the CVCMat class.

Basically a Mat is the core data type for holding images (and lots of other stuff) in OpenCV. You need to get your image into a Mat. imread() is one way to do that, from a file. But if you have the image data in memory you should be able to just create a new CVCMat instance using that as the source image.

If the image is in a memoryblock, look at the “image1” method, which handles that situation. the “image” method basically uses imread() to load from a file.

the Image1() seems to return a picture but I cant figure out how to take a picture and return a CVCMat.

you’re correct. Stung once again by being half asleep when I come up with an idea and not having enough coffee when I tell someone about it!

Try imdecode() - this will take an image from a memory buffer and return a CVCMat - basically you pass it a memoryblock with your image data

1 Like

EDIT: Nevermind this works but is pretty slow for me?

dim mb as new MemoryBlock(pic1.Width * pic1.Height * 4)
mb = pic1.ToData(picture.Formats.BMP)
dim test as openCV.CVCMat
test = openCV.Codecs.imDecode(mb, opencv.ImReadModes.Grayscale)

Try removing the BMP part:

mb = pic1.ToData(picture.Formats.BMP)

If I recall correctly, you want the raw data going into the CVCMat, not a BMP with its associated headers. Again, working from memory here!

The way we did it before was to use raw image data from our camera, which has no headers when it comes off the camera and is in memory. You have to explicitly format it before writing to the disk, but I remember that we had to pass the raw image into a memoryblock and then pass that into OpenCV when I was initially testing.

Sorry if I’m sending you all over the place with this. I wish I had the PC set up with all that code so I could take a look!

No, its all good. It seems to be working with the above code but takes around 10 seconds to process.

hmm. that’s strange. Our experience with anything OpenCV is that its practically instantaneous, even with huge files on relatively modest machines. I was testing with 14k x 10k 16bit RGB images - so, massive files.

Can you isolate where the slowdown is?

Seems to be this line:
test = openCV.Codecs.imDecode(mb, opencv.ImReadModes.Grayscale)

I’ll be patient, not an urgent need, i’m glad we have access to this in xojo now :slight_smile:

huh. Is it a very large file?

No, 1024 x 768 or so.

Got a linux shared library built. libOpenCVC.so. I inspected it with nm -C libOpenCVC.so | grep " T " and noted 256 functions available CVCadsdiff…CVCwatershed. After adding the library the xojo debugger hard crashes as soon as I call a declare function. Did you have any issues with this?

I’ll try build the demo app included on OpenCV-C a bit later to see if it’s a problem with the library build or Xojo.

By running the app from the terminal I get the following. I wonder if I’m missing something here.

/home/user/GitRepositories/xojo-opencvc/DebugopenCVDesktop/DebugopenCVDesktop: symbol lookup error: /home/user/GitRepositories/OpenCV-C/build/libOpenCVC.so: undefined symbol: _ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi

@Perry_Paolantonio Got it working on Linux! It was just some library linking required. As I have it now, the system doesn’t package opencv with the library so libOpenCVC.so is only 124Kib. This means you are required to install opencv separately on Linux. openCV has a couple of other package dependencies so it might be best to let users install it themselves. What do you think?

Sorry. I forgot to reply to this - I think that makes sense.

Did you find a way to make it fast ? Interresting topic for me too.