Ask Your Question
0

Swap Histogram of two different images..

asked 2013-07-09 02:30:11 -0600

images gravatar image

updated 2020-09-24 14:49:49 -0600

Hi, I have two different images (Image A and Image B), whose histograms (histImage and histImage1) i have already computed..
Now I want that the histogram of Image A becomes the histogram of Image B.. so that the Image B gets the colors similar to Image A... code is as follow:

#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("C:/Users/Waqas/Desktop/19juneexp/4.jpg", 1 );   // Image A
  src1 = imread("C:/Users/Waqas/Desktop/19juneexp/body.jpg", 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 ...
(more)
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-07-09 03:49:53 -0600

berak gravatar image

that won't work in rgb!

you want all light / shadow effects on the body to stay as they are.

i'd convert both images to hsv first, then swap only the hue component.

(or, make a histogram of src-hue only, and take the most prominent value there)

edit flag offensive delete link more

Comments

http://shrib.com/c8kXPbFC this is what i am doing... i have separated hsv values for both the image...but how can i swap hue components and convert back to BGR format.. ??

images gravatar imageimages ( 2013-07-09 05:01:28 -0600 )edit

cool pad ;)

but i'm Not going to write your program, dude (unless, ofc, you pay me!) ;)

  • make a histogram of your hsv-face image, for the H channel, only
  • same for body
  • apply minMaxLoc on both histogram, to get the most prominent H-colors in each img
  • then walk through your body-pixels, check if the original H value there is in a reasonable small distance to the "most prominent" color (else it's probably hair/background) . if so, replace ( or interpolate to) the "most prominent" face - hue value. ( they all get the same, you don't need to run over your face-pixels )

  • cvtconvert back to bgr

berak gravatar imageberak ( 2013-07-09 05:45:15 -0600 )edit
0

answered 2013-07-09 02:55:59 -0600

Ben gravatar image

There is a very simple way: copy ImageA's content to ImageB. Done :) Any permutation of pixel coordinates in ImageA will do it, too.

Maybe you have 2 example images to demonstrate what you actually want to achieve? You probably want to adjust one image's contrast/saturation/whatever in order to get a histogram similar to an other image, which is representing the same scene, right? Because I can't think of a useful case where you wnat to get exactly the same histogram for for 2 totally different images.

edit flag offensive delete link more

Comments

hey thanks for the reply,
http://answers.opencv.org/question/15455/how-to-perform-skin-tone-matching/

this is what i actually want to achieve... basically i have two images 1) image of a face 2) image of a body.. i want to change the contrast/saturation/whatever of the body image with respect to the face image so that it matches the skin tone

images gravatar imageimages ( 2013-07-09 03:07:43 -0600 )edit

i have already extracted the skin from the face image...this is to ignore the hair/beard color

images gravatar imageimages ( 2013-07-09 03:10:12 -0600 )edit

Question Tools

Stats

Asked: 2013-07-09 02:30:11 -0600

Seen: 593 times

Last updated: Jul 09 '13