[FIXED] question about python and opencv for merge images

Issue

I wrote this code by python and opencv I have 2 images (first is an image from football match 36.jpg) :

src="https://i.stack.imgur.com/6WPJd.jpg" alt="36.jpg" />

and (second is pitch.png an image (Lines of football field (Red Color)) with png format = without white background) :

pitch.png

With this code , I selected 4 coordinate points in both of the 2 images (4 corners of the right penalty area) and then, with ( cv2.warpPerspective ) and showing it , we can show that first image from (Top View) as below:

top view

My question is, what changes do I need to make in my code so that the red colored lines from the second image appear on the first image in the same way as the images below (drawn in the Paint app): 

desired

this is my code :

import cv2
import numpy as np

if __name__ == '__main__' :

 # Read source image.
 im_src = cv2.imread('c:/36.jpg')
 # Four corners of penalty area in first image
 pts_src = np.array([[314, 108], [693, 108], [903, 493],[311, 490]])

 # Read destination image.
 im_dst = cv2.imread('c:pitch.png')
 # Four corners of right penalty area in pitch image.
 pts_dst = np.array([[480, 76],[569, 76],[569, 292],[480, 292]])

 # Calculate Homography
 h, status = cv2.findHomography(pts_src, pts_dst)

 # Warp source image to destination based on homography
 im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))

 # Display images
 cv2.imshow("Source Image", im_src)
 cv2.imshow("Destination Image", im_dst)
 cv2.imshow("Warped Source Image", im_out)

 cv2.waitKey(0)

Solution

Swap your source and destination images and points. Then, warp the source image:

im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]), borderValue=[255,255,255])

and add this code

mask = im_out[:,:,0] < 100

im_out_overlapped = im_dst.copy()
im_out_overlapped[mask] = [0,0,255]

Overlapped warped image



Answered By - user8190410
Answer Checked By - Robin (sql.easybugfix Admin)

Related Posts

Post a Comment