Skip to content

Commit

Permalink
Bug fix.
Browse files Browse the repository at this point in the history
Chapter 04:
- The `getHueHistogram()` function should contains 2 parameters.
The original one is simply the copy of getHistogram();
- `Finder.cpp` contains redundant code, which are useless and can be
eliminated.

Chapter 09:
- The `remap()` function doesn't need the scope specifier in the header file.
  • Loading branch information
Joseph Pan committed Feb 20, 2014
1 parent 23fcf0b commit c5a0582
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .directory
@@ -0,0 +1,4 @@
[Dolphin]
PreviewsShown=true
Timestamp=2014,2,20,11,28,21
Version=3
31 changes: 22 additions & 9 deletions Chapter 04/colorhistogram.h
Expand Up @@ -111,26 +111,39 @@ class ColorHistogram {
return hist;
}

// Computes the 1D Hue histogram with a mask.
// Computes the 1D Hue histogram with a mask.
// BGR source image is converted to HSV
cv::MatND getHueHistogram(const cv::Mat &image) {
cv::MatND getHueHistogram(const cv::Mat &image,
int minSaturation = 0) {

cv::MatND hist;

// Convert to Lab color space
cv::Mat hue;
cv::cvtColor(image, hue, CV_BGR2HSV);
// Convert to HSV color space
cv::Mat hsv;
cv::cvtColor(image, hsv, CV_BGR2HSV);

// Mask to be used (or not)
cv::Mat mask;

if (minSaturation > 0) {
// Spliting the 3 channels into 3 images
std::vector<cv::Mat> v;
cv::split(hsv, v);
// Mask out the low saturated pixels
cv::threshold(v[1], mask, minSaturation,
255, cv::THRESH_BINARY);
}

// Prepare arguments for a 1D hue histogram
hranges[0]= 0.0;
hranges[1]= 180.0;
channels[0]= 0; // the hue channel
channels[0]= 0; // the hue channel

// Compute histogram
cv::calcHist(&hue,
// Compute histogram
cv::calcHist(&hsv,
1, // histogram of 1 image only
channels, // the channel used
cv::Mat(), // no mask is used
mask, // no mask is used
hist, // the resulting histogram
1, // it is a 1D histogram
histSize, // number of bins
Expand Down
49 changes: 10 additions & 39 deletions Chapter 04/finder.cpp
Expand Up @@ -36,78 +36,50 @@ int main()

// Define ROI
cv::Mat imageROI= image(cv::Rect(110,260,35,40));
cv::rectangle(image, cv::Rect(110,260,35,40),cv::Scalar(0,0,255));
cv::rectangle(image, cv::Rect(110,260,35,40), cv::Scalar(0,0,255));

// Display image
cv::namedWindow("Image");
cv::imshow("Image",image);
cv::namedWindow("Image 1");
cv::imshow("Image 1",image);

// Get the Hue histogram
int minSat=65;
ColorHistogram hc;
cv::MatND colorhist= hc.getHueHistogram(imageROI,minSat);
cv::MatND colorhist= hc.getHueHistogram(imageROI, minSat);

ObjectFinder finder;
finder.setHistogram(colorhist);
finder.setThreshold(0.2f);

// Convert to HSV space
cv::Mat hsv;
cv::cvtColor(image, hsv, CV_BGR2HSV);

// Split the image
vector<cv::Mat> v;
cv::split(hsv,v);

// Eliminate pixels with low saturation
cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
cv::namedWindow("Saturation");
cv::imshow("Saturation",v[1]);

// Get back-projection of hue histogram
int ch[1]={0};
cv::Mat result= finder.find(hsv,0.0f,180.0f,ch,1);

cv::namedWindow("Result Hue");
cv::imshow("Result Hue",result);

cv::bitwise_and(result,v[1],result);
cv::namedWindow("Result Hue and");
cv::imshow("Result Hue and",result);

// Second image
image= cv::imread("../baboon3.jpg");

// Display image
// Display image
cv::namedWindow("Image 2");
cv::imshow("Image 2",image);

// Convert to HSV space
cv::Mat hsv;
cv::cvtColor(image, hsv, CV_BGR2HSV);

// Split the image
vector<cv::Mat> v;
cv::split(hsv,v);

// Eliminate pixels with low saturation
cv::threshold(v[1],v[1],minSat,255,cv::THRESH_BINARY);
cv::namedWindow("Saturation");
cv::imshow("Saturation",v[1]);

// Get back-projection of hue histogram
result= finder.find(hsv,0.0f,180.0f,ch,1);
// Get back-projection of hue histogram
int ch[1]={0};
cv::Mat result= finder.find(hsv,0.0f,180.0f,ch,1);

cv::namedWindow("Result Hue");
cv::imshow("Result Hue",result);

// Eliminate low stauration pixels
cv::bitwise_and(result,v[1],result);
cv::namedWindow("Result Hue and");
cv::imshow("Result Hue and",result);

// Get back-projection of hue histogram
finder.setThreshold(-1.0f);
result= finder.find(hsv,0.0f,180.0f,ch,1);
cv::bitwise_and(result,v[1],result);
cv::namedWindow("Result Hue and raw");
cv::imshow("Result Hue and raw",result);

Expand All @@ -125,4 +97,3 @@ int main()

cv::waitKey();
return 0;
}
2 changes: 1 addition & 1 deletion Chapter 09/CameraCalibrator.h
Expand Up @@ -52,7 +52,7 @@ class CameraCalibrator {
// Set the calibration flag
void setCalibrationFlag(bool radial8CoeffEnabled=false, bool tangentialParamEnabled=false);
// Remove distortion in an image (after calibration)
cv::Mat CameraCalibrator::remap(const cv::Mat &image);
cv::Mat remap(const cv::Mat &image);

// Getters
cv::Mat getCameraMatrix() { return cameraMatrix; }
Expand Down

0 comments on commit c5a0582

Please sign in to comment.