Bringing OpenCV to Xojo (again)

Unfortunately, OpenCV is C++ and you can’t use it in Xojo. However…

While it is not yet done, we started work over a year ago to wrap OpenCV 4.5 in a plain-C library so that it could be used in Xojo. In fact, there actually two projects:

OpenCV-C (The C++ → C library; currently on Github but private since it’s not done)
Xojo-OpenCV-C (also private on github as it’s incomplete)

The Xojo project aims to make OpenCV-C functionality available as native Xojo code. But to be honest, development on this stalled last year. We got pretty far though and many functions work well. There are modifications to be made to OpenCV-C, however, and we haven’t had the time or budget to do so.

Here’s a screenshot of part of the work done so far on the Xojo project:

The aim is to eventually open source both projects but we wanted to get OpenCV-C a little more finalized first. If folks are willing to work on either or both projects, let me know. I have not managed an open source project before, which is why I’ve been a little hesitant to make these public until they’re more complete.

I am realizing that I won’t have time to actively manage a project, but if there’s interest within the Xojo community in either or both then it’s probably a good idea to move ahead with this, especially if someone else is willing to deal with the day to day of managing either or both of these codebases.



That’s a very good idea and I am quite excited by this.

I agree that Open-CV-C is the main goal.

Good luck to you and all the guys working on that project.

Thanks! We really need some help on both the OpenCV-C and the Xojo projects to get this wrapped up. So anyone who might be willing to contribute is welcome to. Please message me and we can discuss access to one or both projects. For now, we’re keeping the github repos private, until things are stable enough to call it a beta, at least (I think we’re quite close).

Some of the remaining work to be done: OpenCV uses lots of overloaded functions, which C doesn’t support, so we need to convert those to their own functions, with unique names (we’ve been simply numbering the variants). The following OpenCV modules have been mostly or completely done at this point:

Image Codecs
Image Processing
Object Detect

…And most of this is also in the Xojo project, which is designed to make all of the OpenCV functions available as Xojo classes so that you don’t have to think about all the CVMat creation/destruction stuff.

We are not doing the OpenCV contrib modules at this time, and chose this first batch mainly because it covered the stuff we (my company) needs for an ongoing project. Happy to add more to that, but this is really most of the core modules for OpenCV.

At this point, my company has paid independent contractors to do the OpenCV-C work, and volunteers (namely @Antonio_Rinaldi ) and I have worked on the Xojo project. Our budget for this has basically run out. We might be able to come up with a bit more on the OpenCV-C side but because we’ve already sunk a bunch of money into it and we plan to make it open source, it’s hard to justify spending much more.

I do think it’s an important project and it really opens up a lot of possibilities for image processing in Xojo that simply don’t exist now. A little tease from the Xojo OpenCV-C manual:

1 Like

Just bumping this - we may have someone to help us with some of the OpenCV-C stuff later in the month, but I’d love to get more eyes on the Xojo project in the mean time, so we can wrap it up and make this available to everyone - for free.

Let me know if you’re interested in contributing to bringing a modern version of OpenCV to Xojo!

Hi Perry,
I don’t know if my skills would allow me to contribute a lot but with a bit of guidance I would be happy to look. As of now I could compile the Xojo “xojo-opencvc-main” project but as soon as I clic on the “Load Image” button the program quits.
Not sur what is wrong with it. Can someone help me ?

Since this thread is also getting some reaction (likes and whatnot), I wanted to tack a link to the Github Repos for OpenCV-C and Xojo-OpenCV-C onto it, for posterity.

You can read a bit more about the project in this announcement post

The OpenCV-C Repo is here. This provides a plain C wrapper for most of OpenCV 4.5.

And the Xojo-OpenCVC Repo is here. This is a Xojo project that incorporates OpenCV-C as a set of classes that handle the nuts and bolts stuff like creating and destroying mats, etc.

Hi Michel,

I haven’t had a lot of time to look at this lately, but the behavior sounds like perhaps the OpenCVC library isn’t present? Are you able to provide any more detail? system logs or anything?

I can try to take a look at it but I probably won’t be digging back into this for at least a couple more weeks.

Here is the problem I get after hitting the “Load Image” button on Xojo

Thank you for looking

The default project has a folder called “external” that contains some sample images that are used by the demo app. The first thing I’d check is that Xojo is able to access that folder because you’ll probably have a crash if you’re not passing CVCimread() a valid path.

Also - did you compile the OpenCVC Library for Windows? The default project does include a Mac dylib, but not a Windows build. As I mentioned in the other thread, it’s really just a matter of opening it in Visual Studio and compiling, pretty much, but my Windows development machine is still in storage since we moved into a new office and I won’t be able to get to that for a bit.

Oh ! That is the problem. I’m not used to Visual Studio compiling. Would you mind helping by writing how to do this ?

I’m sorry to ask again but I don’t know how to compile the OpenCVC Library for Windows.
I looked on the net but did not find anything that could guide me …
When you say “it’s really just a matter of opening it in Visual Studio and compiling”
What folder to you refer to by “it”. What is the Visual Studio command and parameters ?
Thank you

Hi Michel,

I won’t be able to look at this for a while because I don’t have access to my windows dev machine, which is in storage.

That said, Xojo OpenCVC relies on the OpenCV-C project. The basic idea is that you need to build OpenCV-C (NOT Xojo-OpenCVC), which is the C wrapper for OpenCV 4.5, into a DLL. You can do this in visual studio and there are instructions online for how to make a DLL project in VS and compile it. That compiled dll is then linked to in your Xojo project.

When you have the DLL, you’ll need to add it to the openCV module’s libOpenCVC constant. Right now it only has the mac version included, you would either replace that or add a new one for Windows with the path to your DLL.

I read and tried but failed. Given my zero experience in working with DLL, I’ll wait. Thank you for your efforts. Maybe just a last question : is there any reason why the libOpenCVC.dylib was built and not the Windows DLL ?

This is a work in progress, and the people working on it were primarily mac based. So I have a Windows DLL but it’s not thoroughly tested (though it shouldn’t be an issue) on my dev machine, but it was not added to the github repo because of that lack of testing.

I’m sorry to ask again but I fear I got it wrong. I’m on Mac with Xojo. Do I need this windows DLL ? At first I thought I did not need it. Then because of your question “Also - did you compile the OpenCVC Library for Windows?” I understood I needed it even on Mac. Now I’m not sure anymore.
Could you clarify please? Thank you

Sorry. I thought you were on windows for some reason. You do not need the dll. You do need to make sure the dylib is being included in your project however.

You do not need the Windows DLL on MacOS.

Ok thank you Perry and Tim.

One thing I discovered is that the “Architecture Build Option” for libOpenCVC.dylib in Xojo is INTEL.
My Mac is a M2_Max under Ventura 13.1.
So I just asked a friend to do exactly the same on his INTEL processor Mac and it works !!
So after that result I switched the Rosetta Option on the Information Panel of Xojo in the Finder of my M2 MacBookPro so that it would run in the Intel Mode… and it works !!
Of course if I build the app I also need to activate this option on the built App, so it’s not ideal. But at least I can move on. :slight_smile:

Is there anyone who has built the libOpenCVC.dylib for M2 processor ? Or plans to do it or tell me how to do it …

Thank you,

After some more work, the real problem is very simple, but it really took me time as I’m not an expert.
:warning: in the original project the “libOpenCVC.dylib” build option is set to “INTEL”, this got me crazy for several hours until (with the help of a friend) I saw this. It has to be set to “Universal” , specially if you own a M1/M2 (ARM) Mac Processor :warning:


1 Like