iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 25
0
Software Development

【錢不夠買ps的我,只好用OpenCV來修圖了!】系列 第 25

【沒錢ps,我用OpenCV!】Day 25 - 綜合運用4,用 OpenCV 來把圖片p到各種奇怪的地方吧! 透視投影 cv2.warpPerspective, merge two images

先來看看今天的結果圖

https://ithelp.ithome.com.tw/upload/images/20201007/20120424DaGIX4RcQU.png

-> 此篇文章的程式碼 github

Day25_符合p圖輪廓的變形圖片_merge_two_images_transform.ipynb

前言

我們該來運用之前學過的所有東西了!
綜合運用篇就是來一次運用前面的所學!

用 OpenCV 來把圖片p到各種奇怪的地方吧!

今天的功能與昨天的內容非常像,而我們今天來把圖片p到想要的地方:

  • 主程式 (讀取圖片)
  • 運用前天的內容,製作一個能讀取座標點的滑鼠控制畫面 (滑鼠處理)
  • 利用滑鼠控制回傳的座標進行透視投影運算
    • 取得要p圖片的四個座標
    • 找到最佳映射矩陣,並建立透視投影後的圖片
    • 將原圖片需要更改的部分,利用mask挖去
    • 最後合併兩張圖片,得到結果

主程式 (讀取圖片)

#Read the destination image
ori_img = cv2.imread("./testdata/tv.jpg")
print("origin image: ")
show_img(ori_img)

print("Click on four corners of bllboard and the press ENTER")
points = get_points(ori_img) 

這部分就單純的讀取圖片,
我們使用我們定義的 get_points 來幫助我們找到 輪廓的四個座標點

運用前天的內容,製作一個能讀取座標點的滑鼠控制畫面 (滑鼠處理)

def mouse_handler(event, x, y, flags, data):
    if event == cv2.EVENT_LBUTTONDOWN:
        # 標記點位置
        cv2.circle(data['img'], (x,y), 3, (0,0,255), -1) 

        # 改變顯示 window 的內容
        cv2.imshow("Image", data['img'])
        
        # 顯示 (x,y) 並儲存到 list中
        print("get points: (x, y) = ({}, {})".format(x, y))
        data['points'].append((x,y))

def get_points(img):
    # 建立 data dict, img:存放圖片, points:存放點
    data = {}
    data['img'] = img.copy()
    data['points'] = []
    
    # 建立一個 window
    cv2.namedWindow("Image", 0)
    
    # 改變 window 成為適當圖片大小
    h, w, dim = img.shape
    print("img height, width: ({}, {})".format(h, w))
    cv2.resizeWindow("Image", w, h)
        
    # 顯示圖片在 window 中
    cv2.imshow('Image',img)
    
    # 利用滑鼠回傳值,資料皆保存於 data dict中
    cv2.setMouseCallback("Image", mouse_handler, data)
    
    # 等待關閉視窗,藉由 OpenCV 內建函數釋放資源
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # 回傳點 list
    return data['points']

這部分都與前天的成品相同,可參考昨天的描述。


這是我們取的四個點:
(注意:請從左上角,依照順時針順序,在圖片的四個角落點四個點)

https://ithelp.ithome.com.tw/upload/images/20201007/20120424IS62cQG97G.png

利用滑鼠控制回傳的座標進行透視投影運算

# 垂直堆疊點
points_2D = np.vstack(points).astype(float)

print("\npoints list:")
print(points_2D)

我們將回傳的點座標堆疊成一個 2D list

取得要p圖片的四個座標

p_img = cv2.imread("./testdata/cat.jpg")
h, w, dim = p_img.shape

pst_src = np.array(
    [
        [0,0],
        [w-1,0],
        [w-1,h-1],
        [0,h-1]
     ],dtype=float
)

找到最佳映射矩陣,並建立透視投影後的圖片

h, status = cv2.findHomography(pst_src, points_2D)
# print(h)

# 透視投影 (建立變形後的圖)
print("image after reshape: ")
im_temp = cv2.warpPerspective(p_img, h, (ori_img.shape[1], ori_img.shape[0]))
show_img(im_temp)

透視後的圖片大概是長這樣~~~

https://ithelp.ithome.com.tw/upload/images/20201007/20120424CyzlvtLyau.png

將原圖片需要更改的部分,利用mask挖去

# 填充多邊形 - 黑色 (該區域全為0),等同於覆蓋上 mask
print("add mask: ")
cv2.fillConvexPoly(ori_img, points_2D.astype(int), 0, 16)
show_img(ori_img)

https://ithelp.ithome.com.tw/upload/images/20201007/20120424J1ws45EHHc.png

最後合併兩張圖片,得到結果

# add wraped source image to destination image
print("result image: ")
ori_img = cv2.add(ori_img, im_temp)
show_img(ori_img)

https://ithelp.ithome.com.tw/upload/images/20201007/20120424t8nWLJUUKB.png

Reference

https://zhuanlan.zhihu.com/p/143035374

https://kknews.cc/code/3oqxejy.html

https://blog.csdn.net/fanjiule/article/details/81606596

https://blog.csdn.net/yefcion/article/details/79435591

https://blog.csdn.net/fengyeer20120/article/details/87798638


上一篇
【沒錢ps,我用OpenCV!】Day 24 - 綜合運用3,(應用app) 用 OpenCV 來製作一個照片文件掃描機吧! photo scanner 透視投影
下一篇
【沒錢ps,我用OpenCV!】Day 26 - 進階修圖6,銳化圖片,將模糊的圖片變得更清晰吧! sharpen images
系列文
【錢不夠買ps的我,只好用OpenCV來修圖了!】30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言