Animesh Pandey
Animesh Pandey

Reputation: 6018

Corrupted image being saved by cv.SaveImage() in opencv

import sys, Image, scipy, cv2, numpy
from scipy.misc import imread
from cv2 import cv
from SRM import SRM

def ndarrayToIplImage (source):
"""Conversion of ndarray to iplimage"""
    image = cv.CreateImageHeader((source.shape[1], source.shape[0]), cv.IPL_DEPTH_8U, 3)
    cv.SetData(image, source.tostring(), source.dtype.itemsize * 3 * source.shape[1])
    return image

"""Main Program"""

filename = "snap.jpeg"
Q = 64

im = imread(filename)
name = filename[:-4]

img = Image.fromarray(im)

if img.size[0] > 200 or img.size[1] > 200:
    ratio = img.size[0]/img.size[1]
    size = int(ratio*200), 200
    img = numpy.array(img.resize(size, Image.ANTIALIAS))

    srm = SRM(img, Q)
    srm.initialization()
    srm.segmentation()
    classes, map = srm.map()

    """Converting ndarray to PIL Image to iplimage"""
    pil_img = Image.fromarray(map)
    cv_img = cv.CreateImageHeader(pil_img.size, cv.IPL_DEPTH_8U, 3)
    cv.SetData(cv_img, pil_img.tostring(), pil_img.size[0]*3)
    print type(cv_img) ##prints <type 'cv2.cv.iplimage'>

    """Using ndarrayToIplImage function also gives the same error!"""

    """
    cv_img if of type iplimage but still gives error while using cv.ShowImage()
    or cv.SaveImage().

    There is no error displayed. Just the console hangs...

    """

I am using the SRM (Statistical Region Merging) Package available at this page.

I have just changed the example program given in the package. I had to convert the type returned by the SRM package functions to iplimage. There is no error in using the package but somewhere in using opencv functions.

This is the image that is saved after the console closes after hanging. It used cv.SaveImage().

After getting error

I tried cv2.imwrite() and I got this as the result:

enter image description here

This is the image that should have been saved. I used scipy.misc.imsave('image.jpg', map) to save this.

Should have been!

Upvotes: 0

Views: 1832

Answers (1)

EmanuelOverflow
EmanuelOverflow

Reputation: 348

Why do you use IplImage and PIL? SRM library read numpy array and you get a numpy array from cv2.imread(image), then if you need to resize yuor image you can use opencv function cv2.resize(...). Finally you can save an image with opencv with cv2.imwrite(...) your code should appear like this:

import sys, cv2, numpy
from SRM import SRM

"""Main Program"""

filename = "snap.jpeg"
Q = 64

img = cv2.imread(filename)
name = filename[:-4]


if img.shape[0] > 200 or img.shape[1] > 200:
    ratio = img.shape[0] * 1. / img.shape[1]
    size = (int(ratio * 200), 200)

    img = cv2.resize(img, size, interpolation=cv2.INTER_LANCZOS4)

    srm = SRM(img, Q)

    srm.initialization()
    srm.segmentation()
    classes, srmMap = srm.map() # Map is a python function, use different variable name
    srmMap = srmMap.astype('uint8') # or you can try other opencv supported type 
    # I suppose that srmMap is your image returned as numpy array
    cv2.imwrite('name.jpeg', srmMap)
    # or
    cv2.imshow('image', srmMap)
    cv2.waitKey(0)

Upvotes: 2

Related Questions