OpenCV的Hu矩的Android
问题描述:
我开发一个应用程序,将取得在使用该装置的相机图像的胡时刻。在达到HU矩计算之前的图像是:灰度,gausseanBlurr,二值化,Canny算法,应用方法findcontour(),最后计算HU矩。所有这些都是在Android Studio上使用OpenCV完成的。OpenCV的Hu矩的Android
public void Hu()
{
Mat imagenOriginal;
imagenOriginal = new Mat();
Mat binario;
binario = new Mat();
Mat Canny;
Canny = new Mat();
Utils.bitmapToMat(bitmap, imagenOriginal);
Mat gris= new Mat(imagenOriginal.width() ,imagenOriginal.height(),imagenOriginal.type());
Imgproc.cvtColor(imagenOriginal, gris, Imgproc.COLOR_RGB2GRAY);
org.opencv.core.Size s = new Size(3,3);
Imgproc.GaussianBlur(gris, gris, s, 2);
Imgproc.threshold(gris, binario, 100, 255, Imgproc.THRESH_BINARY);
Imgproc.Canny(gris, Canny, 50, 50 * 3);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarcy = new Mat();
Imgproc.findContours(Canny, contours, hierarcy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(Canny, contours, -1, new Scalar(Math.random() * 255, Math.random() * 255, Math.random() * 255));
Moments momento = new Moments();
Mat hu= new Mat();
momento = Imgproc.moments(contours.get(0), false); // ERROR LINE
Imgproc.HuMoments(momento, hu);
}
当我执行计算Hu矩是当我有错误,因为我做了一个试验,每次q一些算法应用到该图像。如何让胡时刻 的代码从这里取:
Android: using hu moments opencv function to get the feature value
我也明白,胡时刻保存在胡变量,但我看不出有什么方法来打印所有值。
谢谢!
答
大哥你好:是的,我有同样的问题,我找人的WEB没有结果:(,但后来想了一会儿后,我得到一个清晰的时刻:如果我有所有的时刻(中心,归一化,生等),在瞬间发挥作用,为什么为了得到胡时刻,我不能直接使用的公式,所以我这样做,它的作品!,请若跌破检查这可以帮助您太...
Moments p = new Moments();
List<Moments> nu = new ArrayList<Moments>(contours.size());
for(int i=0; i<contours.size(); i++)
{
nu.add(i, Imgproc.moments(contours.get(i),false));
p=nu.get(i);
}
//光洁度现在手动计算HU矩 双[]时刻=新双[8];
double
n20 = p.get_nu20(),
n02 = p.get_nu02(),
n30 = p.get_nu30(),
n12 = p.get_nu12(),
n21 = p.get_nu21(),
n03 = p.get_nu03(),
n11 = p.get_nu11();
//First moment
moments[0] = n20 + n02;
//Second moment
moments[1] = Math.pow((n20 - 02), 2) + Math.pow(2 * n11, 2);
//Third moment
moments[2] = Math.pow(n30 - (3 * (n12)), 2)
+ Math.pow((3 * n21 - n03), 2);
//Fourth moment
moments[3] = Math.pow((n30 + n12), 2) + Math.pow((n12 + n03), 2);
//Fifth moment
moments[4] = (n30 - 3 * n12) * (n30 + n12)
* (Math.pow((n30 + n12), 2) - 3 * Math.pow((n21 + n03), 2))
+ (3 * n21 - n03) * (n21 + n03)
* (3 * Math.pow((n30 + n12), 2) - Math.pow((n21 + n03), 2));
//Sixth moment
moments[5] = (n20 - n02)
* (Math.pow((n30 + n12), 2) - Math.pow((n21 + n03), 2))
+ 4 * n11 * (n30 + n12) * (n21 + n03);
//Seventh moment
moments[6] = (3 * n21 - n03) * (n30 + n12)
* (Math.pow((n30 + n12), 2) - 3 * Math.pow((n21 + n03), 2))
+ (n30 - 3 * n12) * (n21 + n03)
* (3 * Math.pow((n30 + n12), 2) - Math.pow((n21 + n03), 2));
//Eighth moment
moments[7] = n11 * (Math.pow((n30 + n12), 2) - Math.pow((n03 + n21), 2))
- (n20 - n02) * (n30 + n12) * (n03 + n21);
return moments;
您好,非常感谢你和你清晰的时刻。但是我对你的代码有疑问。 Hu矩的手工计算你的内心做FOR循环或出来的吗? 如果外面做的,什么是“P”的价值? “P = nu.get(i)”将使用“我”的最后一个值? 如果它里面,我们将有八个瞬间为“女娲”列表中的每个“我”的时刻,我们终于有多少huMoments为每个图像?我需要为每个图像分别放置8个huMoments,而不是每个轮廓,不是? – Nimo
,现在我在循环FOR“Improc.moments(contour.get(i),false)”的行中有错误。它说“java.lang.UnsatisfiedLinkError:未找到本地方法:org.opencv.imgproc.Imgproc.moments_0:(JZ)[D”。你知道如何解决它?我在互联网上什么也没得到。谢谢! @Luis – Nimo
我想你最好用最大轮廓,然后得到胡时刻 – Luis