Как вставить небольшое изображение в другое большое изображение в центре Python с изображением, обработанным с помощью OpenCV? - PullRequest
0 голосов
/ 03 июля 2019

Я изучаю OpenCV и попытался вставить маленькое изображение поверх большого.Но это показало ошибку, так как оба изображения должны иметь одинаковый размер.Я также пытался следовать предложенному предложению ( Как вставить изображение на большее изображение с помощью Подушки? ) и ( Как скомпоновать изображение на другое изображение с PIL в Python? )

   import cv2 as cv
   from scipy import ndimage

   img1 = cv.imread('Please put your file name')

   top_left_x = min([x1,x2,x3,x4])
   top_left_y = min([y1,y2,y3,y4])
   bot_right_x = max([x1,x2,x3,x4])
   bot_right_y = max([y1,y2,y3,y4])

   y_right =bot_right_y + 1
   x_right =bot_right_x + 1

  cropped = img[top_left_y: y_right, top_left_x: x_right]

  rotate = ndimage.rotate(cropped, ang)

Окончательное изображение должно быть в центре.

Ответы [ 2 ]

1 голос
/ 03 июля 2019

Это чистое PIL решение: -

from PIL import Image

img1 = Image.open(r"Source_Image_path")

# The values used to crop the original image (will form a bbox)
x1, y1, x2, y2 = 10, 10, 400, 400

# The angle at which the cropped Image must be rotated
angle = 50

# cropping the original image 
img = img1.crop((x1, y1, x2, y2))

# Firstly converting the Image mode to RGBA, and then rotating it
img = img.convert("RGBA").rotate(angle, resample=Image.BICUBIC)

# calibrating the bbox for the beginning and end position of the cropped image in the original image 
# i.e the cropped image should lie in the center of the original image
x1 = int(.5 * img1.size[0]) - int(.5 * img.size[0])
y1 = int(.5 * img1.size[1]) - int(.5 * img.size[1])
x2 = int(.5 * img1.size[0]) + int(.5 * img.size[0])
y2 = int(.5 * img1.size[1]) + int(.5 * img.size[1])

# pasting the cropped image over the original image, guided by the transparency mask of cropped image
img1.paste(img, box=(x1, y1, x2, y2), mask=img)

# converting the final image into its original color mode, and then saving it
img1.convert(img1.mode).save("Destination_path")

ВХОД ИЗОБРАЖЕНИЯ: -

enter image description here

ИЗОБРАЖЕНИЕ ВЫХОДА: -

enter image description here

Сам код не требует пояснений, но вы можете удивитьсяпочему мы конвертируем обрезанное изображение туда и обратно в RGBA.Причина этого заключается в том, что если мы поворачиваем не альфа-изображение в PIL, мы получаем черные полосы / края на изображении, где значения пикселей больше не существуют ( читайте этот вопрос, чтобы узнать больше ).Но если мы сделаем то же самое для альфа-изображения, то есть передадим альфа-изображение с помощью rotate(), тогда значения пустых пикселей станут полностью прозрачными (или альфа = 0).

0 голосов
/ 05 июля 2019

Хотя ни одно из доступных решений не работало для OpenCV и даже не решило мою проблему.Но после нескольких модификаций я могу решить эту проблему в OpenCV.

import cv2
img = cv2.imread('Please put your file name')

# Setting the parameters
ang = 47
top_left_x = min([12,42,53,11])
top_left_y = min([41,56,75,20])
bot_right_x = max([12,42,53,11])
bot_right_y = max([41,56,75,20])

y_right =bot_right_y + 1
x_right =bot_right_x + 1

# Cropping the image
cropped_img = img[top_left_y: y_right, top_left_x: x_right]

###########  Rotating the image ##########
# First setting the centre and roatation angle parameter. 
# To rotate sub-image from the centre of the original image
rows,cols = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2),ang,1)

# Setting the size with original image to resolve size issue
rotated_img = cv2.warpAffine(cropped_img,M,(img.shape[1],img.shape[0]))

# Pasting the rotated image on original image
# The original image will be in the background with transparency 0.3
# The sub-image will be pasted above the original image with transparency 0.7
img = cv2.addWeighted(img, 0.3, rotated_img, 0.7, 0)

# Showing the image
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('output_image.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Это не самый подходящий ответ, но он может служить цели для многих из нас.

...