2013-04-02 2 views
0

안녕하세요 누구나 두 개의 RGB 이미지를로드하고 회색조로 변환하고 히스토그램을 계산 한 다음 히스토그램을 비교하는 간단한 오픈 cv 프로그램을 제공 할 수 있습니까? 유사한 프로그램이 열린 cv 사이트에서 수행 된 것을 보았지만 그레이 스케일 대신 HSV를 사용했고 C++ 프로그램이었습니다. 내가 사용하고 그 인수가 .... 감사를 무엇을 의미 할 기능 모른다 ... 흐름과 모든 것을 볼 수, 키란 여기opencv에서 그레이 스케일 이미지의 히스토그램을 비교하십시오.

+0

"히스토그램 비교"는 무슨 뜻입니까? – brotherofken

답변

10

는 일을 간단한 코드입니다. 어떻게 히스토그램을 비교할 것인지 말하지 않았기 때문에 시각적으로 할 것을 제안합니다.

#include <opencv2/opencv.hpp> 

void show_histogram(std::string const& name, cv::Mat1b const& image) 
{ 
    // Set histogram bins count 
    int bins = 256; 
    int histSize[] = {bins}; 
    // Set ranges for histogram bins 
    float lranges[] = {0, 256}; 
    const float* ranges[] = {lranges}; 
    // create matrix for histogram 
    cv::Mat hist; 
    int channels[] = {0}; 

    // create matrix for histogram visualization 
    int const hist_height = 256; 
    cv::Mat3b hist_image = cv::Mat3b::zeros(hist_height, bins); 

    cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges, true, false); 

    double max_val=0; 
    minMaxLoc(hist, 0, &max_val); 

    // visualize each bin 
    for(int b = 0; b < bins; b++) { 
     float const binVal = hist.at<float>(b); 
     int const height = cvRound(binVal*hist_height/max_val); 
     cv::line 
      (hist_image 
      , cv::Point(b, hist_height-height), cv::Point(b, hist_height) 
      , cv::Scalar::all(255) 
      ); 
    } 
    cv::imshow(name, hist_image); 
} 

int main (int argc, const char* argv[]) 
{ 
    // here you can use cv::IMREAD_GRAYSCALE to load grayscale image, see image2 
    cv::Mat3b const image1 = cv::imread("C:\\workspace\\horse.png", cv::IMREAD_COLOR); 
    cv::Mat1b image1_gray; 
    cv::cvtColor(image1, image1_gray, cv::COLOR_BGR2GRAY); 
    cv::imshow("image1", image1_gray); 
    show_histogram("image1 hist", image1_gray); 

    cv::Mat1b const image2 = cv::imread("C:\\workspace\\bunny.jpg", cv::IMREAD_GRAYSCALE); 
    cv::imshow("image2", image2); 
    show_histogram("image2 hist", image2); 

    cv::waitKey(); 
    return 0; 
} 

결과 :

enter image description here

+0

감사합니다. 많은 친구 ... 미안하지만 막연한 사람이라면, 내가 찾던 것을 준 것입니다. 다시 감사합니다. – user2236862

+2

대답을 수락하십시오. 고맙습니다. – brotherofken

관련 문제