gpt4 book ai didi

android - 提供的数据元素编号 (0) 应该是 Mat channel 数 (1) Android OpenCV 的倍数

转载 作者:太空宇宙 更新时间:2023-11-03 21:52:52 24 4
gpt4 key购买 nike

我试图从一个矩形对象(纸张)中找到 4 个角。

                Mat source = new Mat();

Org.Opencv.Core.Point center;

public GetCorners(Bitmap _sourceImg)
{
Utils.BitmapToMat(_sourceImg, source);
}
//find corners
public void FindCorners()
{
center = new Org.Opencv.Core.Point(0, 0);
//Mat source = new Mat();

if (source == null)
{
Console.WriteLine("No IMG");
return;
}

Mat BlackWhite = new Mat();

Imgproc.CvtColor(source, BlackWhite, Imgproc. ColorBgr2gray); //ColorBgra2gray, 4);

Imgproc.Blur(BlackWhite, BlackWhite, new Size(3, 3));

Imgproc.Canny(BlackWhite, BlackWhite, 100, 100, 3, true);

Mat Lines = new Mat();
int treshold = 70;
int minLinsize = 30;
int lineGap = 10;

Imgproc.HoughLinesP(BlackWhite, Lines, 1, Math.PI / 180, treshold, minLinsize, lineGap);

for (int i = 0; i < Lines.Cols(); i++)
{
double[] Vector = Lines.Get(0, i);
double[] Value = new double[4];

Value[0] = 0;
Value[1] = ((float) Vector[1] - Vector[3]) / (Vector[0] - Vector[2]) * -Vector[0] + Vector[1];
Value[2] = source.Cols();
Value[3] = ((float)Vector[1] - Vector[3]) / (Vector[0] - Vector[2]) * (source.Cols() - Vector[2]) + Vector[3];

Lines.Put(0, i, Value);
}


Console.WriteLine("##Quantity {0} Founded##",Lines.Cols());
List<Org.Opencv.Core.Point> Corners = new List<Org.Opencv.Core.Point>();

for (int i = 0; i < Lines.Cols(); i++)
{
for (int j = 0 ; i < Lines.Cols(); j++)
{
Mat m1 = new Mat(),
m2 = new Mat();
double[] d1 = Lines.Get(0, i);
double[] d2 = Lines.Get(0, j);
m1.Put(0, j, d1);
m2.Put(0, j, d2);
try
{
//i'm getting exception here
Org.Opencv.Core.Point pt = ComputeInteresect(Lines.Get(0, i), Lines.Get(0, j)); //(m1, m2);

if (pt.X >= 0 && pt.Y >= 0)
{
Corners.Add(pt);
Console.WriteLine ("dobavleno {0} koordinat",Corners.Count);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}

我还有一个计算相交的方法:

static Org.Opencv.Core.Point ComputeInteresect(double[] a, double[] b) //(Mat es,Mat es2)// 
{
double x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
double denom = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
Org.Opencv.Core.Point pt = new Org.Opencv.Core.Point();
if (denom != 0)
{

pt.X = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2)
* (x3 * y4 - y3 * x4))
/ denom;
pt.Y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2)
* (x3 * y4 - y3 * x4))
/ denom;
return pt;
}
else
return new Org.Opencv.Core.Point(-1, -1);
}

我不明白为什么,但我得到了这个异常:


java.lang.UnsupportedOperationException: Provided data element number (0) should be multiple of the Mat channels count (1)


我发现,当图像为 RGB 格式(并且有 4 个 channel )时会出现此问题,但首先,我通过此方法转换为灰色(1 个 channel ) :

Imgproc.CvtColor(source, BlackWhite, Imgproc. ColorBgr2gray);  //ColorBgra2gray, 4);  

任何帮助将不胜感激,谢谢!

最佳答案

最后,我更改了部分代码及其工作:

Console.WriteLine("##Quantity {0} Founded##",Lines.Cols());
List<Org.Opencv.Core.Point> Corners = new List<Org.Opencv.Core.Point>();

for (int i = 0; i < Lines.Cols(); i++)
{
for (int j = i+1 ; i < Lines.Cols(); j++)
{

try
{

Org.Opencv.Core.Point pt = ComputeInteresect(Lines.Get(0, i), Lines.Get(0, j));

if (pt.X >= 0 && pt.Y >= 0)
{
Corners.Add(pt);
Console.WriteLine ("dobavleno {0} koordinat",Corners.Count);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

关于android - 提供的数据元素编号 (0) 应该是 Mat channel 数 (1) Android OpenCV 的倍数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com