Why do I get bad results with calcBackProject in opencv? - c++

I create a project to reduce noise in the target image. I want to do something on Histograms, so I use calcHist to get the histogram of the image. When I use calcBackProject to recover images, the results are very different from the source image that I can't understand. The results show that the histogram of the two images is significant differences. Are there some problems with histogram processing?
By the way, my OpenCV version is 4.5.0.
int main()
{
Mat src = imread("./newpaperback.jpg");
imshow("src", src);
Mat gray;
cvtColor(src, gray, COLOR_BGR2GRAY);
imshow("gray", gray);
const int channels[] = {0};
Mat hist;
int dims = 1;
const int histSize[] = {256};;
float granges[] = {0, 256};
const float* ranges[] = { granges };
calcHist(&gray, 1, channels, Mat(), hist, dims, histSize, ranges, true, false);
int hist_height = 256;
Mat hist_image = Mat::zeros(hist_height, hist_height * 2, CV_8UC3);
double max_val;
minMaxLoc(hist, 0, &max_val, 0, 0);
for(int i = 0; i < hist_height; i++)
{
float bin_val = hist.at<float>(i);
segment[i] = bin_val * hist_height / max_val;
int intensity = bin_val * hist_height / max_val;
rectangle(hist_image, Point(i * 2, hist_height - 1), Point((i + 1) * 2 - 1, hist_height - intensity), Scalar(255, 255, 255));
}
imshow("hist", hist_image);
Mat back_img;
calcBackProject(&gray, 1, channels, hist, back_img, ranges, 1, true);
imshow("back", back_img);
waitKey(0);
return 0;
}

Related

Merging two 1d histograms into one

I got two histograms which I created with standard openCV function calcHist:
int getModels(string filename) {
Mat src = imread(filename, 1);
if(!src.data) { return -1; }
Mat imageHSV;
cvtColor(src, imageHSV, COLOR_BGR2HSV);
vector<Mat> bgr_planes;
split(imageHSV, bgr_planes);
int histSize = 256;
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat g_hist, r_hist;
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 );
cv::Mat combined_hist = g_hist + r_hist;
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
for( int i = 0; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), cvRound(combined_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), cvRound(combined_hist.at<float>(i)) ),
Scalar( 19,90,87), 2, 8, 0 );
}
/// Display
namedWindow(filename, CV_WINDOW_AUTOSIZE );
imshow(filename, histImage );
return 0;
}
Is there any way to merge them into one combined_hist histogram?
Yes, you can. OpenCV has cv::add which can be used like this:
cv::Mat combined_hist;
cv::add(g_hist, r_hist, combined_hist);
However, since cv::Mat has overloaded operators, you can just do this:
cv::Mat combined_hist = g_hist + r_hist;
Hope that helps you.

OpenCV CompareHist of two images returns higher value than expected

I implemented OpenCV CompareHist method in Objective-C to compare similarity of two images:
Image 1
and
Image 2
)
But it returns wrong result.
It returns 0.999971, when I expect that it would be less than 0.9.
- (bool) someMethod:(UIImage *)image :(UIImage *)temp {
cv::Mat src_base, hsv_base;
cv::Mat src_test1, hsv_test1;
src_base = [self cvMatWithImage:image];
src_test1 = [self cvMatWithImage:temp];
cv::cvtColor(src_base, hsv_base, cv::COLOR_BGR2HSV);
cv::cvtColor(src_test1, hsv_test1, cv::COLOR_BGR2HSV);
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges };
// Use the o-th and 1-st channels
int channels[] = { 0, 1 };
/// Histograms
cv::MatND hist_base;
cv::MatND hist_half_down;
cv::MatND hist_test1;
cv::MatND hist_test2;
calcHist( &hsv_base, 1, channels, cv::Mat(), hist_base, 2, histSize, ranges, true, false );
normalize( hist_base, hist_base, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );
calcHist( &hsv_test1, 1, channels, cv::Mat(), hist_test1, 2, histSize, ranges, true, false );
normalize( hist_test1, hist_test1, 0, 1, cv::NORM_MINMAX, -1, cv::Mat() );
double base_test1 = compareHist( hist_base, hist_test1, 0);
NSLog(#"%f", base_test1);
if (base_test1>0.9) {
return true;
}
return false;
}

Multiple template matching only detects one match

