Cara menggunakan python cv2 create image

Yang baru cv2 antarmuka untuk Python terintegrasi numpy array ke dalam kerangka OpenCV, yang membuat operasi lebih sederhana karena diwakili dengan array multidimensi sederhana. Misalnya, pertanyaan Anda adalah dijawab dengan:

import cv2  # Not actually necessary if you just want to create an image.
import numpy as np
blank_image = np.zeros((height,width,3), np.uint8)

Ini menginisialisasi gambar RGB yang hanya hitam. Sekarang, misalnya, jika Anda ingin mengatur bagian kiri gambar menjadi biru dan bagian kanan menjadi hijau, Anda dapat melakukannya dengan mudah:

blank_image[:,0:width//2] = (255,0,0)      # (B, G, R)
blank_image[:,width//2:width] = (0,255,0)

Jika Anda ingin menyelamatkan diri dari banyak masalah di masa depan, serta harus mengajukan pertanyaan seperti ini, saya akan sangat menyarankan menggunakan cv2 antarmuka daripada yang lebih lama cv satu. Saya membuat perubahan baru-baru ini dan tidak pernah melihat ke belakang. Anda dapat membaca lebih lanjut tentang cv2 di OpenCV Change Logs .

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in current working directory.

    Syntax: cv2.imwrite(filename, image)

    Parameters:
    filename: A string representing the file name. The filename must include image format like .jpg, .png, etc.
    image: It is the image that is to be saved.

    Return Value: It returns true if image is saved successfully.

    Example #1:

    import cv2

    import os

    image_path = r'C:\Users\Rajnish\Desktop\GeeksforGeeks\geeks.png'

    directory = r'C:\Users\Rajnish\Desktop\GeeksforGeeks'

    img = cv2.imread(image_path)

    os.chdir(directory)

    print("Before saving image:")  

    print(os.listdir(directory))  

    filename = 'savedImage.jpg'

    cv2.imwrite(filename, img)

    print("After saving image:")  

    print(os.listdir(directory))

    print('Successfully saved')

    Output:

    Before saving image:
    ['geeks.png']
    After saving image:
    ['geeks.png', 'savedImage.jpg']
    Successfully saved
    

    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    OpenCV is a library of programming functions mainly aimed at real-time computer vision. cv2.transpose() method is used to transpose a 2D array. The function cv::transpose rotate the image 90 degrees Counter clockwise.

    Syntax: cv2.cv.transpose( src[, dst] )

    Parameters:
    src: It is the image whose matrix is to be transposed.
    dst: It is the output image of the same size and depth as src image. It is an optional parameter.

    Return Value: It returns an image.

    Image used for all the below examples:

    Cara menggunakan python cv2 create image

    Example:

    import cv2

    path = r'C:\Users\user\Desktop\geeks14.png'

    src = cv2.imread(path)

    window_name = 'Image'

    image = cv2.transpose(src)

    cv2.imshow(window_name, image)

    cv2.waitKey(0)

    Output:

    Cara menggunakan python cv2 create image