Opencv study notes (x) histogram back projection calcBackProject ()

principle:

In general, we grayscale image into several grades intervals, calculating the pixel value of the gradation values ​​of the image pixels corresponding to where each section, we can obtain the image histogram.

And an image histogram according to a reverse projection matrix is ​​a matrix composed of pixel values.

Specific reference: Calculation reverse projection matrix

Join us back projection matrix are as follows:

back_Projection=

4    4    4    4

4    6    6    6

2    2    4    4

4    6    6    6

The value of a point is the back projection matrix histogram value interval corresponding to the original image in which the point is located, it is easy to see, the more pixels located in a range, which is reflected in the brighter the image.

Alt

work process:

Suppose we have a 100x100 input image, there is a 10x10 image of the template, the search process is as follows:
(1) starting from the upper left corner (0,0) of the input image, cutting one (0,0) to (10, 10) the temporary image;
(2) generates a histogram of the temporary image;
(3) comparative histogram and a histogram template image temporary image, referred to as comparative results C;
(. 4) histogram comparison results C, is the result image pixel value at (0,0);
(5) cutting the temporary image from the input image (0,1) to (10,11), the contrast histogram and to record the image result;
(6) repeat (1) ~ (5) step until the lower right corner of the input image.

Related API: calcBackProjection () function

void calcBackProject(
const Mat* images ,

int nimages,

const int* channels ,

InputArray hist ,

OutputArray backProject ,

const float ranges**

double scale=1:

bool uniform=true
)

Parameter Description

1, the input image, the image bit depth must be one kind CV_8U, CV_16U CV_32F or the same size, each image can have any number of channels
2, the number of the input image
3, the channel list for calculating backprojection the number of channels must match the dimensions of the histogram, a first channel array from 0 to the Image [0] .channels () -. 1
. 4, the histogram of the input, the histogram bin may be dense (dense) or sparse (sparse)
. 5, outputs the target back-projection image, is a single-channel image, the original image has the same size and depth
6, the histogram bin for each dimension in the range
7, selectable output ratio backprojection factor
8, the histogram distribution is uniform (uniform) identifier, with a default value of true

Steps for usage:

1, the image is converted into HSV, extract the channel h;

2, using calcHist () function calculates the histogram, and using the normalized treatment normalize.

3 ,, back projection calcBackProject ()

Code demonstrates:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;

Mat src; Mat hsv; Mat hue; 
int bins = 12;
void Hist_And_Backprojection(int, void*);
int main(int argc, char** argv)
 {
 src = imread("D:/vcprojects/images/t1.jpg");
 if (src.empty())
  {
  printf("could not load image...\n");
  return -1;
 }
 const char*  window_image = "input image";
 namedWindow(window_image, CV_WINDOW_NORMAL);
 namedWindow("BackProj", CV_WINDOW_NORMAL);
 namedWindow("Histogram", CV_WINDOW_NORMAL);

cvtColor(src, hsv, CV_BGR2HSV);
 hue.create(hsv.size(), hsv.depth());
 int nchannels[] = { 0, 0 };
 mixChannels(&hsv, 1, &hue, 1, nchannels, 1);
createTrackbar("Histogram Bins:", window_image, &bins, 180, Hist_And_Backprojection);
 Hist_And_Backprojection(0, 0);

imshow(window_image, src);
 waitKey(0);
 return 0;
}

void Hist_And_Backprojection(int, void*)
 {
 float range[] = { 0, 180 };
 const float *histRanges = { range };
 Mat h_hist;
 calcHist(&hue, 1, 0, Mat(), h_hist, 1, &bins, &histRanges, true, false);
 normalize(h_hist, h_hist, 0, 255, NORM_MINMAX, -1, Mat());

Mat backPrjImage;
 calcBackProject(&hue, 1, 0, h_hist, backPrjImage, &histRanges, 1, true);
 imshow("BackProj", backPrjImage);

int hist_h = 400;
 int hist_w = 400;
 Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));
 int bin_w = (hist_w / bins);
 for (int i = 1; i < bins; i++) 
 {
  rectangle(histImage, 
   Point((i - 1)*bin_w, (hist_h - cvRound(h_hist.at<float>(i - 1) * (400 / 255)))),
   //Point(i*bin_w, (hist_h - cvRound(h_hist.at<float>(i) * (400 / 255)))),
   Point(i*bin_w, hist_h),
   Scalar(0, 0, 255), -1);
 }
 imshow("Histogram", histImage);
 return;
}

Readers want to be helpful, like it can look at my public, then I will study notes made in the above, we will be able to learn together!

Here Insert Picture Description
Alt

Published 12 original articles · won praise 9 · views 3628

Guess you like

Origin blog.csdn.net/Rosen_er/article/details/104212684