如何在安卓系统中扭曲图像?

5

我正在开发一个应用程序,其中有一个图像扭曲模块。我参考了几个网站,但是没有找到解决我的问题的方案。

如果有任何关于脸部扭曲的教程/链接或建议,都将非常有帮助。


1
不,这个网站不是这样运作的。如果您有具体的问题,我们会尝试帮助解决它。您必须自己做一些工作。 - Marc B
6
我认为人们可以请求教程等帮助,因为这是一个帮助网站,他们正在寻求与帮助相关的帮助,所以应该被允许。 - FabianCook
1
现在Stack Overflow允许编辑问题了,我想… - Vivek Kalkur
你有解决方案请分享一下,我需要帮助。 - Roadies
2个回答

10

这是随 Android SDK 一起提供的示例。从你的问题中并不清楚你想了解 Android API 还是非常扭曲的算法。

public class BitmapMesh extends GraphicsActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View {
        private static final int WIDTH = 20;
        private static final int HEIGHT = 20;
        private static final int COUNT = (WIDTH + 1) * (HEIGHT + 1);

        private final Bitmap mBitmap;
        private final float[] mVerts = new float[COUNT*2];
        private final float[] mOrig = new float[COUNT*2];

        private final Matrix mMatrix = new Matrix();
        private final Matrix mInverse = new Matrix();

        private static void setXY(float[] array, int index, float x, float y) {
            array[index*2 + 0] = x;
            array[index*2 + 1] = y;
        }

        public SampleView(Context context) {
            super(context);
            setFocusable(true);

            mBitmap = BitmapFactory.decodeResource(getResources(),
                                                     R.drawable.beach);

            float w = mBitmap.getWidth();
            float h = mBitmap.getHeight();
            // construct our mesh
            int index = 0;
            for (int y = 0; y <= HEIGHT; y++) {
                float fy = h * y / HEIGHT;
                for (int x = 0; x <= WIDTH; x++) {
                    float fx = w * x / WIDTH;                    
                    setXY(mVerts, index, fx, fy);
                    setXY(mOrig, index, fx, fy);
                    index += 1;
                }
            }

            mMatrix.setTranslate(10, 10);
            mMatrix.invert(mInverse);
        }

        @Override protected void onDraw(Canvas canvas) {
            canvas.drawColor(0xFFCCCCCC);

            canvas.concat(mMatrix);
            canvas.drawBitmapMesh(mBitmap, WIDTH, HEIGHT, mVerts, 0,
                                  null, 0, null);
        }

        private void warp(float cx, float cy) {
            final float K = 10000;
            float[] src = mOrig;
            float[] dst = mVerts;
            for (int i = 0; i < COUNT*2; i += 2) {
                float x = src[i+0];
                float y = src[i+1];
                float dx = cx - x;
                float dy = cy - y;
                float dd = dx*dx + dy*dy;
                float d = FloatMath.sqrt(dd);
                float pull = K / (dd + 0.000001f);

                pull /= (d + 0.000001f);
             //   android.util.Log.d("skia", "index " + i + " dist=" + d + " pull=" + pull);

                if (pull >= 1) {
                    dst[i+0] = cx;
                    dst[i+1] = cy;
                } else {
                    dst[i+0] = x + dx * pull;
                    dst[i+1] = y + dy * pull;
                }
            }
        }

        private int mLastWarpX = -9999; // don't match a touch coordinate
        private int mLastWarpY;

        @Override public boolean onTouchEvent(MotionEvent event) {
            float[] pt = { event.getX(), event.getY() };
            mInverse.mapPoints(pt);

            int x = (int)pt[0];
            int y = (int)pt[1];
            if (mLastWarpX != x || mLastWarpY != y) {
                mLastWarpX = x;
                mLastWarpY = y;
                warp(pt[0], pt[1]);
                invalidate();
            }
            return true;
        }
    }
}

我在哪里可以获取GraphicsActivity类? - c-an

2
图像变形通常包含两个主要阶段。第一阶段是查找每个图像上匹配的点。第二阶段涉及在匹配点集之间找到转换。这两个阶段都不是琐碎的,图像变形(一般而言)仍然是一个困难的问题。我过去曾经解决过这个问题,因此可以从经验上说话。
将问题分成两部分,您可以独立地为每部分设计解决方案。例如,阅读一些网络材料(http://groups.csail.mit.edu/graphics/classes/CompPhoto06/html/lecturenotes/14_WarpMorph_6.pdf)会很有帮助。
在第一阶段中,交叉相关通常用作在两幅图像上找到匹配点的基础。
第二阶段中使用的转换将确定您可以将一幅图像变形到另一幅图像上的准确程度。线性变换现在将非常好,而使用样条近似的二维变换肯定可以处理非线性。

这是另一个有用的链接(http://cmp.felk.cvut.cz/~hlavac/Public/TeachingLectures/registration_intro.pdf)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接