Can not open images in windows

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Can not open images in windows

Post by ingsew »

Hello,

I wrote an application to manipute images. It's an easy little app to change size, crop, sharpen, smoothen, optimize contrast and such things. For this I use opencv too. Under Linux all things are fully functional.
But in Windows I cannot open images. To setup wxWidgets I used mostly this guide: https://github.com/PBfordev/wxpbguide , mostly because the script to modify codeblocks as described failed.

I have compiled both versions as static librarys, WxWidgets and Opencv, with success because the compiler creates only an exe file.

bitmap.LoadFile("e:\\test_image.jpg") as for wxWidgets or cvImage = imread("e:\\test_image.jpg", IMREAD_UNCHANGED); cannot open an image in windows but in Linux this is no problem.

Error message:

Can't load image from ressources from "e:\testimage.jpg" Check .rc file.

I don't understand this because i use the filedialog to open images and even hard coded, as in my example above, is not functional.

Thanks in advance.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 3955
Joined: Sun Jan 03, 2010 5:45 pm

Re: Can not open images in windows

Post by PB »

ingsew wrote: Fri Sep 15, 2023 8:18 am To setup wxWidgets I used mostly this guide: https://github.com/PBfordev/wxpbguide , mostly because the script to modify codeblocks as described failed.
Which script?
ingsew wrote: Fri Sep 15, 2023 8:18 am I have compiled both versions as static librarys, WxWidgets and Opencv, with success because the compiler creates only an exe file.
Compiler creating an exe file has nothing to do with how libraries are linked. The important thing is, does the executable run when the DLLs are not available? IMO, building OpenCV as static is rather unusual, perhaps impossible?
ingsew wrote: Fri Sep 15, 2023 8:18 am Can't load image from ressources from "e:\testimage.jpg" Check .rc file.
That is because as documented, the default value of the second parameter of wxBitmap::LoadFile() is platform specific and on Windows it is a resource, not a disk file.

So you either need to specify the bitmap type in LoadFile(), e.g., LoadFile("e:\testimage.jpg", wxBITMAP_TYPE_JPEG); or if you do not want to bother with specifying the image format, just use wxImage::LoadFile() which accepts any known bitmap format, and then create a wxBitmap from that wxImage.

BTW, do not forget to call wxInitAllImageHandlers() once in your application.
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Re: Can not open images in windows

Post by ingsew »

Thanks for the fast reply

I think you are the one who made this guide. Thank you it is a great guide and easy to understand.
Which script?
The script to setup codeblock to use wxwidgets version 3.2, in the last? section of the guide. But this was no problem because I could link the newest version.
I have compiled both versions as static librarys, WxWidgets and Opencv, with success because the compiler creates only an exe file.
You are alright, this was a mistake ofme, writing too fast without thinking. I meant I do not need any other library, neither wxWidget or OpenCv nor the stdc++ ones. All went well. I can call the app by doubleclick the exe file and it starts, but if i want to open an image (open with filedialog or dragging) the error occurs. Even if I only use the cv::imread() function without using wxWidgets LoadFile().
That is because as documented, the default value of the second parameter of wxBitmap::LoadFile() is platform specific and on Windows it is a resource, not a disk file.

So you either need to specify the bitmap type in LoadFile(), e.g., LoadFile("e:\testimage.jpg", wxBITMAP_TYPE_JPEG); or if you do not want to bother with specifying the image format, just use wxImage::LoadFile() which accepts any known bitmap format, and then create a wxBitmap from that wxImage.

BTW, do not forget to call wxInitAllImageHandlers() once in your application.
Ok, I called wxInitAllImageHandlers() in my constructor, it is because I want to use all possible image types, that wxWidgets and OpenCV supports.
wxBitmap::LoadFile() is platform specific and on Windows it is a resource, not a disk file.
I think this is not the problem in this case, but I don't know exactly, it is because the opencv::imread() function have the same problem. Maybe this is an issue because of compiling both libraries to static with g++ in windows? Is there anything to change because of the compilation Progress.

Or another question: Would it be better and easier to compile on windows with VisualStudio. As said, in Linux there were no problems. The program can handle all supported image file in every possible format even with alpha channel (therefore I had to extend OpenCv with some converting functions and also a "readable" imshow function for alpha channels). The opencv imread() function shows a problem in system.cpp (part of opencv) therefore my assumption.

Thanks for your help.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 3955
Joined: Sun Jan 03, 2010 5:45 pm

Re: Can not open images in windows

Post by PB »

ingsew wrote: Fri Sep 15, 2023 1:04 pm
wxBitmap::LoadFile() is platform specific and on Windows it is a resource, not a disk file.
I think this is not the problem in this case, but I don't know exactly
I think this exactly the problem, based both on your code using the default for the second parameter and the error message mentioning resource.

