Golang在圖像中繪制矩形框的示例代碼
Golang 在圖像中繪制矩形框
從獲取的坐標(biāo)信息,在圖像中繪制矩形框,并添加標(biāo)注信息。
1. 依賴
- 字體 simsun.ttc
- 第三方庫(kù) github.com/fogleman/gg
2. 源碼
package main import ( "encoding/json" "fmt" "image" "image/color" "image/draw" "log" "math" "reflect" "strings" "github.com/fogleman/gg" ) // test_draw_rect_text 畫圖像矩形, 標(biāo)注, 顏色; 返回保存圖像路徑 // ref: https://github.com/fogleman/gg/blob/master/examples/rotated-image.go func test_draw_rect_text(im_path, font_path, detect_label, save_path string, x, y, w, h float64) { // Load image im, err := gg.LoadImage(im_path) if err != nil { log.Fatal(err) } // 1 method // iw, ih := im.Bounds().Dx(), im.Bounds().Dy() // Set Context // dc := gg.NewContext(iw, ih) // Draw image // dc.DrawImage(im, 0, 0) // 2 method dc := gg.NewContextForImage(im) // Set color and line width dc.SetHexColor("#FF0000") dc.SetLineWidth(1) // Draw rectangle dc.DrawRoundedRectangle(x, y, w, h, 0) // Store set dc.Stroke() // Set font and draw label var font_height float64 = 7 if err := dc.LoadFontFace(font_path, font_height); err != nil { panic(err) } rect_center_x := x + w/2 rect_center_y := y + h/2 dc.DrawStringAnchored(detect_label, rect_center_x, rect_center_y, 0.5, 0.5) dc.Clip() // Save png image dc.SavePNG(save_path) } func main() { im_path := "/home/tianzx/Pictures/lena.jpeg" font_path := "/home/tianzx/ai_model/simsun.ttc" detect_label := "缺角/碎裂" save_path := "/home/tianzx/Pictures/lena_test.png" var x, y, w, h float64 = 50, 100, 50, 50 test_draw_rect_text(im_path, font_path, detect_label, save_path, x, y, w, h) }
3. 結(jié)果
4. 說明
導(dǎo)入的Golang第三方庫(kù),并不是都使用了,按需進(jìn)行刪減。
到此這篇關(guān)于Golang在圖像中繪制矩形框的示例代碼的文章就介紹到這了,更多相關(guān)Golang繪制矩形框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang中一種不常見的switch語(yǔ)句寫法示例詳解
這篇文章主要介紹了golang中一種不常見的switch語(yǔ)句寫法,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05Go打印結(jié)構(gòu)體提升代碼調(diào)試效率實(shí)例詳解
這篇文章主要介紹了Go打印結(jié)構(gòu)體提升代碼調(diào)試效率實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02淺析go中Ticker,Timer和Tick的用法與區(qū)別
在go面試的時(shí)候,面試官經(jīng)常會(huì)問time包的Ticker,Timer以及Tick的區(qū)別,一般在超時(shí)控制的時(shí)候用的比較多,今天就跟隨小編一起來詳細(xì)學(xué)一下這幾個(gè)的區(qū)別吧2023-10-10go使用Gin框架利用阿里云實(shí)現(xiàn)短信驗(yàn)證碼功能
這篇文章主要介紹了go使用Gin框架利用阿里云實(shí)現(xiàn)短信驗(yàn)證碼,使用json配置文件及配置文件解析,編寫路由controller層,本文通過代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08Go語(yǔ)言基礎(chǔ)模板設(shè)計(jì)模式示例詳解
這篇文章主要為大家介紹了Go語(yǔ)言基礎(chǔ)設(shè)計(jì)模式之模板模式的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11