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)
相关推荐
CS创新实验室14 小时前
OpenCV:从经典到现代,计算机视觉的基石与未来
人工智能·opencv·计算机视觉·cv
XXYBMOOO14 小时前
探索图像处理中的九种滤波器:从模糊到锐化与边缘检测
图像处理·人工智能·python·opencv·计算机视觉
胖墩会武术20 小时前
【OpenCV图像处理】图像去噪:cv.fastNlMeansDenoising()
图像处理·opencv·计算机视觉
Valueyou241 天前
论文阅读——CenterNet
论文阅读·python·opencv·目标检测·计算机视觉
hixiong1231 天前
C# OpenCVSharp实现Hand Pose Estimation Mediapipe
开发语言·opencv·ai·c#·手势识别
Dm_dotnet1 天前
OpenCVSharp:ArUco 标记检测与透视变换
opencv
俞凡2 天前
Golang 构建网络漏洞扫描器
go
百锦再2 天前
第14章 智能指针
android·java·开发语言·git·rust·go·错误
yy_xzz2 天前
OpenCV 图像处理与键盘交互
图像处理·opencv
Mgx2 天前
用 Go 写个“端口扫描器”,100 行代码扫描你家路由器?(别慌,只是看看谁在开门!)
go