I have no idea why imread() does not work for you, are you sure the path is correct (OpenCV does not support Unicode paths)? You need to check the error message, it should give you a hint.

For example, this would be a bug

Code: Select all

bmp.LoadBitmap("e:\testimage.jpg")
the backslash must be doubled, otherwise "\t" gets converted to a tab character.

BTW, I wrote an OpenCV bitmap to wxBitmap conversion function (does not support transparency): https://github.com/PBfordev/wxopencvtest

As for using MSVS vs C::B. I believe that MSVS (not VS Code) is the best IDE on Windows. However, since the Express Edition has been abandoned, its licence may prevent some from using the Community Edition. Either way, I doubt the IDE/compiler is an issue here.

Finally, to make sure the libraries are really linked in static, I would recommend either verifying with the Depends tool or running while being sure the required DLLs (compiler, wxWidgets, OpenCV including its own dependencies) are not anywhere in the PATH where the Windows can find them.

As I wrote, linking complete OpenCV statically would probably result into an executable being several hundred megabytes large, is yours that big?
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Re: Can not open images in windows

Post by ingsew »

Hi, I will check this today,
I think this is not the problem in this case, but I don't know exactly
.
I have no idea why imread() does not work for you, are you sure the path is correct (OpenCV does not support Unicode paths)? You need to check the error message, it should give you a hint.

For example, this would be a bug

Code: Alles auswählen

bmp.LoadBitmap("e:\testimage.jpg")

the backslash must be doubled, otherwise "\t" gets converted to a tab character.
bmp.LoadBitmap("e:\testimage.jpg")
I tested this also with double backslash.
Finally, to make sure the libraries are really linked in static, I would recommend either verifying with the Depends tool or running while being sure the required DLLs (compiler, wxWidgets, OpenCV including its own dependencies) are not anywhere in the PATH where the Windows can find them.
As you surely know, to use static libs we have to compile the dependencies to static libs, therefore there are no *.dll files but .a files. and I can run the compiled exe file at an other windows pc without any other files (libs). I used your perfect guide to compile wxWidgets and I tested this also as you described. The same way I compiled opencv (the newest version) which was indeed time consuming to find out and set the right parameters.
As I wrote, linking complete OpenCV statically would probably result into an executable being several hundred megabytes large, is yours that big?
Yes it is, 222 MBytes. For the debug version until now because I compiled first the opencv debug version for testing if all things went well.
BTW, I wrote an OpenCV bitmap to wxBitmap conversion function (does not support transparency): https://github.com/PBfordev/wxopencvtest
I did the same in all directions with alpha channel. It was tricky to find out but wxWidgets saves the alpha channel after the image channels that means RGB first in image file and then the alpha channel. Opencv uses BGRA. And for testing I wrote imshowAlpha() because imshow() displays the alpha channel in black but if you have a black image you will see nothing.

Next I will change the loading process for wxWidgets, as you described, after that I give some feedback and for opencv it is more tricky because they prevent compiling to some parts because of license issues. It is not easy to find out what went wrong in this case.

Thanks for your answers und Grüsse aus dem Schwarzwald
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Re: Can not open images in windows

Post by ingsew »

Hello,

with image.loadFile the image is loaded now I have to find out the opencv issue.
I will give feedback if it is solved.
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Re: Can not open images in windows

Post by ingsew »

All is functional now, wxWidgets and OpenCv, I can build static in Windows.

Thanks for your help!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7060
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Can not open images in windows

Post by ONEEYEMAN »

Hi,
What was the solution?
Ot might help someone else...

Thank you.
ingsew
Earned a small fee
Earned a small fee
Posts: 19
Joined: Wed Jul 12, 2023 9:00 am
Location: Selva Negro

Re: Can not open images in windows

Post by ingsew »

The solution was to find out the correct settings to compile opencv with gcc on windows.
I will write a little documentation how to setup opencv using the same gcc compiler as pb used in his guide. If it is finished I will give feedback.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 3955
Joined: Sun Jan 03, 2010 5:45 pm

Re: Can not open images in windows

Post by PB »

ingsew wrote: Wed Sep 20, 2023 2:34 pm The solution was to find out the correct settings to compile opencv with gcc on windows.
Assuming you mean static linkage here; considering the OpenCV dependencies (which may have its own dependencies, just think of FFMPEG alone), this looks quite tricky.

Moreover, there may be issues with libraries used by both OpenCV (or its dependencies) and wxWidgets (e.g., libtiff, libjpeg, libpng, zlib), unlike when using a prebuilt MSYS2 package using their system versions...
Post Reply