【发布时间】:2021-12-29 11:00:39
【问题描述】:
我想在不改变图像大小的情况下将图像向上移动 10 个像素。 我知道对此有一个非常相似的问题,但网络上没有关于如何使用 Java 进行操作的信息。 OpenCV 文档也没有说明这一点。
原图:
移动后的图像应如下所示:
我尝试使用 warpAffine() 函数对矩阵进行处理,但我不知道如何让它工作,我只弄乱了随机值,但它只是转置图像,也许我找不到合适的函数这个。
var imagePhoto = Imgcodecs.imread("images/ZAD1/myPhoto.jpg");
Point[] srcTri = new Point[3];
srcTri[0] = new Point(0, 0);
srcTri[1] = new Point(imagePhoto.cols() - 1, 0);
srcTri[2] = new Point(0, imagePhoto.rows() - 1);
Point[] dstTri = new Point[3];
dstTri[0] = new Point(0, imagePhoto.rows()*0);
dstTri[1] = new Point(imagePhoto.cols()*0.5, imagePhoto.rows()*0.25);
dstTri[2] = new Point(imagePhoto.cols()*0.15, imagePhoto.rows()*0.7);
Mat warpMat = Imgproc.getAffineTransform( new MatOfPoint2f(srcTri), new MatOfPoint2f(dstTri) );
Mat warpDst = Mat.zeros(imagePhoto.rows(), imagePhoto.cols(), imagePhoto.type() );
Imgproc.warpAffine(imagePhoto, warpDst, warpMat, warpDst.size() );
Imgcodecs.imwrite("images/ZAD1/shiftedMyPhoto.jpg", warpDst);
HighGui.imshow("image", warpDst);
HighGui.waitKey();
【问题讨论】:
-
我假设你已经看过这篇文章了?他们描述了如何做到这一点:stackoverflow.com/questions/19068085/…
-
你真的只是想换档,还是想“滚动”?我想 2013 年的问题有足够的例子,所以你可以将 C++ 翻译成 Java,不是吗?
-
这能回答你的问题吗? Shift image content with OpenCV