VS实现获取图像灰度共生矩阵

之前用MATLAB写的灰度共生矩阵,忽生想法,将其转为VS来实现。
首先极其简单说一下什么是灰度共生矩阵,我个人的理解是,就是像素间的一种对应关系,详细的讲解网上很多,我就不多说了。
这里的算法为了方便理解,没有处理时,最左边一列没有处理。
环境:WIN10+VS2015+OpenCV3.1.0
首先是最经典的lena图片
这里写图片描述

下面就直接贴代码
VS

#include<opencv2/opencv.hpp>
#include<iostream>
#include <fstream>

using namespace std;
using namespace cv;

void main()
{
    Mat img_ = imread("D:\\lena.bmp", IMREAD_GRAYSCALE);

    Mat img(img_.rows, img_.cols, CV_32SC1);
    img_.convertTo(img, CV_32SC1);

    Mat GLCM(256, 256, CV_32SC4, Scalar(0, 0, 0, 0));

    // 将数据进行保存,与MATLAB进行对比
    ofstream f_img("path\\img.txt");            // 将原始图像转换维int类型进行保存
    ofstream f_0("path\\0.txt");                // 将获取的灰度共生矩阵进行保存
    ofstream f_45("path\\45.txt");          
    ofstream f_90("path\\90.txt");          
    ofstream f_135("path\\135.txt");

    for (int i = 0; i < img.rows; i++)
    {
        for (int j = 0; j < img.cols; j++)
        {
            f_img << img.ptr<int>(i)[j] << "    ";
        }
        f_img << endl;
    }
    f_img.close();

    for (int i = 0; i < img.rows - 1; i++)
    {
        for (int j = 1; j < img.cols - 1; j++)
        {
            GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i)[j + 1])[0]       = (int)(GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i)[j + 1])[0]) + 1;         // 0
            GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j + 1])[1]   = (int)(GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j + 1])[1]) + 1;     // -45
            GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j])[2]       = (int)(GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j])[2]) + 1;         // -90
            GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j - 1])[3]   = (int)(GLCM.at<Vec4i>(img.ptr<int>(i)[j], img.ptr<int>(i + 1)[j - 1])[0]) + 1;     // -135
        }
    }

    for (int i = 0; i < 256; i++)
    {
        for (int j = 0; j < 256; j++)
        {
            f_0   << GLCM.at<Vec4i>(i, j)[0] << "   ";
            f_45  << GLCM.at<Vec4i>(i, j)[1] << "   ";
            f_90  << GLCM.at<Vec4i>(i, j)[2] << "   ";
            f_135 << GLCM.at<Vec4i>(i, j)[3] << "   ";
        }
        f_0   << endl;
        f_45  << endl;
        f_90  << endl;
        f_135 << endl;
    }
    f_0.close();
    f_45.close();
    f_90.close();
    f_135.close();
}

这里将处理后的数据进行保存,方便在MATLAB中对程序进行验证

MATLAB

clc;clear;

I = (rgb2gray(imread('D:\lena.bmp')));

% VS获得数据
img=importdata('path\img.txt');
a0=importdata('path\0.txt');
a45=importdata('path\45.txt');
a90=importdata('path\90.txt');
a135=importdata('Cpath\135.txt');

[m,n] = size(I);
I_0 = zeros(256,256);
I_45 = zeros(256,256);
I_90 = zeros(256,256);
I_135 = zeros(256,256);

for i = 1:m-1
    for j = 2:n-1
        I_0(I(i,j),I(i,j+1)) = I_0(I(i,j),I(i,j+1)) + 1;
        I_45(I(i,j),I(i+1,j+1)) = I_45(I(i,j),I(i+1,j+1)) + 1;
        I_90(I(i,j),I(i+1,j)) = I_90(I(i,j),I(i+1,j)) + 1;
        I_135(I(i,j),I(i+1,j-1)) = I_135(I(i,j),I(i+1,j-1)) + 1;
    end
end

[max(max(I_0)) max(max(I_45)) max(max(I_90)) max(max(I_135))]           % MATLAB中各个方向灰度共生的最大值
[max(max(a0)) max(max(a45)) max(max(a90)) max(max(I_135))]          % VS中各个方向灰度共生的最大值

结果

这是VS和MATLAB处理后的结果,可以看出,VS程序是正确的。
下一步用VS实现图像灰度级降级处理。

打包好的文件下载链接:http://download.csdn.net/download/oemt_301/10117142

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值