我正在制作可以制作透视变换图像的Android应用程序。 我想做同样的事情,如下面的代码。
我试过但我无法阅读C代码。请告诉我!
谢谢,
祥一
示例代码(我想做)
#include <cv.h>
#include <highgui.h>
int
main (int argc, char **argv)
{
IplImage *src_img = 0, *dst_img = 0;
CvMat *map_matrix;
CvPoint2D32f src_pnt[4], dst_pnt[4];
if (argc >= 2)
src_img = cvLoadImage (argv[1], CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if (src_img == 0)
return -1;
dst_img = cvCloneImage (src_img);
src_pnt[0] = cvPoint2D32f (150.0, 150.0);
src_pnt[1] = cvPoint2D32f (150.0, 300.0);
src_pnt[2] = cvPoint2D32f (350.0, 300.0);
src_pnt[3] = cvPoint2D32f (350.0, 150.0);
dst_pnt[0] = cvPoint2D32f (200.0, 200.0);
dst_pnt[1] = cvPoint2D32f (150.0, 300.0);
dst_pnt[2] = cvPoint2D32f (350.0, 300.0);
dst_pnt[3] = cvPoint2D32f (300.0, 200.0);
map_matrix = cvCreateMat (3, 3, CV_32FC1);
cvGetPerspectiveTransform (src_pnt, dst_pnt, map_matrix);
cvWarpPerspective (src_img, dst_img, map_matrix, CV_INTER_LINEAR + CV_WARP_FILL_OUTLIERS, cvScalarAll (100));
cvNamedWindow ("src", CV_WINDOW_AUTOSIZE);
cvNamedWindow ("dst", CV_WINDOW_AUTOSIZE);
cvShowImage ("src", src_img);
cvShowImage ("dst", dst_img);
cvWaitKey (0);
cvDestroyWindow ("src");
cvDestroyWindow ("dst");
cvReleaseImage (&src_img);
cvReleaseImage (&dst_img);
cvReleaseMat (&map_matrix);
return 1;
}
我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Resources r = getResources();
Bitmap inputBitmap = BitmapFactory.decodeResource(r, R.drawable.icon);
Mat inputMat = Utils.bitmapToMat(inputBitmap);
Mat outputMat = inputMat.clone();
List<Point> src_pnt = new ArrayList<Point>();
Point p0 = new Point(75.0, 75.0);
src_pnt.add(p0);
Point p1 = new Point(75.0, 100.0);
src_pnt.add(p1);
Point p2 = new Point(100.0, 100.0);
src_pnt.add(p2);
Point p3 = new Point(100.0, 75.0);
src_pnt.add(p3);
Mat startM = Converters.vector_Point2f_to_Mat(src_pnt);
List<Point> dst_pnt = new ArrayList<Point>();
Point p4 = new Point(75.0, 75.0);
dst_pnt.add(p4);
Point p5 = new Point(75.0, 100.0);
dst_pnt.add(p5);
Point p6 = new Point(100.0, 100.0);
dst_pnt.add(p6);
Point p7 = new Point(100.0, 75.0);
dst_pnt.add(p7);
Mat endM = Converters.vector_Point2f_to_Mat(dst_pnt);
Mat M = new Mat(3, 3, CvType.CV_32F);
Core.perspectiveTransform(startM, endM, M);
Size size = new Size(200.0, 200.0);
Scalar scalar = new Scalar(50.0);
Imgproc.warpPerspective(inputMat, outputMat, M, size, Imgproc.INTER_LINEAR + Imgproc.CV_WARP_FILL_OUTLIERS, Imgproc.BORDER_DEFAULT, scalar);
Bitmap outputBitmap = inputBitmap;
Utils.matToBitmap(outputMat, outputBitmap);
ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView1.setImageBitmap(outputBitmap);
}
答案 0 :(得分:0)
您的dst_pnt点与src_pnt相同,因此转换矩阵将是标识,并且根本不会更改图像。使用其他一些观点。
其次,我认为warPerspective(size)的第四个参数应该是outputMap的大小,所以如果你想要一个200x200的图像,而不是
Mat outputMat = inputMat.clone();
使用
Mat outputMat = new Mat(200, 200);
答案 1 :(得分:-1)
有一个类似的问题,但找到了解决方案。有关工作示例,请参阅here。