opencv 如何用白色填充图像?

neskvpey  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(137)

我试图创建一个旋转的图像是白色的,但这不起作用。图像总是变成黑色。
我怎么能做到这一点?

def rotate_image(image):
    # Convert the image to a NumPy array
    image_array = np.array(image)

    # Set the fill color (RGB format)
    fill_color = (255, 255, 255)  # White color

    # Define the rotation angle
    rotation_angle = random.randint(1, 360)

    # Perform rotation using OpenCV
    rows, cols = image_array.shape[:2]
    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), rotation_angle, 1)
    rotated_array = cv2.warpAffine(image_array, M, (cols, rows), borderValue=fill_color)
    
    # Convert the rotated array back to an image
    rotated_image = Image.fromarray(rotated_array)

    # Save the rotated image as a PNG file
    rotated_image.save("test1.png")
    
    return rotated_image

字符串


的数据
下面是我使用它的代码:

def get_dataset_batch(batch_size=2):
    flip = False
    base_image = Image.open("star_map_base.png")
    train_A = []
    train_B = []
    
    for i in range(0, batch_size):
        if flip:
            turbulence_size = random.choice([1, 2, 3, 4])
            turbulence_image = Image.open("turbulence.jpg")
            x = random.randint(0, 4096 - IMG_SIZE * turbulence_size)
            y = random.randint(0, 2136 - IMG_SIZE * turbulence_size)
            crop_actual_rect = (x, y, x + IMG_SIZE * turbulence_size, y + IMG_SIZE * turbulence_size)
            cropped_actual = turbulence_image.crop(crop_actual_rect)
            cropped_actual = cropped_actual.resize((IMG_SIZE, IMG_SIZE))
        else:
            helix_size = random.choice([1, 2, 3, 4, 5, 6, 7])
            helix_image = Image.open("helix_bw_base.jpg")
            x = random.randint(0, 4096 - IMG_SIZE * helix_size)
            y = random.randint(0, 4096 - IMG_SIZE * helix_size)
            crop_actual_rect = (x, y, x + IMG_SIZE * helix_size, y + IMG_SIZE * helix_size)
            cropped_actual = helix_image.crop(crop_actual_rect)
            cropped_actual = cropped_actual.resize((IMG_SIZE, IMG_SIZE))
        
        flip = not flip

        cropped_actual = cropped_actual.convert('LA')

        star_overlayed = cropped_actual
        star_overlayed = rotate_image(star_overlayed)
        star_overlayed = star_overlayed.convert('L')
        star_overlayed = Image.fromarray(transform(image=np.asarray(star_overlayed))["image"] / 1)
        star_overlayed = star_overlayed.convert('LA')

        ca = star_overlayed.copy()
        ca = ca.convert('L')
        
        base_image = base_image.convert('RGBA')
        star_overlayed = star_overlayed.convert('RGBA')
        overlaid_image = overlay_images(star_overlayed, base_image)
        overlaid_image = Image.fromarray(overlaid_image)
        
        star_overlayed = overlaid_image.convert('L')
        a = np.asarray(ca, dtype="float32").reshape(1, IMG_SIZE, IMG_SIZE, 1) / 512
        b = np.asarray(star_overlayed, dtype="float32").reshape(1, IMG_SIZE, IMG_SIZE, 1) / 512
        train_A.append(a)
        train_B.append(b)
    
    return train_A, train_B

31moq8wy

31moq8wy1#

验证您的源映像是否确实包含有用的信息。它似乎已经完全黑了。
顺便说一下,你的源图像似乎有一个第四通道,一个阿尔法通道。不完整地给出borderValue会导致剩余值为0。这就是为什么超出范围的像素变成透明的原因。
简单地设置borderValue是不够的。您还需要设置warpAffine()的边框 * 模式 * 标志。
BORDER_CONSTANT使用您提供的borderValue
BORDER_REPLICATE对任何目标像素使用最近的有效源像素。
您还应该坚持使用一个库,而不是将两者混合使用。OpenCV完全能够阅读和写入图像文件。要获得带有alpha通道的图像,您只需传递正确的标志。

5anewei6

5anewei62#

尝试在应用旋转之前用白色**泛色填充矩形。使用图形/图像的中心作为种子点:

# Read the image:
imagePath = "D://opencvImages//blackRect.png"
inputImage = cv2.imread(imagePath)

# Set seed:
random.seed(42)

# Get image dimensions:
(imageHeight, imageWidth) = inputImage.shape[:2]

# Get center coordinates:
xCenter = int(0.5 * imageWidth)
yCenter = int(0.5 * imageHeight)

# Flood fill at center:
center = (xCenter, yCenter)
whiteColor = (255, 255, 255)
cv2.floodFill(inputImage, None, center, whiteColor)

# Show filled image:
cv2.imshow("Filled Image", inputImage)
cv2.waitKey(0)

# Define the rotation angle
rotation_angle = random.randint(1, 360)

# Perform rotation using OpenCV
M = cv2.getRotationMatrix2D((imageWidth / 2, imageHeight / 2), rotation_angle, 1)
rotatedImage = cv2.warpAffine(inputImage, M, (imageWidth, imageHeight), borderValue=(0, 0, 0))

# Show rotated image:
cv2.imshow("Rotated Image", rotatedImage)
cv2.waitKey(0)

字符串
这是进度-输入图像(黑色矩形)->填充图像(白色矩形)->旋转图像:x1c 0d1x的数据

相关问题