Java 用OpenCV检测矩形
因此,我对OpenCV完全陌生,在过去的2-3天里,我搜索了很多关于如何在Java和Android studio中使用OpenCV来执行透视校正和检测位图中的最大矩形的信息,根据我的搜索,我做了一些工作,但结果位图不正确。我确信我做了很多错误的事情,所以这将是正确的如果有人帮我,那太好了 提前感谢你的帮助Java 用OpenCV检测矩形,java,android,android-studio,opencv3.0,Java,Android,Android Studio,Opencv3.0,因此,我对OpenCV完全陌生,在过去的2-3天里,我搜索了很多关于如何在Java和Android studio中使用OpenCV来执行透视校正和检测位图中的最大矩形的信息,根据我的搜索,我做了一些工作,但结果位图不正确。我确信我做了很多错误的事情,所以这将是正确的如果有人帮我,那太好了 提前感谢你的帮助 public void onPictureTaken(byte[] data, Camera camera) { Bitmap myImage = BitmapFactory.dec
public void onPictureTaken(byte[] data, Camera camera)
{
Bitmap myImage = BitmapFactory.decodeByteArray(data, 0, data.length);
Mat matImage = new Mat(myImage.getHeight(),myImage.getWidth(), CvType.CV_8UC3);
Bitmap myBitmap32 = myImage.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(myBitmap32, matImage);
correctPerspective(matImage);
}
公共静态透视图(Mat imgSource)
{
//将图像转换为黑白图像(8位)
Canny(imgSource.clone(),imgSource,50,50);
//将高斯模糊应用于平滑的点线
GaussianBlur(imgSource,imgSource,neworg.opencv.core.Size(5,5,5));
//找到轮廓
列表等高线=新的ArrayList();
Imgproc.findContours(imgSource、contours、new Mat()、Imgproc.RETR\u LIST、Imgproc.CHAIN\u APPROX\u SIMPLE);
双最大面积=-1;
MatOfPoint temp_contour=等高线。获取(0);
//用于启动的索引0
//点
MatOfPoint2f approxCurve=新的MatOfPoint2f();
对于(int idx=0;idx最大面积){
//检查此轮廓是否为正方形
MatOfPoint2f new_mat=新MatOfPoint2f(temp_contour.toArray());
int contourSize=(int)temp_contour.total();
MatOfPoint2f approxCurve_temp=新的MatOfPoint2f();
Imgproc.approxPolyDP(新垫,近似弯曲温度,轮廓尺寸*0.05,真实);
如果(近似曲线温度总计=4){
最大面积=轮廓面积;
近似曲线=近似曲线温度;
}
}
}
Imgproc.cvtColor(imgSource,imgSource,Imgproc.COLOR_BayerBG2RGB);
双[]双温度;
temp_double=approxCurve.get(0,0);
点p1=新点(温度双精度[0],温度双精度[1]);
temp_double=approxCurve.get(1,0);
点p2=新点(温度双精度[0],温度双精度[1]);
temp_double=approxCurve.get(2,0);
点p3=新点(温度双精度[0],温度双精度[1]);
temp_double=approxCurve.get(3,0);
点p4=新点(温度双精度[0],温度双精度[1]);
列表源=新的ArrayList();
来源.添加(p1);
来源:add(p2);
来源.添加(p3);
来源.增加(第4页);
Mat startM=转换器。矢量点2f到Mat(源);
Mat结果=翘曲(imgSource,startM);
//保存为位图
位图resultBitmap=Bitmap.createBitmap(result.cols(),result.rows(),Bitmap.Config.ARGB_8888);;
Mat tmp=new Mat(result.cols(),result.rows(),CvType.CV_8U,新标量(4));
Imgproc.cvtColor(结果、tmp、Imgproc.COLOR_RGB2BGRA);
Utils.matToBitmap(tmp,resultBitmap);
}
public static Mat warp(Mat inputMat,Mat startM)
{
int resultWidth=1200;
int resultHeight=680;
点ocvPOut4=新点(0,0);
点ocvPOut1=新点(0,结果右侧);
点ocvPOut2=新点(结果宽度,结果宽度);
点ocvPOut3=新点(结果宽度,0);
if(inputMat.height()>inputMat.width())
{
ocvPOut3=新点(0,0);
ocvPOut4=新点(0,结果右侧);
ocvPOut1=新点(结果宽度,结果宽度);
ocvPOut2=新点(结果宽度,0);
}
Mat outputMat=新Mat(结果宽度、结果宽度、CvType.CV_8UC4);
List dest=new ArrayList();
目的地添加(ocvPOut1);
目的地添加(ocvPOut2);
目的地添加(ocvPOut3);
目的地添加(ocvPOut4);
Mat endM=转换器。向量点2f到Mat(dest);
Mat perspectiveTransform=Imgproc.getPerspectiveTransform(startM,endM);
Imgproc.warpPerspective(输入框、输出框、透视变换、新大小(resultWidth、ResultWight)、Imgproc.INTER_立方体);
返回输出矩阵;
}
您找到解决方案了吗
public static void correctPerspective(Mat imgSource)
{
// convert the image to black and white does (8 bit)
Imgproc.Canny(imgSource.clone(), imgSource, 50, 50);
// apply gaussian blur to smoothen lines of dots
Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);
// find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
double maxArea = -1;
MatOfPoint temp_contour = contours.get(0);
// index 0 for starting
// point
MatOfPoint2f approxCurve = new MatOfPoint2f();
for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
// compare this contour to the previous largest contour found
if (contourarea > maxArea) {
// check if this contour is a square
MatOfPoint2f new_mat = new MatOfPoint2f(temp_contour.toArray());
int contourSize = (int) temp_contour.total();
MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
if (approxCurve_temp.total() == 4) {
maxArea = contourarea;
approxCurve = approxCurve_temp;
}
}
}
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
double[] temp_double;
temp_double = approxCurve.get(0, 0);
Point p1 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(1, 0);
Point p2 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(2, 0);
Point p3 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve.get(3, 0);
Point p4 = new Point(temp_double[0], temp_double[1]);
List<Point> source = new ArrayList<Point>();
source.add(p1);
source.add(p2);
source.add(p3);
source.add(p4);
Mat startM = Converters.vector_Point2f_to_Mat(source);
Mat result = warp(imgSource, startM);
//Saving into bitmap
Bitmap resultBitmap = Bitmap.createBitmap(result.cols(), result.rows(),Bitmap.Config.ARGB_8888);;
Mat tmp = new Mat (result.cols(), result.rows(), CvType.CV_8U, new Scalar(4));
Imgproc.cvtColor(result, tmp, Imgproc.COLOR_RGB2BGRA);
Utils.matToBitmap(tmp, resultBitmap);
}
public static Mat warp(Mat inputMat, Mat startM)
{
int resultWidth = 1200;
int resultHeight = 680;
Point ocvPOut4 = new Point(0, 0);
Point ocvPOut1 = new Point(0, resultHeight);
Point ocvPOut2 = new Point(resultWidth, resultHeight);
Point ocvPOut3 = new Point(resultWidth, 0);
if (inputMat.height() > inputMat.width())
{
ocvPOut3 = new Point(0, 0);
ocvPOut4 = new Point(0, resultHeight);
ocvPOut1 = new Point(resultWidth, resultHeight);
ocvPOut2 = new Point(resultWidth, 0);
}
Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);
List<Point> dest = new ArrayList<Point>();
dest.add(ocvPOut1);
dest.add(ocvPOut2);
dest.add(ocvPOut3);
dest.add(ocvPOut4);
Mat endM = Converters.vector_Point2f_to_Mat(dest);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);
Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, new Size(resultWidth, resultHeight), Imgproc.INTER_CUBIC);
return outputMat;
}