【问题标题】:errors occuring while compiling the code for histogram equalization [closed]编译直方图均衡代码时发生的错误[关闭]
【发布时间】:2017-03-12 08:44:01
【问题描述】:

我正在尝试实现自己的直方图均衡,而不使用 opencv c++ 2.4.13.2 版本中的直方图均衡库例程。 我在终端编译程序如下:

g++ `pkg-config --cflags opencv` histogram_Equalization.cpp `pkg-config --libs opencv` -o histeq

我收到如下错误:

这是我的代码:

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
using namespace cv;
Mat *calculateHist(Mat &M, Mat &He)
{
    float P[256]={0};
    int i, j, k, r;
    float sum = 0.0;
    float T[256]={0}; 
    int S[256]={0};
    for(i=0;i<M.rows;i++)
    {
        for(j=0;j<M.cols;j++)
        {
            int tmp = M.at<uchar>(i,j); 
            P[tmp] = P[tmp] +1; //[(M.at<uchar>(i,j))] + 1;
        }
    }
    for (i=0;i<M.rows;i++)
    {
        for(j=0;j<M.cols;j++)
        {
            sum = sum + P[(M.at<uchar>(i,j))];
        }
    }
    //int num_pixel = (M.rows)*(M.cols)
    for(i=0;i<256;i++)
    {
        P[i] = P[i]/(sum); 
    }
    T[0] = P[0];
    for( k=1; k<256;k++)
    {
        T[k] = T[k-1] + P[k];
    }
    for( r=0; r< 256; r++)
    {
        S[r] = cvRound(T[r]*255);
    }
    for(i=0;i<M.rows;i++)
    {
        for(j=0;j<M.cols;j++)
        {
            r = M.at<uchar>(i, j);
            He.at<uchar>(i,j) = S[r];
        }
    }
    return (&He);
}
int main(int argc, char *argv[])
{

    Mat image = imread(argv[1],0);
    Mat He(image.size,image.type);

    He = calculateHist(image, He);
    imshow("original_image", image);
    imshow( "histogram_equalized", He );


    waitKey(0);
    return 0;
}

【问题讨论】:

  • 你有什么问题?
  • 因为你的方法的返回类型是Mat *指向一个Mat的指针,但是你使用的是He = calculateHist(image, He);,其中He不是一个指针
  • 抱歉打错了 He = calculateHist(&image, &He) 并且在这个函数的声明中是 Mat* CalculateHist(Mat *M, Mat *He)

标签: c++ opencv image-processing digital


【解决方案1】:

sizetypefunctions of the class Mat,而不是属性,因此您必须使用 size()type() 调用它们。

正如 ZdaR 在他的评论中所说,calculateHist 返回一个指向修改后图像的指针。这里其实没必要,因为既然你传了Heby reference,原来的对象就被函数修改了。

所以,这应该可行:

Mat He(image.size(),image.type());
calculateHist(image, He);

【讨论】:

    【解决方案2】:

    我自己纠正了错误,这里是代码:

    #include <opencv2/opencv.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include<opencv2/highgui/highgui.hpp>
    #include<opencv2/core/core.hpp>
    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    using namespace cv;
    
    int main(int argc, char *argv[])
    {
    
        Mat image = imread(argv[1],0);
        Mat He(image.rows, image.cols, CV_8U);
        float P[256]={0};
        int i, j, k, r;
        float sum = 0.0;
        float T[256]={0}; 
        int S[256]={0};
    
        /* This is to calculate the frequency of each pixel value in the range 0-255*/
        for(i=0;i<image.rows;i++)
        {
            for(j=0;j<image.cols;j++)
            {
                int tmp = image.at<uchar>(i,j); 
                P[tmp] = P[tmp] +1; //[(M.at<uchar>(i,j))] + 1;
            }
        }
         /*this part of the code is to find the total number of all the frequency of each pixel and then divide each freq val by this sum*/  
    
            for(j=0;j<256;j++)
            {
                sum = sum + P[j];
            }
    
        for(i=0;i<256;i++)
        {
            P[i] = P[i]/(sum); 
        }
        /*calculation of the  cdf*/
    
        T[0] = P[0];
        for( k=1; k<256;k++)
        {
            T[k] = T[k-1] + P[k];
        }
        /*multiply it with the Level-1 here L=256*/
        for( r=0; r< 256; r++)
        {
            S[r] = cvRound(T[r]*255);
        }
        /*mapping of each pixel value to the new based on the values in S array and assign it to the output image He*/
        for(i=0;i<image.rows;i++)
        {
            for(j=0;j<image.cols;j++)
            {
                r = image.at<uchar>(i, j);
                He.at<uchar>(i,j) = S[r];
            }
        }
        imshow("original_image", image);
        imshow( "histogram_equalized", He );
    
    
        waitKey(0);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多