在 OpenCV 中移动图像

Shift image in OpenCV

我想在不改变图像大小的情况下将图像向上移动 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();

您需要手动构建变形垫来实现此目的,而不是使用 getAffineTransform。尝试将 warpAffine 与以下垫子一起使用(未经测试):

Mat warpMat = new Mat( 2, 3, CvType.CV_64FC1 );
int row = 0, col = 0;
warpMat.put(row ,col, 1, 0, offsetx, 0, 1, offsety);

来源:

Shift image content with OpenCV

declare Mat in OpenCV java