how to convert a jpg image to pgm format in cpp - c++

I want to covert a .jpg image to a .pgm image.The image is being obtained from a tcp socket which has live streaming by a OPENCV program.
In matlab I used the imread function to do it. How do I do it in cpp?
I am working in linux platform. Is there any function to do it in OPENCV?
can anyone help?
regards,
shiksha

Yes.
In OpenCV, you can use imread() to load the JPG image, and then use imwrite() it to the PGM image (by using the CV_IMWRITE_PXM_BINARY format flag).

please look at OpenCV documentation for HighGui library functions cv::imread and cv::imwrite.
Read the jpg using cv::imread and resave it with cv::imsave using filename with proper extension.
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imread
http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#imwrite

Related

OPENCV How can I read and use Bayer filter on a NEF image?

I'm currently using opencv C++ to process images that have file type NEF. But imread doesn't seem to work on raw images. Does anyone know how to read and use Bayer filter on a NEF type image? Thanks!
http://www.mannyphoto.com/D700D3/
here are some sample pictures.

How to convert an image from .bmp to .png in c++

Is there any open source C++ library using which I can convert an image present in .bmp format to .png . I went through libpng didn't find any way to do it there, while boost/gil looks complex don't know that can be used to do this job.
Maybe this can help you solve the problem: LodePNG
I did not see BMP in boost/GIL.. maybe take a look at Magick++ ImageMagick API
http://www.imagemagick.org/Magick++/tutorial/Magick++_tutorial.pdf
Also GDK-Pixbuf can certainly do this
I use CImg for all my image manipulation. Well written, with good documentation.

Unable to write Mat into JPG or any other image format other than BMP for OpenCV/C++

I'm unable to use the function imwrite to write a Mat into a image file. It works fine when I am trying to write it into a BMP format , but it gives me an unhandled exception when trying to write into other formats.
merge(yuv_channels,3,resImg);
imshow("Result",resImg);
imwrite("C:\\result.jpg",resImg);
The above code is my attempt for saving into jpg .
merge(yuv_channels,3,resImg);
imshow("Result",resImg);
imwrite("C:\\result.bmp",resImg);
It works fine when I changed the extension to bmp though.
Any help would be greatly appreciated .
Thank you.
The content of resImg might be of incorrect format. Only 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving. See http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite

c++ decode CCITT encoded images in pdfs

I'm trying to extract all images out of PDF files in C++. I'm stuck in decoding CCITT encoded images.
Does anyone know an opensourced code for this?
I use the ImageMagick Magick++ Library, is it possible to do the decoding with this library, too?
Thanks for your help!
CCITT is one of the encodings TIFF supports, though in a PDF file the CCITT images are probably raw data.
You can convert a raw CCITT image into a Tiff image using Fax2Tiff. It should be easy enough to work with the image once it is encoded as a Tiff.
Fax2Tiff is part of LibTiff. See LibTiff Source
Or you can append a header on it and treat it as a tiff file.

Convert YUV to lossy compression with OpenCV

How would I go about converting an image in YUV colorspace to a JPEG image?
I have a raw image data saved in a char* variable:
char* frame = (char*)camera->getFrame(); // YUV colorspace image data
I need to convert this to a JPEG image data instead. I don't want to save it to disk because I will be sending it in a stream.
OpenCV itself does not export this functionality. Cleanest is to use libjpeg for encoding. See the answers to these questions:
Convert IplImage into a JPEG without using CvSaveImage in OpenCV
OpenCV to use in memory buffers or file pointers
Check the opencv src for the file cvcolor.cpp. This has all the color conversions in it.
I suggest you modify the existing routines near this line:
/* BGR/RGB -> YCrCb */
They are almost exactly what you need for YUV encoding. if its 4:4:4 and not 4:2:2 or 4:1:1
for jpg compression
The jpg encoder and decoder are in grfmt_jpeg.cpp which happens to #include "jpeglib.h"
You can call these directly