如何将GLCM的C ++实现转换为Java? [英] How to convert C++ implementation of GLCM into Java?

查看:277
本文介绍了如何将GLCM的C ++实现转换为Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从GitHub获得了以下代码片段来计算灰色通过OpenCV的级别共生矩阵(GLCM):

I got the following snippet from GitHub to compute the gray level co-occurrence matrix (GLCM) through OpenCV:

float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
int row=img.rows,col=img.cols;
Mat gl=Mat::zeros(256,256,CV_32FC1);

//creating glcm matrix with 256 levels,radius=1 and in the horizontal direction 
for(int i=0;i<row;i++)
   for(int j=0;j<col-1;j++)
       gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))=gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))+1;   

// normalizing glcm matrix for parameter determination
gl=gl+gl.t();            
gl=gl/sum(gl)[0];

上面的代码是用C ++编写的。我需要将其转换为Java但我仍然坚持这一行:

The code above is in C++. I need to convert this into Java but I'm stuck in this line:

gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))=gl.at<‌​float>(img.at<uchar>‌​(i,j),img.at<uchar>(‌​i,j+1))+1; 

有人能帮帮我吗?

推荐答案

计算 256x256 图像的对称灰度共生矩阵<$ c对应于偏移向右一个像素的$ c> img (类 Mat )可以通过OpenCV在Java中实现,如下所示:

The calculation of a 256x256 symmetric gray level co-occurrence matrix of image img (of class Mat) corresponding to an offset "one pixel to the right" may be implemented in Java through OpenCV as follows:

Mat gl = Mat.zeros(256, 256, CvType.CV_64F);
Mat glt = gl.clone();

for (int y = 0; y < img.rows(); y++) {
    for (int x = 0; x < img.cols()-1; x++) {

        int i = (int) img.get(y, x)[0];
        int j = (int) img.get(y, x + 1)[0];

        double[] count = gl.get(i, j);
        count[0]++;
        gl.put(i, j, count);
    }
}

Core.transpose(gl, glt);
Core.add(gl, glt, gl);
Scalar sum = Core.sumElems(gl);
Core.divide(gl, sum, gl);

有很多公开可用的库来计算GLCM并从Java中提取Haralick功能,例如 GLCM2 JFeatureLib 等。

There is a good bunch of publicly available libraries to compute GLCMs and extract Haralick features from them in Java, for example GLCM2, JFeatureLib, etc.

这篇关于如何将GLCM的C ++实现转换为Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