PGM image handling [closed]

asked 2012-10-26 16:00:22 -0500

Ashok gravatar image

Is there a way to convert a non pgm format image to pgm format and then to store it in a Mat array without saving it in the file system?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2017-09-24 11:28:32.183415


Do you want to convert a non-pgm to pgm in RAM? Sounds a bit strange... What prevents you from loading a pgm with imread, and exporting it with imwrite as other format?

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-10-29 03:32:11 -0500 )edit

imdecode() and imencode() may be your friends. But note that it doesn't make much sense to store an encoded image in a Mat array() - it's useless.

sammy gravatar imagesammy ( 2012-10-29 04:05:12 -0500 )edit

May be my question is not clear. Loading a pgm with imread works just fine but imread needs the location of the pgm image in the file system. I can always load a jpg image and save it as pgm image and then load the pgm image using imread to achieve what I want. But what I am thinking here is to avoid saving the pgm image in the local file system. Instead I would like to use jpg image to convert to pgm and without saving the pgm image in the file system, I would like to load it using imread/any other method to a Mat array

Ashok gravatar imageAshok ( 2012-10-29 10:37:25 -0500 )edit

Sammy - just curious why do you think it is useless? If I have to process 1,000,000 jpg images, I dont want to waste my disk space for saving 1,000,000 pgm images.

Ashok gravatar imageAshok ( 2012-10-29 10:42:42 -0500 )edit

It's not clear what you want to do with all those PGM images. pgm is a format that you use to save images on disk. If you want to process them, you have to decode them - from jpg, pgm, bmp or any other image file format, and keep them in a matrix.So it's not clear what could you do with your images: load from jpg (and decode), then encode to png, then decode back to Mat and process? If you are more specific about what you're trying to accomplish, we could help you better.

sammy gravatar imagesammy ( 2012-10-29 12:10:45 -0500 )edit

I have this function call that takes a matrix of pgm format image. I have a jpg format image. I dont want to convert and save the jpg image to the file system as pgm and then load it to the matrix. Instead I want to convert jpg image to pgm matrix.

Ashok gravatar imageAshok ( 2012-10-31 17:06:42 -0500 )edit

I think you should take a look at the basics of image processing. There is no such thing like pgm matrix. PGM is a binary file format, and you cannot access the image pixels unless you decode it into a matrix, in the same way you decode any other file format, like jpg. It makes absolutely no sense to keep png images in memory, or try to process them. It is only for storage purposes, or (in some cases) as a way to package images before sending them through a network. And if for whatever reasons you have this function that takes a pgm blob, you have to convert your jpg images to matrix (imread or imdecode) then imencode() them to png. But that function will decode them back to process them.

sammy gravatar imagesammy ( 2012-11-01 01:32:06 -0500 )edit