OpenCV 彩色圖像的直方圖均衡化、平滑和銳化 C++

前面已經介紹過灰度圖像的平滑和銳化,下面使用均值平滑和拉普拉斯銳化處理彩色圖像。

平滑結果:

平滑差異和銳化結果

 

代碼實現:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;

int main() {

    Mat src = imread("lena.jpg");

    imshow("src", src);
    std::vector<Mat>    yuv;
    Mat split_img;
    cvtColor(src, split_img, COLOR_BGR2YCrCb);
    split(split_img, yuv );
    blur(yuv[0], yuv[0], Size(5, 5));
    merge( yuv, split_img);
    cvtColor(split_img, split_img, COLOR_YCrCb2BGR);
    imshow( "split_img", split_img);
    Mat dst;
    blur(src, dst, Size(5, 5));
    namedWindow("box");
    imshow("box", dst);

    Mat kernel = (Mat_<float>(3, 3) << 0, -1, 0, -1, 4, -1, 0, -1, 0);
    Mat laplace_dst;
    filter2D(src, laplace_dst, CV_8UC3, kernel);
    imshow( "lap_dst", laplace_dst + src);
    imshow( "laplace", laplace_dst);
    Mat sub = dst - split_img;
    imshow( "sub", sub*20);

    waitKey(0);
    return 0;
}

 

直方圖均衡化亮度和原圖像對比,木桌子的紋理已經看出來了;

代碼實現:

/*
 * color_equalizehist.cpp
 *
 *  Created on: May 20, 2018
 *      Author: cyf
 */


#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

void histogram_calculation( const Mat &Image, Mat &histoImage )
{
    // Create the histogram for 256 bins
    // The number of possibles values
    int histSize= 255;

    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 } ;
    const float* histRange = { range };

    bool uniform = true;
    bool accumulate = false;

    Mat b_hist, g_hist, r_hist;

    vector<Mat>  bgr_planes;
    split( Image, bgr_planes );

    calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize,
            &histRange, uniform, accumulate );
    calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize,
            &histRange, uniform, accumulate );
    calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize,
            &histRange, uniform, accumulate );

    // Draw the histogram
    // We go to draw lines for each channel
    int hist_w= 512;
    int hist_h= 400;

    int bin_w= cvRound((float)hist_w/(float)histSize);

    // Create image with gray base
    Mat histImage( hist_h, hist_w, CV_8UC3, Scalar(0,0,0) );

    // Normalize the histograms to height of image
   normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
   normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
   normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

   for( int i=1; i< histSize; i++){
       line( histImage,
               Point( bin_w*(i-1), hist_h-cvRound(b_hist.at<float>(i-1) ) ),
               Point( bin_w*(i), hist_h-cvRound(b_hist.at<float>(i) ) ),
               Scalar(255,0,0), 2, 8, 0
           );
       line( histImage,
               Point( bin_w*(i-1), hist_h-cvRound(g_hist.at<float>(i-1) ) ),
               Point( bin_w*(i), hist_h-cvRound(g_hist.at<float>(i) ) ),
               Scalar(0,255,0), 2, 8, 0
           );
       line( histImage,
               Point( bin_w*(i-1), hist_h-cvRound(r_hist.at<float>(i-1) ) ),
               Point( bin_w*(i), hist_h-cvRound(r_hist.at<float>(i) ) ),
               Scalar(0,0,255), 2, 8, 0
           );
   }

//   imshow("Histogram", histImage);
   histoImage = histImage;

}

int main( int argc, char *argv[] )
{
    Mat src, ycrcb, imageq;
    Mat histImage;

    //get src image
    src = imread( "637.tif" );
    if( src.empty() ){
        fprintf( stderr, "Error image, can't load\n" );
        exit(-1);
    }
    resize(src,src,Size(), 0.5, 0.5);
    imshow( "Source image", src );
    //calculate src channel equalize
    histogram_calculation( src, histImage);
    imshow( "Color Image Histogram", histImage );

    vector<Mat> yuv;
    cvtColor( src, ycrcb, COLOR_BGR2YCrCb);
    split( ycrcb, yuv );

    //equalize Y
    equalizeHist( yuv[0], yuv[0] );

    //merge channels
    merge( yuv, imageq);
    cvtColor( imageq, imageq, COLOR_YCrCb2BGR );
    imshow( "Equalized image", imageq );

    //calculate every channel histogram
    histogram_calculation( imageq, histImage );
    imshow( "Equalized color image histogram", histImage );

    waitKey(0);
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章