- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 matrix.setPolyToPoly 函数将位图的选定区域(4 个角)转换为矩形,通常效果非常好。但在下一个例子中:
polyToPoly 函数无法进行透视变换:
我画了两条线用于测试,线标记了我想要四个选定点的位置。
我做错了什么?谢谢!
编辑:我已经使用 canvas.drawBitmapMesh 解决了这个问题,谢谢 pskink征求您的意见!!
这是最终代码
private float[] generateVertices(int widthBitmap, int heightBitmap) {
float[] vertices=new float[(WIDTH_BLOCK+1)*(HEIGHT_BLOCK+1)*2];
float widthBlock = (float)widthBitmap/WIDTH_BLOCK;
float heightBlock = (float)heightBitmap/HEIGHT_BLOCK;
for(int i=0;i<=HEIGHT_BLOCK;i++)
for(int j=0;j<=WIDTH_BLOCK;j++) {
vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)] = j * widthBlock;
vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)+1] = i * heightBlock;
}
return vertices;
}
private Bitmap perspectiveTransformation(Bitmap bitmap, ArrayList<Point> bitmapPoints) {
Bitmap correctedBitmap;
int maxX = (int) Math.max(Math.abs(bitmapPoints.get(0).x - bitmapPoints.get(1).x), Math.abs(bitmapPoints.get(2).x - bitmapPoints.get(3).x));
int maxY = (int) Math.max(Math.abs(bitmapPoints.get(0).y - bitmapPoints.get(3).y), Math.abs(bitmapPoints.get(1).y - bitmapPoints.get(2).y));
Log.d("max", "x=" + maxX + " y=" + maxY); //This is the desired final size
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
correctedBitmap = Bitmap.createBitmap(maxX,maxY,conf); //the final bitmap
float mVertices[] =generateVertices(bitmap.getWidth(),bitmap.getHeight());
Point mLeftTop = bitmapPoints.get(0);
Point mRightTop = bitmapPoints.get(1);
Point mLeftBot = bitmapPoints.get(3);
Point mRightBot = bitmapPoints.get(2); //the points on the image where the user has clicked
Canvas canvas = new Canvas(correctedBitmap);
Matrix matrix = new Matrix();
matrix.setPolyToPoly(
new float[]{mLeftTop.x, mLeftTop.y,
mRightTop.x, mRightTop.y,
mRightBot.x, mRightBot.y,
mLeftBot.x, mLeftBot.y //the user's points
},
0,
new float[]{0, 0,
maxX - 1, 0,
maxX - 1, maxY - 1,
0, maxY - 1 //where I want the user points in the corrected image
}
, 0, 4);
canvas.concat(matrix);
Paint paint = new Paint();
paint.setAntiAlias(true); //testing parameters
paint.setFilterBitmap(true); //testing parameters
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawBitmapMesh(bitmap, WIDTH_BLOCK , HEIGHT_BLOCK, mVertices,0,null,0, paint); //draw the original bitmap into the corrected bitmap with PolyToPoly transformation matrix
canvas.drawLine(mLeftTop.x, mLeftTop.y, mRightBot.x, mRightBot.y, paint); //draw two lines for testing the transformation matrix
canvas.drawLine(mLeftBot.x, mLeftBot.y, mRightTop.x, mRightTop.y, paint);
//bitmap.recycle(); //just testing
return correctedBitmap;
}
最佳答案
private float[] generateVertices(int widthBitmap, int heightBitmap) {
float[] vertices=new float[(WIDTH_BLOCK+1)*(HEIGHT_BLOCK+1)*2];
float widthBlock = (float)widthBitmap/WIDTH_BLOCK;
float heightBlock = (float)heightBitmap/HEIGHT_BLOCK;
for(int i=0;i<=HEIGHT_BLOCK;i++)
for(int j=0;j<=WIDTH_BLOCK;j++) {
vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)] = j * widthBlock;
vertices[i * ((HEIGHT_BLOCK+1)*2) + (j*2)+1] = i * heightBlock;
}
return vertices;
}
private Bitmap perspectiveTransformation(Bitmap bitmap, ArrayList<Point> bitmapPoints) {
Bitmap correctedBitmap;
int maxX = (int) Math.max(Math.abs(bitmapPoints.get(0).x - bitmapPoints.get(1).x), Math.abs(bitmapPoints.get(2).x - bitmapPoints.get(3).x));
int maxY = (int) Math.max(Math.abs(bitmapPoints.get(0).y - bitmapPoints.get(3).y), Math.abs(bitmapPoints.get(1).y - bitmapPoints.get(2).y));
Log.d("max", "x=" + maxX + " y=" + maxY); //This is the desired final size
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
correctedBitmap = Bitmap.createBitmap(maxX,maxY,conf); //the final bitmap
float mVertices[] =generateVertices(bitmap.getWidth(),bitmap.getHeight());
Point mLeftTop = bitmapPoints.get(0);
Point mRightTop = bitmapPoints.get(1);
Point mLeftBot = bitmapPoints.get(3);
Point mRightBot = bitmapPoints.get(2); //the points on the image where the user has clicked
Canvas canvas = new Canvas(correctedBitmap);
Matrix matrix = new Matrix();
matrix.setPolyToPoly(
new float[]{mLeftTop.x, mLeftTop.y,
mRightTop.x, mRightTop.y,
mRightBot.x, mRightBot.y,
mLeftBot.x, mLeftBot.y //the user's points
},
0,
new float[]{0, 0,
maxX - 1, 0,
maxX - 1, maxY - 1,
0, maxY - 1 //where I want the user points in the corrected image
}
, 0, 4);
canvas.concat(matrix);
Paint paint = new Paint();
paint.setAntiAlias(true); //testing parameters
paint.setFilterBitmap(true); //testing parameters
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
canvas.drawBitmapMesh(bitmap, WIDTH_BLOCK , HEIGHT_BLOCK, mVertices,0,null,0, paint); //draw the original bitmap into the corrected bitmap with PolyToPoly transformation matrix
canvas.drawLine(mLeftTop.x, mLeftTop.y, mRightBot.x, mRightBot.y, paint); //draw two lines for testing the transformation matrix
canvas.drawLine(mLeftBot.x, mLeftBot.y, mRightTop.x, mRightTop.y, paint);
//bitmap.recycle(); //just testing
return correctedBitmap;
}
关于android - 在 Android 上的某些情况下,将图像扭曲成四边形会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308856/
我确实遇到了这个问题。 我有一个可以是任何形状的多边形(四边形)。当我的鼠标位于多边形内时,我需要找到鼠标所在位置(四边形内)的 x,y 值,就好像多边形是完美的正方形一样。进一步解释;我有一个 32
我的问题是关于 OpenGL 和法线,我理解它们背后的数学原理,并且我取得了一些成功。 我在下面附加的函数接受一个交错的顶点数组,并计算每 4 个顶点的法线。这些代表具有相同方向的 QUADS。据我了
我想检测图像中的矩形。 我使用 cv2.findContours() 和 cv2.convexHull() 过滤掉不规则多边形。 之后,我将使用船体长度来判断轮廓是否为矩形。 hull = cv2.c
有谁知道将 vector 路径转换为由三角形/四边形面组成的描边路径的好算法?理想情况下使用圆线连接。 基本上,我试图绘制一条粗路径,其颜色基于随路径距离变化的值。我正在考虑将路径转换为三角形/四边形
我有 WebGL/OpenGL 的基本知识,但不了解 gl.bufferSubData。所以我的目标是创建一个 SpriteBatch 类,就像这个问题 First Question 一样。由于我认为
我用 OpenGL 渲染两个具有相等 z 的四边形。当我启用 DEPTH 时,我得到以下图像,但当它关闭时,我得到我需要的一个水果而不是另一个。是否可以按我的意愿绘制具有相等 z 的四边形?我的 OG
我一直在尝试让模板测试在我的 OpenTK 2D 游戏中工作,但没有成功 - 我只想在模板缓冲区中绘制低于 say 1 值的纹理部分。花了很长时间阅读模板及其工作原理,但在 C# 中找不到一个示例。下
我有一个 2D 空间,其中包含任意数量的对象(它们是圆形或四边形 - 这无关紧要),在每个时间刻度中具有不同的大小和不同的位置。我想为任何基元(示例图片中的绿色圆圈)找到一个 y 位置,使其不与任何其
我是一名优秀的程序员,十分优秀!