I'm trying to match this image
in this image
However, I can't find more than one boss enemy. What do I need to do to find the others?
Image Loading
struct XYposition{
float X;
float Y;
};
std::vector<cv::Mat> bossList;
std::string bossStrings[1] = { "sprites\\boss\\bossUp.png" };
for (int i = 0; i < 1; i++){
cv::Mat pic = cv::imread(bossStrings[i], CV_LOAD_IMAGE_GRAYSCALE);
bossList.push_back(pic);
}
multipleTemplateMatch(screenImage, bossList);
Template Matching
std::vector<XYposition> multipleTemplateMatch(cv::Mat &img, std::vector<cv::Mat> tplList){
std::vector<XYposition> matches;
cv::Mat convertImg(img.rows, img.cols, CV_8UC3);
cv::cvtColor(img, convertImg, CV_BGRA2GRAY);
double threshold = 0.8;
int imgint = convertImg.type();
for(cv::Mat tpl : tplList){
int tplint = tpl.type();
cv::Mat result(convertImg.rows - tpl.rows + 1, convertImg.cols - tpl.cols + 1,
CV_32FC1); //must be this result type
cv::matchTemplate(convertImg, tpl, result, CV_TM_CCOEFF_NORMED);
cv::threshold(result, result, threshold, 1., CV_THRESH_TOZERO);
while (true)
{
double minval, maxval;
cv::Point minloc, maxloc;
cv::minMaxLoc(result, &minval, &maxval, &minloc, &maxloc);
if (maxval >= threshold)
{
rectangle(result, maxloc, cv::Point(maxloc.x - tpl.cols, maxloc.y - tpl.rows),
cv::Scalar(0, 0, 255), 4, 8, 0);
cv::floodFill(result, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
XYposition info = {
maxloc.x - ceil(tpl.cols / 2), maxloc.y - ceil(tpl.rows / 2)
};
matches.push_back(info);
}
else
break;
}
}
return matches;
}
I didn't debug your code, but since it doesn't work (probably floodfill is messing up your result matrix), this is a simple working sample.
I iterate over the maximum points in the result matrix finding the blobs where values are over a threshold, and finding the highest value within each blob (used as a mask to retrieve actual values in the result matrix).
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
Mat3b img = imread("path_to_image");
Mat3b templ = imread("path_to_template");
Mat1b img_gray;
Mat1b templ_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY);
cvtColor(templ, templ_gray, COLOR_BGR2GRAY);
Mat1f result;
matchTemplate(img, templ, result, TM_CCOEFF_NORMED);
double thresh = 0.7;
threshold(result, result, thresh, 1., THRESH_BINARY);
Mat1b resb;
result.convertTo(resb, CV_8U, 255);
vector<vector<Point>> contours;
findContours(resb, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (int i=0; i<contours.size(); ++i)
{
Mat1b mask(result.rows, result.cols, uchar(0));
drawContours(mask, contours, i, Scalar(255), CV_FILLED);
Point max_point;
double max_val;
minMaxLoc(result, NULL, &max_val, NULL, &max_point, mask);
rectangle(img, Rect(max_point.x, max_point.y, templ.cols, templ.rows), Scalar(0,255,0), 2);
}
return 0;
}
Result:

Compute correlation of two Mat files in OpenCV

I have got a vector of Mat files and I want to calculate the correlation between them so as to keep the two mat files with which are theoretical similar. Actually in this vector are stored detected eyes from images, so I am trying to delete outliers. How is it possible to calculate correlation between two Mat files???
EDIT:
Mat Detection::hist_calculation(Mat image){
// Establish the number of bins
int histSize = 256;
// Set the ranges
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat hist;
// Compute the histograms:
calcHist( &image, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
normalize(hist, hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(hist.at<float>(i)) ) ,
Scalar( 255, 0, 0), 2, 8, 0 );
}
//// Display
//namedWindow("calcHist Demo", CV_WINDOW_AUTOSIZE );
//imshow("calcHist Demo", histImage );
//waitKey(0);
return hist;
}
double Detection::cvMatHistCorrelation(Mat file1, Mat file2) {
cvtColor(file1, file1, CV_BGR2GRAY); cvtColor(file2, file2, CV_BGR2GRAY);
Mat hist1 = hist_calculation(file1);
Mat hist2 = hist_calculation(file2);
double autoCorrelation1 = compareHist( hist1, hist1, CV_COMP_BHATTACHARYYA );
double autoCorrelation2 = compareHist( hist1, hist1, CV_COMP_BHATTACHARYYA );
double correlation = compareHist( hist1, hist2, CV_COMP_BHATTACHARYYA );
cout << "autocorrelation of his1: "<< autoCorrelation1 << endl;
cout << "autocorrelation of hist2: "<< autoCorrelation2 << endl;
cout << "correlation between hist1 and hist2: "<< autoCorrelation << endl;
return correlation;
}
I think it works fine.
It's better to compute the correlation of feature vectors of these two Mat files instead of on the Mat data directly.
For example, you can first compute RGB/HSV color histogram (24d vector if use 8-bins for each channel) for each Mat file and then compute correlation of these two histogram vectors.

Histogram for green component of an image in c++ using OpenCV

