Created
April 5, 2021 05:55
-
-
Save kaskichandrakant/e96b6abf63a9af25bfd3296a5a113df3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
cap = cv2.VideoCapture(0) | |
imgTarget=cv2.imread("wheel.jpg") | |
advtImg = cv2.imread("wheeladvt.jpg") | |
hT,wT,cT = imgTarget.shape | |
advtImg = cv2.resize(advtImg,(wT,hT)) | |
orb = cv2.ORB_create(nfeatures=1000) | |
kp1,desc1 = orb.detectAndCompute(imgTarget,None) | |
while True: | |
success,imgWebcam = cap.read() | |
imgAug = imgWebcam.copy() | |
kp2,desc2 = orb.detectAndCompute(imgWebcam,None) | |
bf = cv2.BFMatcher() | |
matches=bf.knnMatch(desc1,desc2,k=2) | |
good=[] | |
for m,n in matches: | |
if(m.distance < 0.75 *n.distance): | |
good.append(m) | |
# imgFeatchers = cv2.drawMatches(imgTarget,kp1,imgWebcam,kp2,good,None,flags=2) | |
detected = False | |
if len(good)>20: | |
detected=True | |
srcPoints = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1,1,2) | |
dstPoints = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1,1,2) | |
matrix,mask = cv2.findHomography(srcPoints,dstPoints,cv2.RANSAC,5) | |
pts = np.float32([[0,0],[0,hT],[wT,hT],[wT,0]]).reshape(-1,1,2) | |
dst = cv2.perspectiveTransform(pts,matrix) | |
img2 = cv2.polylines(imgWebcam,[np.int32(dst)],True,(255,0,255),3) | |
imgWarp= cv2.warpPerspective(advtImg,matrix,(imgWebcam.shape[1],imgWebcam.shape[0])) | |
maskNew=np.zeros((imgWebcam.shape[0],imgWebcam.shape[1]),np.uint8) | |
cv2.fillPoly(maskNew,[np.int32(dst)],(255,255,255)) | |
maskInv = cv2.bitwise_not(maskNew) | |
imgAug = cv2.bitwise_and(imgAug,imgAug,mask=maskInv) | |
imgAug = cv2.bitwise_or(imgWarp,imgAug) | |
cv2.imshow('Aurgumented',imgAug) | |
if detected: | |
cv2.imshow('Aurgumented',imgAug) | |
else: | |
cv2.imshow('origianal',imgWebcam) | |
cv2.waitKey(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment