我想在不改變影像大小的情況下將影像向上移動 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();
uj5u.com熱心網友回復:
您需要手動構建經紗墊來實作這一點,而不是使用 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);
資料來源:
使用 OpenCV 移動影像內容
在 OpenCV java 中宣告 Mat
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361549.html
