Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

BMP: Drawing or photograph?

by myforwik (Acolyte)
on Oct 29, 2012 at 10:04 UTC ( #1001335=perlquestion: print w/ replies, xml ) Need Help??
myforwik has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I was wondering if there exists a perl module that takes a BMP image and recommends if it should be converted to PNG or JPEG by analyzing the picture and figuring out if its a drawing or a photograph.

I have tried a using edge detection filters, but this is very slow and requires a lot of computation.

P.S. - What is the best module for converting BMP's to PNG/JPEG?

Thanks, Mike

Comment on BMP: Drawing or photograph?
Re: BMP: Drawing or photograph?
by BrowserUk (Pope) on Oct 29, 2012 at 10:10 UTC
    if it should be converted to PNG or JPEG by analyzing the picture and figuring out if its a drawing or a photograph.

    Which would you use for which? And why?

    Both formats can represent either. JPG can be more compact; but is lossy. PNG is non-lossy and does a pretty good job of compaction commensurate with not throwing away information.

    For my taste, png is a far better format for either; but "best" depends completely upon your criteria?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: BMP: Drawing or photograph?
by zentara (Archbishop) on Oct 29, 2012 at 10:28 UTC
    PNG is better if there is written text, JPG is better for drawings and images. The best converter is ImageMagick.
    # using the ImageMagick c program system( "convert my.bmp my png");
    or using the Perl module, here is a generic conversion routine:
    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $imgfile = shift; # your BMP file my $image = Image::Magick->new(); $image->Read($imgfile); my $blob = $image->ImageToBlob(); # make in memory image #and to convert my $output = Image::Magick->new(magick=>'jpg'); # set your output for +mat $output->BlobToImage( $blob ); $output->Resize(geometry=>'160x120'); $output->Write( "$0.jpg" ); #or if you want to write to stdout #binmode STDOUT; #$output->Write('jpg:-');

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: BMP: Drawing or photograph?
by syphilis (Canon) on Oct 29, 2012 at 10:44 UTC
    What is the best module for converting BMP's to PNG/JPEG?

    Does it have to be a module ?
    I think I would first try PngUtil's bmptopng or NetPBM's bmptopnm, pnmtojpeg and pnmtopng.

    Cheers,
    Rob
Re: BMP: Drawing or photograph?
by tospo (Hermit) on Oct 29, 2012 at 11:11 UTC
    I guess the main reason for doing this would be to deal with images that have text in them which you probably want to convert to PNG rather than JPEG, right? If that's the case then how about feeding the image into some sort of OCR software to figure out if there is any recognizable text in there? I have no idea if that's practical or not, just an idea.
      OCR is notorious for not working right, unless you know the fonts being used before hand. Also throw on top of that the Text is on an image, that further screws up software detection.

      What I might do is just try a few, and look at both the png and jpg versions and see which is more pleasant when trying read the text. Maybe the fonts are big enough that it dosn't matter much. Jpgs are definitely smaller, and even simple pngs can be quite large. Its a size versus clarity tradeoff.

      If you have the time and inclination, you might approach this scientifically, and convert a series of bmp images to corresponding jpg's and png's. Then use your edge detection software to compare their overlays. That ought to be proof enough of the exact differences. Doing it with PDL might be the easiest way to do that comparison.


      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
Re: BMP: Drawing or photograph?
by Corion (Pope) on Oct 29, 2012 at 11:12 UTC

    Personally, I find ImageMagick to be hard to install, and find Imager far more pleasing and transparent in its API.

      ... but if you use gnu don't underestimate this great tool and give it a chance. Imagemagick is very easy to install (in linux at least), script oriented, and very powerful

        I have used ImageMagick, and it is very powerful. But its methods and parameters are underdocumented IMO, and there are subtle changes in behaviour between minor versions that make me prefer Imager.

        Of course, the external helpers for ImageMagick make it highly convenient when one needs to convert from Postscript (or PDF?) to a raster image. But that's not my use case.

Re: BMP: Drawing or photograph?
by daxim (Chaplain) on Oct 29, 2012 at 11:32 UTC
    I am not a computer vision expert. Here's how I would do it:

    Load some representative target images into Mathematica or similar program and try out various filters. Start with transformations related to edge-detection or finding continous areas of colour and brightness. Visit http://mathematica.stackexchange.com/questions/tagged/image-processing if you need help.

    When you have found a filter or combination of filters that correctly recognise to a high confidence whether an image is suitable for JPEG compression or not, recreate them in Perl. Binding to various imaging toolkits written in C are available, so this shouldn't be too complicated. The filter (pipeline) simply returns a boolean, use PNG or JPEG output format accordingly.

Re: BMP: Drawing or photograph?
by Anonymous Monk on Oct 29, 2012 at 12:35 UTC
    Just pull a histogram on a random sample of the pixels, then measure the standard deviation. If it's low, i.e. if the histogram is well-distributed between highs and lows, and especially among colors, it's a photograph. If it's high, and tends toward white or black pixels, it's a drawing. Asssemble a test set of a few hundred of both types for algorithm-training purposes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001335]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2014-12-20 11:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls