Java & OpenCV can't convert RGB .gif image to GRAY


I want to convert pictures using Java and OpenCV from RGB to GRAY

All extensions images work correctly and I take the gray image,

just if I make .GIF image (not moving) it's give me this error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor

the java code :

Mat scrImg = Highgui.imread(path);
Mat dstImg = new Mat(scrImg.rows(),scrImg.cols(),scrImg.type());
Imgproc.cvtColor(scrImg, dstImg, Imgproc.COLOR_RGB2GRAY);

private static BufferedImage Mat2BufferedImage(Mat matrix){
	BufferedImage bimOut;
	int type;
	if(matrix.channels() == 1)
		type = BufferedImage.TYPE_BYTE_GRAY;
		type = BufferedImage.TYPE_3BYTE_BGR;
	int dataLength = matrix.channels()*matrix.cols()*matrix.rows();
	byte [] buffer = new byte[dataLength];
	bimOut = new BufferedImage(matrix.cols(),matrix.rows(),type);
	final byte[] bimPixels = ((DataBufferByte) bimOut.getRaster().getDataBuffer()).getData();
	System.arraycopy(buffer, 0, bimPixels, 0, buffer.length);
	return bimOut;


Show source
| java   | eclipse   | opencv   | rgb   | gif   2017-01-03 17:01 1 Answers

Answers to Java & OpenCV can't convert RGB .gif image to GRAY ( 1 )

  1. 2017-01-03 17:01

    According to the official documentation

    Currently, the following file formats are supported:

    • Windows bitmaps - *.bmp, *.dib (always supported)
    • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
    • JPEG 2000 files - *.jp2 (see the Notes section)
    • Portable Network Graphics - *.png (see the Notes section)
    • WebP - *.webp (see the Notes section)
    • Portable image format - *.pbm, *.pgm, *.ppm *.pxm, *.pnm (always supported)
    • Sun rasters - *.sr, *.ras (always supported)
    • TIFF files - *.tiff, *.tif (see the Notes section)
    • OpenEXR Image files - *.exr (see the Notes section)
    • Radiance HDR - *.hdr, *.pic (always supported)
    • Raster and Vector geospatial data supported by Gdal (see the Notes section)

    Apparently support is not included Because gif is a proprietary format.

Leave a reply to - Java & OpenCV can't convert RGB .gif image to GRAY

◀ Go back