2017-06-05 41 views
0

在下面我對準使用單應的兩個圖像,並減少在im_out圖像im_dst圖像的不透明度給出的程序(比如不透明性= 0.5),這樣我可以看到在im_out圖像二者im_src和im_dst圖像。但是我所得到的只是im_out圖像中黑色的im_dst圖像!具有降低的不透明度圖像對齊

import cv2 
import numpy as np 
im_src = cv2.imread('src.jpg') 
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]]) 
im_dst = cv2.imread('dst.jpg') 
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]]) 
h, status = cv2.findHomography(pts_src, pts_dst) 
img1 = np.array(im_dst , dtype=np.float) 
img2 = np.array(im_src , dtype=np.float) 
img1 /= 255.0 
# pre-multiplication 
a_channel = np.ones(img1.shape, dtype=np.float)/2.0 
im_dst = img1*a_channel 
im_src = img2*(1-a_channel) 
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0])) 
cv2.imshow("Warped Image", im_out) 
cv2.waitKey(0) 

我是新來的OpenCV,所以我可能失去了一些東西簡單。感謝幫助!

+0

看起來像是[相關](https://開頭計算器.com/a/3375291/5997596) –

+0

@AzatIbrakov我不想像Image.alpha_composite()或cv2.addWeighted()那樣簡單地疊加圖像。我也想匹配他們的同形異義詞! – Ank

+0

這正是你想要的!你只是想使用'cv2.addWeighted()'或'。alpha_composite()'在帶目標圖像的變形圖像上。 –

回答

2

嘿,我以前也seen those points

你的代碼做的是減少兩個圖像,im_dstim_src的值,但此時你的im_src褪色圖像只需移動到新的位置並顯示。相反,您應該將褪色和變形的圖像添加到目標圖像並輸出。下面是你的代碼的最後的修飾工作:

alpha = 0.5 
im_dst = img1 * alpha 
im_src = img2 * (1-alpha) 
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0])) 
im_blended = im_dst + im_out 
cv2.imshow("Blended Warped Image", im_blended) 
cv2.waitKey(0) 

但是你只分爲img1,而不是通過img2 255所以你要分割兩個第一。


但是,沒有理由手動執行此操作,因爲您必須擔心轉換圖像類型和縮放以及所有這些操作。相反,更簡單的方法是使用內置的OpenCV函數addWeighted()將兩個圖像與alpha混合一起添加。所以,你的整個代碼反而會如此之短:

import cv2 
import numpy as np 

im_src = cv2.imread('src.jpg') 
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]]) 
im_dst = cv2.imread('dst.jpg') 
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]]) 

h, status = cv2.findHomography(pts_src, pts_dst) 
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0])) 

alpha = 0.5 
beta = (1.0 - alpha) 
dst_warp_blended = cv2.addWeighted(im_dst, alpha, im_out, beta, 0.0) 

cv2.imshow('Blended destination and warped image', dst_warp_blended) 
cv2.waitKey(0) 

功能addWeighted()乘以第一圖像im_dst通過alpha,並通過beta第二圖像im_out。最後一個參數是你可以在需要的時候增加結果的正面轉變。最後,結果是飽和的,這樣高於您的數據類型允許的最大值將被截斷。這樣,您的結果與您的輸入類型相同 - 您不必轉換爲浮點型。


最後一點關於您的代碼。很多教程,包括上面鏈接的,使用findHomography()從四個匹配點獲得單應性。在這種情況下使用getPerspectiveTransform()更合適。該功能findHomography()發現基於許多匹配點的最佳單應,使用異常值拒絕方案,並隨機抽樣,以加快通過所有可能的四個一組的匹配點去。當然,它可以很好地適用於4個點的組合,但當您有四個匹配點時使用getPerspectiveTransform(),如果您有四個以上匹配點,則使用findHomography()更合理。雖然,令人煩惱的是,無論出於何種原因,您傳入getPerspectiveTransform()的點必須是np.float32。因此,這將是我對你的代碼的最終建議:

import cv2 
import numpy as np 

# Read source image. 
im_src = cv2.imread('src.jpg') 
# Four corners of the book in source image 
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]], dtype=np.float32) 

# Read destination image. 
im_dst = cv2.imread('dst.jpg') 
# Four corners of the book in destination image. 
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]], dtype=np.float32) 

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

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

# Blend the warped image and the destination image 
alpha = 0.5 
beta = (1.0 - alpha) 
dst_warp_blended = cv2.addWeighted(im_dst, alpha, warp_src, beta, 0.0) 

# Show the output 
cv2.imshow('Blended destination and warped image', dst_warp_blended) 
cv2.waitKey(0) 

這(和上面的所有其他解決方案)將產生以下圖片: Warped im_src blended with im_dst

+0

第一部分(您對我現有代碼的修改)給了我內存錯誤(可能是因爲我正在做很多分析以找到findHomography()函數的要點),但是它在該線本身給出了內存錯誤,所以我不確定。無論如何,最後的代碼工作得很好,所以我很好。感謝幫助!! – Ank

+0

此外,我想你錯過了這條線 im_out = cv2.warpPerspective(im_src,h,(im_dst.shape [1],im_dst.shape [0])) 在第二個程序 – Ank

+0

@Ank這是可能的。我只是再次測試它,它在我的最後工作正常。感謝那次的收穫,完全錯過了這一點。我將編輯答案。很高興你有它的工作!你怎麼找到這本單子的長方形單元?只是好奇。 –

相關問題