go调用opencv图片矫正

这篇是用go的图片矫正代码记录。参考前面一篇文章: Java调用opencv图片矫正

图片矫正实现流程

1.获取角度

HoughLines获取角度

css 复制代码
HoughLines 返回的是角度,画线坐标点需要计算。[0]像素单位距离 [1]角度;注意问题得到线条有干扰需要控制
HoughLinesPWithParams 返回的是坐标点直接画线

FindContours获取角度

复制代码
FindContours 得到最大面积轮廓-》得到角度

2.旋转图片

go 复制代码
temp2D := gocv.GetRotationMatrix2D(rect.Center, angle, 0.8)
gocv.WarpAffine(reactImg, &desc, temp2D, image.Pt(0, reactImg.Cols()/2))

代码

css 复制代码
windowA := gocv.NewWindow("watch")
	defer windowA.Close()

	root := getCurrentAbPathByCaller()
	img1 := gocv.IMRead(root+string(os.PathSeparator)+"tp.jfif", gocv.IMReadColor)
	img2 := gocv.NewMat()
	gocv.Canny(img1, &img2, 80, 160)
	//img3, _ := img2.ToImage()
	/*
			RetrievalExternal 最外侧轮廓
		    RETR_LIST:返回所有轮廓线,不建立等级关系
			RETR_CCOMP :返回所有轮廓,包含两个层级结构
			RETR_TREE :返回所有轮廓,建立完整的层次结构
	*/
	pointVecotr := gocv.FindContours(img2, gocv.RetrievalExternal, gocv.ChainApproxNone)
	gocv.DrawContours(&img1, pointVecotr, -1, color.RGBA{255, 0, 0, 0}, 1)

	maxindex := getMaxArea(pointVecotr)
	react := gocv.BoundingRect(pointVecotr.At(maxindex))
	reactImg := img1.Region(react)

	rect := gocv.MinAreaRect(pointVecotr.At(maxindex))
	points := rect.Points
	for i := 0; i < len(points); i++ {
		fmt.Println("%d %d", points[i].X, points[i].Y)
	}

	//
	line1 := math.Sqrt(math.Pow(float64(points[1].Y-points[0].Y), 2) + math.Pow(float64(points[1].X-points[0].X), 2))
	line2 := math.Sqrt(math.Pow(float64(points[3].Y-points[0].Y), 2) + math.Pow(float64(points[3].X-points[0].X), 2))

	//角度
	var angle = rect.Angle

	if line1 > line2 {
		angle = 90 + angle
	}

	desc := gocv.NewMat()

	/*
	GetAffineTransform 原图像点-》转换后图像点
	*/
	temp2D := gocv.GetRotationMatrix2D(rect.Center, angle, 0.8)
	gocv.WarpAffine(reactImg, &desc, temp2D, image.Pt(0, reactImg.Cols()/2))

	//reactImg := img1.Region(react)
	//gocv.BoxPoints(gocv.MinAreaRect(pointVecotr.At(getMaxArea(pointVecotr))), &img2)
	windowA.IMShow(desc)
	windowA.WaitKey(0)
相关推荐
喵个咪2 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:kratos-bootstrap 入门教程(类比 Spring Boot)
后端·微服务·go
王中阳Go6 小时前
12 Go Eino AI应用开发实战 | 消息队列架构
人工智能·后端·go
香吧香6 小时前
go项目使用go build 与 MakeFile 构建项目
go
代码扳手9 小时前
Go 微服务数据库实现全解析:读写分离、缓存防护与生产级优化实战
数据库·后端·go
王中阳Go10 小时前
我辅导400+学员拿Go Offer后发现:突破年薪50W,常离不开这10个实战技巧
后端·面试·go
jghhh0116 小时前
基于阈值分割的车牌定位识别
图像处理·opencv·计算机视觉
黄昏单车16 小时前
golang语言基础到进阶学习笔记
笔记·golang·go
王中阳Go1 天前
15 Go Eino AI应用开发实战 | 性能优化
后端·面试·go
王中阳Go1 天前
09 Go Eino AI应用开发实战 | Hertz Web 框架搭建
人工智能·后端·go
陈天伟教授2 天前
人工智能训练师认证教程(4)OpenCV 快速实践
人工智能·python·神经网络·opencv·机器学习·计算机视觉