【问题标题】:Swap Histogram of two different images交换两个不同图像的直方图
【发布时间】:2013-07-09 07:33:04
【问题描述】:

我有两个不同的图像(图像 A 和图像 B),我已经计算了它们的直方图(histImage 和 histImage1)。 现在我希望图像 A 的直方图成为图像 B 的直方图。这样图像 B 就会得到与图像 A 相似的颜色。 代码如下:

    #include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main( )
{
  Mat src, dst, src1;

  /// Load image
  src = imread("ImageA", 1 );   // Image A
  src1 = imread("ImageB", 1 ); // Image B

  if( !src.data )
    { return -1; }

  /// Separate the image in 3 places ( B, G and R )
  vector<Mat> bgr_planes;
   vector<Mat> bgr_planes1;
  split( src, bgr_planes );
  split( src1, bgr_planes1 );

  /// Establish the number of bins
  int histSize = 256;

  /// 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;  //ImageA
  Mat b_hist1, g_hist1, r_hist1; //ImageB

  /// Compute the histograms of Image A
  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 );
   /// Compute the histograms of Image B
    calcHist( &bgr_planes1[0], 1, 0, Mat(), b_hist1, 1, &histSize, &histRange, uniform, accumulate );
  calcHist( &bgr_planes1[1], 1, 0, Mat(), g_hist1, 1, &histSize, &histRange, uniform, accumulate );
  calcHist( &bgr_planes1[2], 1, 0, Mat(), r_hist1, 1, &histSize, &histRange, uniform, accumulate );


  // Draw the histograms for B, G and R
  int hist_w = 512; int hist_h = 400;   //Image A
  int bin_w = cvRound( (double) hist_w/histSize ); //Image A
   int hist_w1 = 512; int hist_h1 = 400;  //Image B
    int bin_w1 = cvRound( (double) hist_w1/histSize );//Image B

  Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );  //ImageA
    Mat histImage1( hist_h1, hist_w1, CV_8UC3, Scalar( 0,0,0) ); //ImageB

  /// Normalize the result to [ 0, histImage.rows ] ImageA
  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() );
    /// Normalize the result to [ 0, histImage.rows ] ImageB
    normalize(b_hist1, b_hist1, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  normalize(g_hist1, g_hist1, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
  normalize(r_hist1, r_hist1, 0, histImage.rows, NORM_MINMAX, -1, Mat() );


  /// Draw for each channel  ImageA
  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  );
  }
  ////////////////////////////////////////////////////
  /// Draw for each channel  ImageB
  for( int i = 1; i < histSize; i++ )
  {
      line( histImage1, Point( bin_w1*(i-1), hist_h1 - cvRound(b_hist1.at<float>(i-1)) ) ,
                       Point( bin_w1*(i), hist_h1 - cvRound(b_hist1.at<float>(i)) ),
                       Scalar( 255, 0, 0), 2, 8, 0  );
      line( histImage1, Point( bin_w1*(i-1), hist_h1 - cvRound(g_hist1.at<float>(i-1)) ) ,
                       Point( bin_w1*(i), hist_h1 - cvRound(g_hist1.at<float>(i)) ),
                       Scalar( 0, 255, 0), 2, 8, 0  );
      line( histImage1, Point( bin_w1*(i-1), hist_h1 - cvRound(r_hist1.at<float>(i-1)) ) ,
                       Point( bin_w1*(i), hist_h1 - cvRound(r_hist1.at<float>(i)) ),
                       Scalar( 0, 0, 255), 2, 8, 0  );
  }
  /////////////////////////////////////////////////////

  /// Display
  namedWindow("calcHist", CV_WINDOW_AUTOSIZE );
  imshow("face ", histImage );  //Histogram of Image A
   /// Display
  namedWindow("calcHist1", CV_WINDOW_AUTOSIZE );
  imshow("body ", histImage1 ); //Histogram of Image B

  waitKey(0);

  return 0;
}

【问题讨论】:

  • 您在这方面有什么特别的问题吗?
  • 不,到目前为止代码运行良好..
  • stackoverflow.com/questions/17207916/… 这是我真正想要实现的......基本上我有两个图像 1)脸部图像 2)身体图像..我想改变对比度/饱和度/任何关于面部图像的身体图像,使其与肤色相匹配
  • 我同意您所指的论坛上的帖子。检测第一张图像中的人脸或所有皮肤区域,然后仅提取这些像素的直方图。接下来,检测第二张图像中的皮肤并提取直方图。最后,像上面那样交换直方图。
  • 我已经从两个图像中提取了皮肤区域....实际上我被困在最后一步,交换直方图..上面的代码只计算两个直方图....跨度>

标签: opencv image-processing histogram


【解决方案1】:

交换直方图的一种方法是遵循histogram equalisation 中使用的方法。

  • 分别计算两个图像(I1 和 I2)的直方图(H1 和 H2)并对其进行归一化(已在您的代码中完成)。
  • 计算累积直方图 - 也称为累积分布函数 - C1 和 C2 对应于 H1 和 H2,如 here 所述。
  • here 所述,使用累积直方图 C2 为 I1 中的每个像素替换新值。
  • 使用累积直方图 C1 对 I2 中的每个像素执行相同操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多