Java OpenCV和Android
我正在开发一个应用程序,它可以拍摄设备摄像头拍摄的图像的HU瞬间。计算HU矩之前的图像为:灰度、高斯模糊、二值化、canny算法、应用FindOntour()方法,最后计算HU矩。所有这些都是通过Android Studio上的OpenCV完成的Java OpenCV和Android,java,android,image,opencv,android-studio,Java,Android,Image,Opencv,Android Studio,我正在开发一个应用程序,它可以拍摄设备摄像头拍摄的图像的HU瞬间。计算HU矩之前的图像为:灰度、高斯模糊、二值化、canny算法、应用FindOntour()方法,最后计算HU矩。所有这些都是通过Android Studio上的OpenCV完成的 public void Hu() { Mat imagenOriginal; imagenOriginal = new Mat(); Mat binario; binario = new Mat(); Mat
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);
}
public void Hu()
{
原始图像;
imagenOriginal=新材料();
马特·比纳里奥;
binario=新材料();
坎尼;
Canny=新垫();
位图编辑器(位图,图像原始);
Mat gris=新Mat(imagenOriginal.width()、imagenOriginal.height()、imagenOriginal.type());
Imgproc.cvt颜色(图像原始、灰色、Imgproc.COLOR\u rgb2灰色);
org.opencv.core.Size s=新尺寸(3,3);
GaussianBlur(gris,gris,s,2);
Imgproc.threshold(gris,binario,100255,Imgproc.THRESH_BINARY);
Imgproc.Canny(gris,Canny,50,50*3);
列表等高线=新的ArrayList();
Mat层次=新Mat();
Imgproc.findContours(Canny、等高线、层次、Imgproc.RETR\u外部、Imgproc.CHAIN\u近似\u简单);
drawContours(Canny,contours,-1,新标量(Math.random()*255,Math.random()*255,Math.random()*255));
矩矩矩=新矩();
Mat hu=新Mat();
momento=Imgproc.moments(cours.get(0),false);//错误行
Imgproc.HuMoments(momento,hu);
}
当我执行计算时,Hu矩就是我有错误的时候,因为我每次对图像应用某些算法时都会进行测试。
如何获得HU时刻的代码取自此处:
我也知道HU时刻保存在HU变量中,但我看不到打印所有值的方法
谢谢大家! 嗨,兄弟:是的,我也有同样的问题,我在网上找了所有的东西,但都没有结果:(,但经过一段时间的思考,我得到了一个清晰的时刻:如果我有所有的时刻(中心、标准化、原始等)在矩函数中,为什么我不能直接使用公式来获得Hu矩,所以我这样做了,它是有效的!!,请在下面检查这是否对您也有帮助
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);
}
您好,非常感谢您和您的清晰时刻。但我对您的代码有一个疑问。您在FOR循环内部或外部手动计算Hu时刻?如果在外部进行,则“p”的值是多少?“p=nu.get(I)”将使用最后一个“I”值?如果在内部进行,则每个“I”将有八个时刻“nu”列表的时刻,最后是每个图像有多少个huMoments?我需要每个图像有8个huMoments,不是每个轮廓,不是吗?现在我在“Improc.moments(contour.get(I),false)”循环中有一个错误。它说“java.lang.UnsatisfiedLinkError:未找到本机方法:org.opencv.imgproc.imgproc.moments\u 0:(JZ)[D”.你知道怎么解决吗?我在网上什么都没有。thx!@LuisI认为你最好使用最大轮廓,然后获得Hu时刻
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;