我有一個代碼:
import cv2
import numpy
background = numpy.zeros((1080, 1920, 3))
img = numpy.ones((255, 465, 3))
offset = numpy.array((12, 12))
x_offset = offset[0]
y_offset = offset[1]
img_w = img.shape[1]
img_h = img.shape[0]
background_w = background.shape[1]
background_h = background.shape[0]
x = x_offset
y = y_offset
for i in range(0, 16):
background[y:y img.shape[0], x:x img.shape[1]] = img
x = img_w x_offset
if x > background_w - img_w:
x = x_offset
y = img_h y_offset
cv2.imshow("test", background)
cv2.imwrite("background.jpg", background)
cv2.waitKey(0)
cv2.destroyAllWindows()
生成像這樣的網格:

所以cv2.imshow顯示了網格,但cv2.imwrite由于某種原因只寫了初始的黑色背景而不是網格。如何解決?
uj5u.com熱心網友回復:
您需要縮放顏色通道:
cv2.imwrite("background.jpg", background)
cv2.imwrite("background.jpg", background * 255)
或者,您可以使用以下型別創建“白色”影像uint8:
img = numpy.ones((255, 465, 3))
img = numpy.ones((255, 465, 3), dtype = numpy.uint8) * 255
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/439570.html
