带有OpenCV的android中的信号线 [英] Houghlines in android with opencv
问题描述
我正在尝试使用 opencv3 对四边形对象进行透视校正.我设法显示了这些行,并使用Imgproc.HoughLinesP()
实施了 Houghlines ,并尝试使用Imgproc.lines()
突出显示了这些行,但是输出没有成功.下面是我的代码,并且还附加了我的输出图像.
I am trying to make a perspective correction for quadrilateral objects using opencv3. I managed to show the lines and also implemented the Houghlines using Imgproc.HoughLinesP()
and tried to highlight the lines using Imgproc.lines()
but output was no success. Below is my code and also I have attached my output image.
请让我知道发生了什么错误以及应该怎么做...
Please let me know what is wrong happening and what should be done...
Mat initImg; // initial image
Mat greyImg; // converted to grey
Mat lines = new Mat();
int threshold = 50;
int minLineSize = 20;
int lineGap = 10;
initImg = Imgcodecs.imread(imgLoc, 1);
greyImg = new Mat();
Imgproc.cvtColor(initImg, greyImg, Imgproc.COLOR_BGR2GRAY);
Bitmap bitm = Bitmap.createBitmap(greyImg.cols(), greyImg.rows(),Bitmap.Config.ARGB_8888);
Imgproc.blur(greyImg, greyImg, new Size(3.d, 3.d));
Imgproc.adaptiveThreshold(greyImg, greyImg, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
minLineSize, lineGap);
// lines returns rows x columns and rows is always 1. I dont know why please help me to understand
for (int x = 0; x < lines.cols(); x++) {
double[] vec = lines.get(0, x);
double[] val = new double[4];
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
System.out.println(TAG+"Coordinates: x1=>"+x1+" y1=>"+y1+" x2=>"+x2+" y2=>"+y2);
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Imgproc.line(greyImg, start, end, new Scalar(0,255, 0, 255), 3);
}
Utils.matToBitmap(greyImg, bitm);
if(bitm!=null){
Toast.makeText(getApplicationContext(), "Bitmap not null", Toast.LENGTH_SHORT).show();
iv.setImageBitmap(bitm);
}else{
Toast.makeText(getApplicationContext(), "Bitmap null", Toast.LENGTH_SHORT).show();
}
我的输出:
推荐答案
我终于设法获得了警戒线.在灰色图像中没有显示粗线,我在灰色图像中提取了粗线,但是在彩色图像中绘制了粗线,然后就可以了.
I finally managed to get the houghlines. The houghlines was not showing in the grey images and I extracted the houghlines in grey image but plotted the lines in color image and it worked.
Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
minLineSize, lineGap);
for (int x = 0; x < lines.rows(); x++)
{
double[] vec = lines.get(x, 0);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
double dx = x1 - x2;
double dy = y1 - y2;
double dist = Math.sqrt (dx*dx + dy*dy);
if(dist>300.d) // show those lines that have length greater than 300
Imgproc.line(initImg, start, end, new Scalar(0,255, 0, 255),5);// here initimg is the original image.
}
这篇关于带有OpenCV的android中的信号线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!