Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I have following code for image validation:

C#
sprintf(imageFileName_jp2, "%s\\image_%03d.jp2", fileMenu.m_logPath, tmp_no);
cvImage = imread(imageFileName_jp2, IMREAD_COLOR);
if (cvImage.data == NULL)
    continue;


If an image is not suitable to be read (when some image data is imperfect due to communication problem), I want to ignore the image.

But, imread() takes time and it makes the code slow.

Is there another method to validate the image?

Thank you.
Posted
Updated 19-Jul-15 21:21pm
v2

1 solution

I don't think that there is any other way.
The problem is that you need to perform the whole process of loading and decoding to be sure that the image file is valid, even sanity checks are not enough because you are using JPEG compressed images. In such images a wrong byte can be detected only during decompression.
The only suggestion I can give you is to make two fast sanity check before trying to load the image:
1. check file dimension, if it is shorter than a reasonable length you can discard it
2. Read the first 2 bytes of the image file, they contain a JPEG SOI marker = 0xFFD8
3. Then a JFIF-APP0 marker segment:
Field       Size (bytes)     Description
APP0 marker       2             FF E0
Length            2             Length of segment excluding APP0 marker
Identifier        5             4A 46 49 46 00 = "JFIF" in ASCII, terminated by a null byte
JFIF version      2             First byte for major version, second byte for minor version (01 02 for 1.02)
Density units     1             Units for the following pixel density fields
                                  00 : No units; width:height pixel aspect ratio = Xdensity:Ydensity
                                  01 : Pixels per inch (2.54 cm)
                                  02 : Pixels per centimeter
Xdensity          2             Horizontal pixel density. Must not be zero.
Ydensity          2             Vertical pixel density. Must not be zero.
Xthumbnail        1             Horizontal pixel count of the following embedded RGB thumbnail. May be zero.
Ythumbnail        1             Vertical pixel count of the following embedded RGB thumbnail. May be zero.
Thumbnail data    3 × n         Uncompressed 24 bit RGB (8 bits per color channel) raster thumbnail data in the order R0, G0, B0, ... Rn, Gn, Bn; with n = Xthumbnail × Ythumbnail.

Also here you can make a check for reasonable values.

For more detail look here[^].
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900