問題
OPENCV的IMShow一次只能顯示一張圖片,但是很多時候我們需要同時顯示多張圖片。
方案一
網上搜索解決方案,多數是基於Python的,要麼用numpy的hstack/vstack,要麼使用plt解決。所幸,在opencv函數中找到了hconcat和vconcat,但是hconcat需要圖片高度一致,vconcat需要圖片寬度一致。我這裏選用hconcat,以第一張圖片的高度爲基準,其他圖片resize到同樣高度,但是爲了圖片不變形,寬度要按照高度變化比例縮放。
func ShowImages(title string,imgs ...cv.Mat,)*cv.Window{
if len(imgs)==0{
fmt.Println("[showImg] should give me at least one image!")
return nil
}
img, width, height:=StackImagesHV(imgs...)
defer func(img cv.Mat){
if img.Size()[0]!=imgs[0].Size()[0]{ img.Close() }
}(img)
win:=cv.NewWindow(title)
win.SetWindowTitle(title)
win.ResizeWindow(width, height )
//
win.IMShow(img)
return win
}
func StackImagesH(imgs ...cv.Mat) (img cv.Mat, width int, height int){
if len(imgs)==0{
panic("[StackImagesH] should give me at least one image!")
}
img=cv.NewMat()
if len(imgs)==1{
img = imgs[0].Clone()
width,height=ImageSize(img)
return
}
imgs[0].CopyTo(&img)
width,height = ImageSize(imgs[0])
for i:=1;i<len(imgs);i++{
pic:=imgs[i]
tw,th:=ImageSize(pic)
if th!=height {
np,nw:=ResizeImageByHeight(pic, height