2014年1月21日 星期二

影像三分法

影像三分法假設 : 若影像中的主角與圖中四個交叉點的距離越近,則代表圖片越美。


方法 :

1. 先將影像依據三分法,找出四個交叉點,並以該四點為中心形成四個block



2. 方法: 接著計算各個block的Saliency map,我們認為Saliency value 最大的block就是該影像主要內容的位置。

Saliency (影像顯著性) 公式 : S(x,y) = | Iu-Iwhc(x,y) |。
S(x,y): 像素在座標(x,y)上的Saliency value。
Iu: 一張相片的平均像素強度( in lab color space)。
Iwhc: 經過高斯模糊後,像素在座標(x,y)上強度值(in lab color space)
參考論文 : R. Achanta, S. Hemami, F. Estrada, and S. Susstrunk. Frequency-tuned salient region detection. In Computer Vision and Pattern Recognition, 2009. CVPR 2009.IEEE  Conference on, pages 1597 –1604, june 2009.


3. 計算最終三分法的值

公式 :  Frot = average( max saliency of block ) / average( saliency of image )

4. 程式碼
環境 : visual studio 2010、opencv 2.4.7
測試資料: 取自下列文獻所提供之資料的前1000張影像
N. Murray, L. Marchesotti, and F. Perronnin, “AVA: A Large-Scale Database for Aesthetic Visual Analysis,” CVPR, 2012.

#include <iostream> // for standard I/O
#include <string>   // for strings
#include <vector>
#include <cmath>
#include <ctime>
#include <fstream>

#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O
#include "opencv2/features2d/features2d.hpp"

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
  fstream fout; 
  fout.open("Frot.txt",ios::out);
  string image_name;
  char arr[50];
  int i,j,k;
  double l,a,b;
  double mean_l,mean_a,mean_b;
  double rows,cols;
  double aver_saliency_max;
  double temp;
  Mat ori_image,img_blur,img_Lab,image_saliency_output ;   
  for(k=0;k<=999;k++)   //1000 test image
  {
    /**********construct Saliency map****************/
    sprintf(arr,"ava_photos/%d.jpg",k);
    image_name.assign(arr);
    cout << image_name << endl;
    ori_image = imread ( image_name,  CV_LOAD_IMAGE_COLOR );  
    cvtColor( ori_image, img_Lab, CV_BGR2Lab );
    GaussianBlur(img_Lab, img_blur, Size(5,5),0,0 );
    mean_l=0;
    mean_a=0;
    mean_b=0;
    rows = ori_image.rows;
    cols = ori_image.cols;
    image_saliency_output = Mat(rows,cols,CV_8U);

    for(i=0;i<rows;i++)
    {
       for(j=0;j<cols;j++)
       {
          mean_l += img_Lab.at<Vec3b> (i,j).val[0];
          mean_a += img_Lab.at<Vec3b> (i,j).val[1];
          mean_b += img_Lab.at<Vec3b> (i,j).val[2];
       }
    }
    mean_l = mean_l/(rows*cols);
    mean_a = mean_a/(rows*cols);
    mean_b = mean_b/(rows*cols);
    for(i=0;i<rows;i++)
    {
       for(j=0;j<cols;j++)
       {
          l = mean_l - img_blur.at<Vec3b> (i,j).val[0];
          a = mean_a - img_blur.at<Vec3b> (i,j).val[1];
          b = mean_b - img_blur.at<Vec3b> (i,j).val[2];
          image_saliency_output.at<uchar> (i,j) = sqrt( l*l + a*a + b*b );
       }
    }
    /*********end Saliency map**************/
    aver_saliency_max = -1;
    /********compute left-up segment average saliency value*********/
    temp = 0;
    for(i=rows/3-rows/6;i<=rows/3+rows/6;i++)
    {
       for(j=cols/3-cols/6;j<=cols/3+cols/6;j++)
       {
          temp += image_saliency_output.at<uchar> (i,j);
       }
    }
    if( temp >= aver_saliency_max )
       aver_saliency_max = temp;
    /*******compute right-up segment average saliency value*********/
    temp = 0;
    for(i=rows/3-rows/6;i<=rows/3+rows/6;i++)
    {
       for(j=cols/3*2-cols/6;j<=cols/3*2+cols/6;j++)
       {
           temp += image_saliency_output.at<uchar> (i,j);
       }
    }
    if( temp >= aver_saliency_max )
       aver_saliency_max = temp;
    /******compute left-down segment average saliency value*********/
    temp = 0;
    for(i=rows/3*2-rows/6;i<=rows/3*2+rows/6;i++)
    {
       for(j=cols/3-cols/6;j<=cols/3+cols/6;j++)
       {
          temp += image_saliency_output.at<uchar> (i,j);
       }
    }
    if( temp >= aver_saliency_max )
       aver_saliency_max = temp;
    /******compute right-down segment average saliency value*******/
    temp = 0;
    for(i=rows/3*2-rows/6;i<=rows/3*2+rows/6;i++)
    {
       for(j=cols/3*2-cols/6;j<=cols/3*2+cols/6;j++)
       {
          temp += image_saliency_output.at<uchar> (i,j);
       }
    }
    if( temp >= aver_saliency_max )
       aver_saliency_max = temp;
    /********compute image's all average saliency value************/
    temp = 0;
    for(i=0;i<rows;i++)
    {
       for(j=0;j<cols;j++)
       {
          temp += image_saliency_output.at<uchar> (i,j);
       }
    }
    /***********************compute Frot***************************/
    aver_saliency_max = aver_saliency_max/temp;
    fout << k << ".jpg " << aver_saliency_max << endl; 
  }
  system("pause");
  return 0;
}


5. final top 5 image of frot in ava_photos images






6. final last 5 image of frot in ava_photos images






沒有留言:

張貼留言