2017년 8월 15일 화요일

opencv histogram equalize, CDF graph 출력


















1. histogram (PDF) 구하고
2. CDF 구하고
3. Y(imageout) = CDF(imagein)


#pragma warning(disable: 4819)

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

using namespace cv;
using namespace std;

int main(void)
{
    Mat src = imread("lenna.bmp", IMREAD_GRAYSCALE);if (src.empty()) {      cerr << "Image load failed!" << endl;       return -1;  }
    
    int hist[256] = { 0, };
    for (int j = 0; j < src.rows; j++) {
        for (int i = 0; i < src.cols; i++) {
            hist[src.at<uchar>(j,i)]++;
        }
    }
    int hist_max = 0;
    for (int i = 0; i < 256; i++) {
        if (hist[i] > hist_max)
            hist_max = hist[i];
    }
    Mat histogram_original;
    histogram_original.create(256,256, CV_8U);
    histogram_original.setTo(0);
    int N=0;
    for (int i = 0; i < 256; i++) {
        int v = (double) (255.* hist[i])/ hist_max;
        v = cvRound(v);
        for (int j = 0; j < v; j++) {
            histogram_original.at<uchar>(255-j, i) = 255;
        }
    }
    int CDF[256] = { 0, };
    CDF[0] = 0;
    for (int i = 1; i < 256; i++) {
        CDF[i] = hist[i] + CDF[i - 1];
    }
    //CDF normalize
    for (int i = 1; i < 256; i++) {
        CDF[i] = cvRound(255.*CDF[i]/CDF[255]);
    //  CDF[i] = 255.*CDF[i] / CDF[255];
    }
    Mat CDFdrawing;
    CDFdrawing.create(256, 256, CV_8U);
    CDFdrawing.setTo(255);
    for (int i = 0 ; i < 256; i++) {
        line(CDFdrawing, Point(i, 255 - CDF[i]), Point(i, 255 - CDF[i]), 0);
    }
    //imshow("dst", dst);
    //CDF function Y(output) = CDF(input)

    Mat src2 = src.clone();
    Mat dst(src2.rows, src2.cols, src2.type());
    for (int i = 0; i < src2.rows; i++) {
        for (int j = 0; j < src2.cols; j++) {
            dst.at<uchar>(j, i) = CDF[(uchar)src2.at<uchar>(j, i)];
        }
    }
    imshow("src", src);
    imshow("histogram_original", histogram_original);
    imshow("CDFdrawing", CDFdrawing);
    imshow("dst", dst);


    //check equalized Image historam, CDF
    
    int hist2[256] = { 0, };
    for (int j = 0; j < dst.rows; j++) {
        for (int i = 0; i < dst.cols; i++) {
            hist2[dst.at<uchar>(j, i)]++;
        }
    }
    int hist2_max = 0;
    for (int i = 0; i < 256; i++) {
        if (hist2[i] > hist2_max)
            hist2_max = hist2[i];
    }
    Mat histogram_equalized;
    histogram_equalized.create(256, 256, CV_8U);
    histogram_equalized.setTo(0);
    int N2 = 0;
    for (int i = 0; i < 256; i++) {
        int v = (double)(255.* hist2[i]) / hist2_max;
        v = cvRound(v);
        for (int j = 0; j < v; j++) {
            histogram_equalized.at<uchar>(255 - j, i) = 255;
        }
    }
    int CDF2[256] = { 0, };
    CDF2[0] = 0;
    for (int i = 1; i < 256; i++) {
        CDF2[i] = hist2[i] + CDF2[i - 1];
    }
    //CDF normalize
    for (int i = 1; i < 256; i++) {
        CDF2[i] = cvRound(255.*CDF2[i] / CDF2[255]);
        //  CDF[i] = 255.*CDF[i] / CDF[255];
    }
    Mat CDFdrawing2;
    CDFdrawing2.create(256, 256, CV_8U);
    CDFdrawing2.setTo(255);
    for (int i = 0; i < 256; i++) {
        line(CDFdrawing2, Point(i, 255 - CDF2[i]), Point(i, 255 - CDF2[i]), 0);
    }
    imshow("histogram_equalized", histogram_equalized);
    imshow("CDF_equalized", CDFdrawing2);
    waitKey(0);
    return 0;
}

댓글 없음:

댓글 쓰기