Python制作全息金字塔

问题描述 投票:0回答:2

我正在研究全息视觉。我想将 4 张图像放置到黑屏上。

我写了这段代码:

import numpy as np
import cv2
from screeninfo import get_monitors

if __name__ == '__main__':
    screen = get_monitors()[0]
    print(screen)
    width, height = screen.width, screen.height

    image = np.zeros((height, width, 3), dtype=np.float64)
    image[:, :] = 0  # black screen
    img = cv2.imread("newBen.png")
    p = 0.25
    w = int(img.shape[1])
    h = int(img.shape[0])
    new_img = cv2.resize(img, (w, h))
    image[:h, :w] = new_img
    window_name = 'projector'
    cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
    cv2.moveWindow(window_name, screen.x - 1, screen.y - 1)
    cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
    cv2.WINDOW_FULLSCREEN)
    cv2.imshow(window_name, image)
    cv2.waitKey()
    cv2.destroyAllWindows()

但是我的形象看起来像这样。

我该如何修复它?

python opencv image-processing
2个回答
0
投票

普通 RGB 图像的

dtype
uint8
,而不是
float64

image = np.zeros((height, width, 3), dtype=np.uint8)

顺便说一句:您不必设置

image[:, :] = 0  # black screen
。这已经由
np.zeros
完成了。


0
投票

我接受了主要想法并做了一些修改。也许这不是一种优雅的方式,但它确实有效。

import numpy as np
import cv2
from screeninfo import get_monitors

window_name = 'projector'

if __name__ == '__main__':
    screen = get_monitors()[0]
    print(screen)
    width, height = screen.width, screen.height

    image = np.zeros((height, width, 3), dtype=np.uint8)
    img = cv2.imread("image.jpg")
    p = 0.35
    w = int(img.shape[1]*p)
    h = int(img.shape[0]*p)
    new_img = cv2.resize(img, (w, h))
    image[:h, :w] = new_img
    pci=(w/2,h/2)
    rotacao=cv2.getRotationMatrix2D(pci, 270, 1.0)
    r90image=cv2.rotate(new_img, cv2.ROTATE_90_CLOCKWISE)
    r180image=cv2.rotate(new_img, cv2.ROTATE_180)
    r270image=cv2.rotate(new_img, cv2.ROTATE_90_COUNTERCLOCKWISE)
    
    posicao_h=(height-2*h)
    
    posicao_h_f_1=(width/2)-h-(posicao_h/2)
    
    posicao_h_f=posicao_h_f_1+h+posicao_h    

    
    T = np.float32([[1, 0, ((width/2)-(w/2))], [0, 1, 0]])
    img_translation = cv2.warpAffine(new_img, T, (width, height))

    T = np.float32([[1, 0, (posicao_h_f)], [0, 1, ((height/2)-(w/2))]])
    img_translation1 = cv2.warpAffine(r90image, T, (width, height))
    
    T = np.float32([[1, 0, ((width/2)-(w/2))], [0, 1, (height-h)]]) 
    img_translation2 = cv2.warpAffine(r180image, T, (width, height))

    T = np.float32([[1, 0, (posicao_h_f_1)], [0, 1, ((height/2)-(w/2))]])
    img_translation3 = cv2.warpAffine(r270image, T, (width, height))

    
    cv2.namedWindow(window_name, cv2.WND_PROP_FULLSCREEN)
    cv2.moveWindow(window_name, screen.x - 1, screen.y - 1)
    cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN,
    cv2.WINDOW_FULLSCREEN)


    t4=img_translation+img_translation1+img_translation2+img_translation3
    cv2.imshow(window_name, t4)
    
    
    cv2.waitKey()
    
    cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.