I want to create a histogram for an green component of an image in c++ using OpenCV.
The following code is working fine for color image but once i split the image into its RGB component and using the green component to call calcHist function, I am getting the following error.
OpenCV Error: Assertion failed (j < nimages) in histPrepareImages, file /root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp, line 148
terminate called after throwing an instance of 'cv::Exception'
what(): /root/src/OpenCV-2.4.1/modules/imgproc/src/histogram.cpp:148: error: (-215) j < nimages in function histPrepareImages
Aborted (core dumped)
Here is my code for the same.
I took two images to create the histogram. Anyone pls help so solve this problem.
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main( int argc, char** argv )
{
Mat src,src1, hsv, hsv1;
if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
return -1;
std::vector<cv::Mat> three_channels;
cv::split(src,three_channels);
std::vector<cv::Mat> three_channels1;
cv::split(src1,three_channels1);
//cvtColor(src, hsv, CV_BGR2HSV);
//cvtColor(src1, hsv1, CV_BGR2HSV);
// Quantize the hue to 30 levels
// and the saturation to 32 levels
int hbins = 30, sbins = 32;
int histSize[] = {hbins, sbins};
// hue varies from 0 to 179, see cvtColor
float hranges[] = { 0, 180 };
// saturation varies from 0 (black-gray-white) to
// 255 (pure spectrum color)
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist, hist1, difference;
// we compute the histogram from the 0-th and 1-st channels
int channels[] = {0, 1};
calcHist( &three_channels[1], 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
calcHist( &three_channels1[1], 1, channels, Mat(), // do not use mask
hist1, 2, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
minMaxLoc(hist1, 0, &maxVal, 0, 0);
int scale = 10;
Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
Mat hist1Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
Mat hist2Img = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
double hist_diff =0;
hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);
absdiff(hist, hist1, difference);
printf("\nHist Diff: %f\n", hist_diff);
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = hist.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = hist1.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist1Img, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
{
float binVal = difference.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist2Img, Point(h*scale, s*scale),
Point( (h+1)*scale - 1, (s+1)*scale - 1),
Scalar::all(intensity),
CV_FILLED );
}
namedWindow( "Source", 1 );
imshow( "Source", src );
namedWindow( "H-S Histogram", 1 );
imshow( "H-S Histogram", histImg );
namedWindow( "H-S Histogram1", 1 );
imshow( "H-S Histogram1", hist1Img );
namedWindow( "H-S Histogram2", 1 );
imshow( "H-S Histogram2", hist2Img );
waitKey();
}
You're trying to calculate the histogram of two channels (0 and 1) from an image that has only one channel, as you splitted it.
I did not look at your code in detail, but I guess you could omit the splitting and pass src/src1 to calcHist instead of three_channels[1]/three_channels1[1], setting channels = {1}
EDIT
In your code, change channels = {0,1} to channels{0}, you should not get any errors.
You're passing a single-channel image to calcHist(), that's why you should only use channel 0 (the only one). By passing three_channels[1] as input image, you're making sure that you're actually analysing the second channel of your input image.
OR do the following:
int channels[] = {1};
calcHist( &src, 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
You don't need to cv::split(src,three_channels) anymore.
That's two versions that compile, but you actually want to compute a green (1D) histogram and not a 2D histogram. So here's your edited code, that (hopefully) does, what you want:
int main( int argc, char** argv )
{
Mat src,src1;
if( argc != 3 || !(src=imread(argv[1], 1)).data || !(src=imread(argv[2], 1)).data)
return -1;
// Quantize the green to 30 levels
int greenbins = 30;
int histSize[] = {greenbins};
// green varies from 0 to 255 (pure spectrum color)
float greenranges[] = { 0, 256 };
const float* ranges[] = { greenranges };
MatND hist, hist1, difference;
// we compute the histogram from the 2nd channel (green, index is 1)
int channels[] = {1};
calcHist( &src, 1, channels, Mat(), // do not use mask
hist, 1, histSize, ranges,
true, // the histogram is uniform
false );
calcHist( &src1, 1, channels, Mat(), // do not use mask
hist1, 1, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal1=0;
double maxVal2 =0;
minMaxLoc(hist, 0, &maxVal1, 0, 0);
minMaxLoc(hist1, 0, &maxVal2, 0, 0);
double maxVal = max(maxVal1, maxVal2);
int scale = 10;
int width = 50;
Mat histImg = Mat::zeros(greenbins*scale, width, CV_8UC3);
Mat hist1Img = Mat::zeros(greenbins*scale, width, CV_8UC3);
Mat hist2Img = Mat::zeros(greenbins*scale, width, CV_8UC3);
double hist_diff =0;
hist_diff = compareHist(hist, hist1, CV_COMP_CORREL);
absdiff(hist, hist1, difference);
printf("\nHist Diff: %f\n", hist_diff);
for( int h = 0; h<greenbins; ++h)
{
float binVal = hist.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}
for( int h = 0; h<greenbins; ++h)
{
float binVal = hist1.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist1Img, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}
for(int h = 0; h < greenbins; ++h)
{
float binVal = difference.at<float>(h);
int intensity = cvRound(binVal*255/maxVal);
rectangle( hist2Img, Point(0, h*scale),
Point(width, (h+1)*scale),
Scalar::all(intensity),
CV_FILLED );
}
imshow( "Source", src );
imshow( "Source1", src1 );
imshow( "src1 green Histogram", histImg );
imshow( "src2 green Histogram", hist1Img );
imshow( "diff green Histogram", hist2Img );
waitKey();
}