Pyplot.imsave () saves the image correctly, but cv2.imwrite () retained the same image as black

from scipy.misc import imread
from matplotlib import pyplot

import cv2
from cv2 import cv

from SRM import SRM ## Module for Statistical Regional Segmentation

im = imread("lena.png") 
im2 = cv2.imread("lena.png")
print type(im), type(im2), im.shape, im2.shape 
## Prints <type 'numpy.ndarray'> <type 'numpy.ndarray'> (120, 120, 3) (120, 120, 3)

srm = SRM(im, 256)
segmented = srm.run()

srm2 = SRM(im2, 256)
segmented2 = srm2.run()

pic = segmented/256
pic2 = segmented2/256

pyplot.imshow(pic)
pyplot.imsave("onePic.jpg", pic)

pic = pic.astype('uint8')
cv2.imwrite("onePic2.jpg", pic2)

pyplot.show()

onePic.jpggives the correct segmented image, but onePic2.jpggives a full black image. Converting the data type uint8to using pic = pic.astype('uint8')did not help. I am still giving a black image!

onePic.jpg using pyplot.imsave():

enter image description here

onePic2.jpg using cv2.imwrite():

enter image description here

Please, help!

+12
source share
2 answers

Before converting picto uint8you need to multiply it by 255 to get the correct range.

+23
source

@sansuiso, , , .

, / , .

cv2 convertScaleAbs 255.0, .

def write_image(path, img):
    # img = img*(2**16-1)
    # img = img.astype(np.uint16)
    # img = img.astype(np.uint8)
    img = cv.convertScaleAbs(img, alpha=(255.0))
    cv.imwrite(path, img)

.

0

All Articles