Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert JPEG image in memory to Imaq Image

I need to convert and display a JPEG image in LabVIEW l that I continously capture from Canon 1D Mark III camera using the Canon SDK.  We have a dll wrapper for the SDK that downloads the images into memory so we have a pointer to the jpeg image stream and its size.  Now we just need to convert the JPEG image to the IMAQ image type.

 

The problem we're running into is that it appears the JPEG - IMAQ conversion functions are buried inside the IMAQ ReadFile vi.  Unless I'm missing something simple is saving the JPEG file and reading it back with IMAQ readfile the only way to easily get a JPEG image into an IMAQ image?

 

I also downloaded some code from the forums called IMAQ JPEG decode that converts a binary JPEG stream, but it has a couple problems including a big memory leak and swapping red and blue color planes.

 

The JPEG image is 1024x680 32bit RGB.

 

0 Kudos
Message 1 of 11
(13,702 Views)

Unfortunately, that is the only way to do it - write a file and read it back.  The string decoder works well for grayscale images, but not color, as you discovered.

 

I have been bugging the Vision R&D team to add this simple functionality, but they don't see the need.  Perhaps if you submit a feature request that would help.

 

Bruce

Bruce Ammons
Ammons Engineering
Message 2 of 11
(13,696 Views)
There may be one way to do it.  They have the feature to flatten an image to a string, and it uses JPEG internally (I think).  You might be able to figure out which part of the string is the JPEG image, and how to insert your JPEG image in its place.  This would be challenging, though.
Bruce Ammons
Ammons Engineering
0 Kudos
Message 3 of 11
(13,694 Views)

Bruce thanks for the reply.

 

It seems like the R&D team has done this before in the IMAQ_JPG.dll that is called by IMAQ Jpeg Decode vi's.  Does anyone know who wrote that dll? 

0 Kudos
Message 4 of 11
(13,686 Views)

Hi,

 

Why do not use suggestion from Bruce? Quick and dirty test shows that It works in general:

 

 

The only problem that I haven't found where in header the length of the jpeg part was stored. So, I create "dummy" jpeg which is guaranteed bigger than original, and then perform jpeg part replacement and pad with zeroes after that (otherwise you will get error from Unflatten). In attachment two examples - one for 8 bit and another one for rgb image both are OK for play.

 

Andrey.

PS

Otherwise (personally) I will never use such hack in commercial application ;).

Message Edited by Andrey Dmitriev on 02-11-2009 07:03 PM
Download All
Message 5 of 11
(13,676 Views)

Andrey - impressive work!  I had the idea, but I wasn't sure how to implement it.  Your method appears to work well.

 

Somebody in NI R&D wrote those JPEG decode routines many years ago.  I don't think he is in R&D anymore.  I chatted with him about them long ago, but I can't recall his name at the moment.  They were actually designed so that images could be easily transferred from a remote RT target back to a host PC, but they work great for many other uses.  I like to be able to pack a large number of images into a single file, which reads and writes much faster than individual images.

 

Bruce

 

 

Bruce Ammons
Ammons Engineering
0 Kudos
Message 6 of 11
(13,668 Views)

Andrey that is impressive.  I will look at incorporating in my code.  It just seems like for the $$$ spent for the toolkit it should be a built in function.

 

Bruce - I submitted a feature request.

0 Kudos
Message 7 of 11
(13,662 Views)

I posted an idea request for a function to convert strings back to images.  Please vote for it at:

 

http://forums.ni.com/t5/LabVIEW-Idea-Exchange/Add-function-to-convert-string-to-image/idi-p/1104573#...

 

Thanks,

 

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 8 of 11
(11,972 Views)

I've had a similar task of converting JPEG image stream into a common 32-bit RGB array, that could be passed to IMAQ VIs. As I'm working in 64-bit version of LabVIEW, I ended at Windows Imaging Component (WIC) and its proxy functions (thanks to this thread). So, I have rewritten that C code in pure LabVIEW with the help of CLFN blocks (ole32, windowscodecs and shlwapi calls). Here's how to use it:

2018-04-27_11-10-47.jpg

Works fine in both 32- and 64-bit LabVIEW and doesn't need any non-standard external libraries or .NET assemblies. Moreover WIC is able to read not only JPEG, but any other widespread formats, such as PNG, BMP etc.

Try it and tell me, how it goes for you.

Message 9 of 11
(8,094 Views)
Just to chip in: If you are working with a slow hard drive / or want to do this 24/7, use a RAM-Disk. That is a little piece of software that basically takes a slice of RAM and creates a virtual drive that looks like it is part of your file system. So LabVIEW (and any other program) sees a standard disk drive, but it's actually RAM: Fast and doesn't damage drives with continous write operations. Not the most elegant solution, but sometimes you have to do ugly things to get a job done.
0 Kudos
Message 10 of 11
(7,914 Views)