【问题标题】:Shift image in OpenCV在 OpenCV 中移动图像
【发布时间】: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();

【问题讨论】:

标签: java opencv


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 2021-12-10
    • 2017-02-19
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多