我的目标是使用Python(OpenCV,numpy,Pillow)将正常图像转换为X射线图像,我已经完成了其中的一部分,我已经修改了图像,但我不能使其成为X射线。
我已经尝试了多种方法来完成这个特定的任务,但它并没有按照我想要的方式My result进行,这是我希望看到的Something like this
这是我目前为止的代码。
import cv2
import numpy as np
from PIL import Image
import os
def apply_xray_effect(input_image_path, output_image_path):
# Read the input image using Pillow (PIL)
pil_image = Image.open(input_image_path)
# Convert PIL image to NumPy array
img_np = np.array(pil_image)
# Apply a custom transformation to simulate X-ray effect
# You can customize this transformation to achieve the desired effect
xray_effect = img_np.copy()
# Apply brightness and contrast adjustments
alpha = 0.3 # Adjust contrast (1.0 is the original contrast)
beta = -140 # Adjust brightness (0 is the original brightness)
xray_effect = cv2.convertScaleAbs(xray_effect, alpha=alpha, beta=beta)
# Apply a pseudo-color map to give it a blueish tone
colormap = cv2.applyColorMap(xray_effect, cv2.COLORMAP_BONE)
x = 200 # X-coordinate of the top-left corner of the ROI
y = 250 # Y-coordinate of the top-left corner of the ROI
width = 300 # Width of the ROI
height = 250 # Height of the ROI
# Convert PIL image to NumPy array
pil_image_np = np.array(pil_image)
# Create a mask for the ROI
mask = np.zeros_like(pil_image_np)
cv2.rectangle(mask, (x, y), (x + width, y + height), (255, 255, 255), thickness=-1) # Fill the ROI with white
# Apply the mask to the image to extract the ROI
roi = cv2.bitwise_and(pil_image_np, mask)
# Change the color of the ROI (e.g., from blue to green)
roi[:, :, 0] = 0 # Set the blue channel to 0 (remove blue)
roi[:, :, 1] = 0 # Set the green channel to 255 (max green)
roi[:, :, 2] = 0 # Set the red channel to 0 (remove red)
# Save the resulting X-ray-like image
cv2.imwrite(output_image_path, colormap)
if __name__ == "__main__":
input_image_path = r"C:\Users\Ayad\OneDrive\Desktop\girl.jpeg" # Replace with your input image file path
output_directory = r"C:\Users\Ayad\OneDrive\Desktop" # Output directory path
output_xray_image_path = os.path.join(output_directory, "changedImg_xray.png")
apply_xray_effect(input_image_path, output_xray_image_path)
# Display the images
xray_image = cv2.imread(output_xray_image_path)
cv2.imshow("X-ray Image", xray_image)
cv2.waitKey(0) # Wait until a key is pressed to close the windows
cv2.destroyAllWindows()
1条答案
按热度按时间w3nuxt5m1#
使你的示例图像看起来更接近x射线示例的一个步骤是反转它。例如,这将使牙齿之间的间隙变黑,而不是白色。你可以通过在这里添加
xray_effect = ~xray_effect
来实现这一点:更多反转图像的方法是mentioned in this answer
正如其他评论者所指出的,您无法将普通图像转换为X射线。我假设你只是想要美学上的“x射线效果”,就像一个过滤器。