2017-03-12 1 views
-4

J'essaie d'implémenter ma propre égalisation d'histogramme sans utiliser la routine de bibliothèque pour l'égalisation de l'histogramme dans la version opencv C++ 2.4.13.2. Je compilé le programme dans le terminal comme suit:erreurs lors de la compilation du code pour l'égalisation de l'histogramme

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

je reçois les erreurs comme indiqué ci-dessous: errors

Voici le code de la mine:

#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; 
} 
+1

Quelle est votre question? – Rakete1111

+1

Parce que le type de retour de votre méthode est le pointeur 'Mat *' vers un Mat, mais alors vous utilisez 'He = calculateHist (image, He);', où 'He' n'est pas un Pointer – ZdaR

+0

Désolé c'est une faute de frappe He = calculateHist (& image, & He) et dans la déclaration de cette fonction est Mat * CalculateHist (Mat * M, Mat * He) –

Répondre

1

size et type sont functions of the class Mat, pas les attributs, donc vous devez les appeler avec size() et type().

Comme ZdaR l'a indiqué dans son commentaire, calculateHist renvoie un pointeur vers l'image modifiée. Ce n'est pas vraiment nécessaire ici, car puisque vous passez Heby reference, l'objet original est modifié par la fonction.

Donc, cela devrait fonctionner:

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

I GOT les erreurs corrigées et moi-même est le code ici:

#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; 
}