反射投影直方图以检测待定的图像内容

#include"stdafx.h"
#include 
using namespace std;

#include "opencv\cv.h"
#include "opencv\highgui.h"

#include "histogram.h"
#include "objectFinder.h"
#include "colorhistogram.h"
#include"ContentFinder.h"

int main()
{
	// Read input image
	ColorHistogram h;
	cv::Mat image = cv::imread("waves.jpg", -1);
	cv::namedWindow("color");
	cv::imshow("color", image);
	image = h.colorReduce(image, 32);
	if (!image.data)
		return 0;

	// define image ROI
	cv::Mat imageROI;
	imageROI = image(cv::Rect(360, 55, 40, 50)); // Cloud region

	// Display reference patch
	cv::namedWindow("Reference");
	cv::imshow("Reference", imageROI);

	// Find histogram of reference
	
	cv::MatND hist = h.getHistogram(imageROI);
	
	// Create the objectfinder
	ContentFinder finder;
	finder.setHistogram(hist);
	finder.setThreshold(0.5f);
	// Get back-projection
	int minValue = 0;
	int maxValue = 255;
	int channels[3] = { 0, 1, 2 };
	int dim = 3;
	
	cv::Mat result= finder.find(image,minValue,maxValue,channels,dim);
	cv::namedWindow("result");
	cv::imshow("result", result);
	

	cv::waitKey();
	return 0;
}
///////ContentFinder.h
#pragma once
#include "opencv2\opencv.hpp"
#include"opencv\highgui.h"
class ContentFinder
{
public:
 
 ~ContentFinder();
private:
 float hranges[2];
 const float*ranges[3];
 int channels[3];
 float threshold;
 cv::MatND histogram;
public:
 ContentFinder() :threshold(-1.0f){
  hranges[0] = 0.0;
  hranges[1] = 255.0;
  ranges[0] = hranges;
  ranges[1] = hranges;
  ranges[2] = hranges;
 }
 void setThreshold(float t){
  threshold = t;
 }
 float getThreshold(){
  return threshold;
 }
 void setHistogram(const cv::MatND& h){
  histogram = h;
  cv::normalize(histogram, histogram, 1.0);
 }
 cv::Mat find(const cv::Mat& image,
  float minValue, float maxValue,
  int* channels, int dim){
  cv::Mat result;
  hranges[0] = minValue;
  hranges[1] = maxValue;
  for (int i = 0; i < dim; i++)
   this->channels[i] = channels[i];
  
  cv::calcBackProject(&image, 1,channels, histogram, result, ranges, 255.0); 
  if (threshold>0.0)
   cv::threshold(result, result, 255 * threshold, 255, cv::THRESH_BINARY);
  return result;
 }
 
};





你可能感兴趣的:(opencv)