0

I need to convert a Drawing.Bitmap ("_MyBitmap") to an EmguCV.Image

To do that, I use the following code:

    Dim nCVImage As New Emgu.CV.Image(Of Bgr, Byte)("c:\users\myuser\desktop\1.png")

    MsgBox("Width: " & nCVImage.Width)'this work fine!! :-)

    'Now (after the messagebox), the error is shown.        

    Stop'this is just some Stop statement for me to see to which point VB.NET executes the code when OpenCV throws the message. Stop is not hit. This means that the error must have occured in the very first line already.


    Dim m As Mat = nCVImage.Mat

    Dim imagez0 As Image(Of Gray, Byte) = m.ToImage(Of Gray, Byte)
    Dim imagez1 As Image(Of Bgr, Byte) = m.ToImage(Of Bgr, Byte)
    Dim imagez2 As Image(Of Gray, Byte) = imagez0.Canny(150, 60)

However, the first line already raises the exception "Overflow for imageSize".

_MyBitmap is perfectly fine. I can show it in a picturebox. It's size is 1200 x 1000 pixels.

Does anybody see my mistake?

Thank you!

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • IIRC, there's an overload that accepts a jagged array that contains the data bytes and one that accepts (width, height, stride, Scan0). To use the latter, see the notes in: [Analyze colors of an Image](https://stackoverflow.com/a/59102380/7444103) (in short, get the ImageData from `Image.LockBits`, it contains the properties you need. Consider the Stride, since you're creating a specific type of bitmap, so a conversion may be needed) – Jimi Dec 22 '20 at 19:54
  • @jimi What's wrong about my approach? – tmighty Dec 22 '20 at 19:56
  • I don't remember an overload of the `Emgu.CV.Image()` constructor that accepts a source `System.Drawing.Bitmap` object. You can specify the file location, though. – Jimi Dec 22 '20 at 19:59
  • @Jimi You mean like this? Dim nCVImage As New Emgu.CV.Image(Of Bgr, Byte)("c:\users\myuser\desktop\1.png") I'm asking because it throws the same error. – tmighty Dec 22 '20 at 20:05
  • 1
    @Jimi Actually, it's weird. I have augmented my posting with some observations. I'm pretty sure that the error occurs in the first line, but I don't see why or which as the image seems to have loaded fine. – tmighty Dec 22 '20 at 20:14
  • If you have an error loading a PNG from a file, I cannot say why that happens. Try another image. You can also try the `Image` format. But it's probably the image. – Jimi Dec 22 '20 at 20:16
  • BTW, check whether the image is not locked on disc by your own process. – Jimi Dec 22 '20 at 20:28
  • I have restarted my app to make sure it's not locked. Still the same error, but not immediately. Instead, it is shown at some arbitrary point of time. What a mess. :-( Doesn't sound really promising. – tmighty Dec 22 '20 at 20:33
  • If the exception appears *at some arbitrary point in time*, then you still have not identified the code that actually throws it. Do you have timers / threads / tasks running? Check those before anything else. If not, then *something* must happen. See what that *something* is, one operation at a time. – Jimi Dec 22 '20 at 20:36
  • @Jimi I have uploaded a video here: https://www.youtube.com/watch?v=AkBP800jISM As you can see, I can also not click the member-view. I guess that indicates that something went wrong in the first line already? – tmighty Dec 22 '20 at 20:55
  • Well, that's a completely different thing, it's an AccessViolationException: your Process has no access rights to that path or file. Move the Image somewhere else (the Project's \Bin\Debug\ folder, for testing). It may also be that a stream is opened using that file as source. – Jimi Dec 22 '20 at 20:57
  • @Jimi Ok, thank you, so it's a AccessViolation. I have moved to the file to the folder you suggested, but it's the same problem. I have also tried other files. – tmighty Dec 22 '20 at 21:24
  • Are you opening/accessing that file in any way somewhere else in your code? In that case, just don't – Jimi Dec 22 '20 at 21:27
  • @Jimi No, I don't. I have now even made it so that a new copy is created, and the newly created copy is being used, just to make sure it's not locked. Still the same error. – tmighty Dec 22 '20 at 21:28
  • So, you say that replacing that line with, e.g., `PictureBox.Load("[The same path]")`, you have no exceptions with that Image or any other image that EmguCv.Image cannot load? If that's the case, it might be that you're not referencing the correct EmguCv assemblies. You need `Emgu.Cv`, `Emgu.Cv.Platforms.Netframework` and `Emgu.Cv.runtime.windows`, plus Imports of `Emgu.CV, Emgu.CV.CvEnum, Emgu.CV.Structure, Emgu.CV.Util`, in case of a standard WinForm Project. See that you have installed an EmguCv version that supports your Framework version. (Unless you have a very broken anti-virus) – Jimi Dec 22 '20 at 21:50

1 Answers1

1

Found a solution:

"cvextern.dll" was missing.

What I did was to download

https://vorboss.dl.sourceforge.net/project/emgucv/emgucv/4.4.0/libemgucv-windesktop-4.4.0.4099.exe

and install it as it was suggested.

(not sure how I got there)

I compiled the following file:

C:\Emgu\emgucv-windesktop 4.4.0.4099\Solution\Windows.Desktop\Emgu.CV.Example.sln

Then I copied \x86\cvextern.dll to my \Debug\x86 folder.

Also, I upgraded my project to framework 4.7.2 and used Nuget's Emug.CV v4.4.0.4099.

Not sure why it would not put cvextern.dll into my app dir automatically. Totally unnerving!!!

tmighty
  • 10,734
  • 21
  • 104
  • 218