import cv2
import keras
from matplotlib import pyplot as plt

def writeImage(file, x):
    cv2.imwrite(file, x)

def showImage(data):
    plt.imshow(data)
    plt.show()

def composite(X, Y, out):
    writeImage(X, "/tmp/A.png")
    writeImage(Y, "/tmp/B.png")
    A = cv2.imread("/tmp/A.png", 0)
    B = cv2.imread("/tmp/B.png", 0)
    C = np.dstack((A, B))
    writeImage(C, out)
    return C

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
showImage(x_train[0])
showImage(x_train[5])
composite(x_train[0], x_train[5], "/tmp/ImageCompositeExample.jpg")

Saya mencoba menggabungkan dua gambar menjadi satu seperti Bagaimana saya bisa membuat komposit dua gambar di OpenCV dengan Python?. Tapi saya dapatkan:

SystemError: <built-in function imwrite> returned NULL without setting an error 

---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
<ipython-input-15-f533fdd71378> in <module>
----> 1 composite(x_train[0], x_train[5], "/tmp/ImageCompositeExample.jpg")

<ipython-input-11-ff434edc5914> in composite(X, Y, out)
      1 def composite(X, Y, out):
----> 2     writeImage(X, "/tmp/A.png")
      3     writeImage(Y, "/tmp/B.png")
      4     A = cv2.imread("/tmp/A.png", 0)
      5     B = cv2.imread("/tmp/B.png", 0)

<ipython-input-9-5ef37533556e> in writeImage(file, x)
      1 def writeImage(file, x):
----> 2     cv2.imwrite(file, x)

SystemError: <built-in function imwrite> returned NULL without setting an error
0
SmallChess 5 April 2021, 02:21

2 jawaban

Jawaban Terbaik

Ada kesalahan yang lulus argumen ke fungsi timage

def composite(X, Y, out):

    #writeImage(X, "/tmp/A.png") <-- error
    #writeImage(Y, "/tmp/B.png") <-- error

    writeImage("/tmp/A.png",X)
    writeImage("/tmp/B.png",Y)
    
    
    A = cv2.imread("/tmp/A.png", 0)
    B = cv2.imread("/tmp/B.png", 0)
    C = np.dstack((A, B))

    # def writeImage(file, x):  how is defined 
    # writeImage(C, out)  <--- error

    writeImage(out, C)
    return C
    
1
Mario Abbruscato 5 April 2021, 00:10

Tukar argumen dalam panggilan fungsi, Anda melewati (x, file) sekarang.

writeImage("/tmp/A.png", X)
writeImage("/tmp/B.png", Y)
1
SuperStormer 4 April 2021, 23:34