Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Histogram targetid #53

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
8 changes: 4 additions & 4 deletions modules/targetid/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include_directories(include ../core/include)
include_directories(include ../core/include ../imgimport/include)
ADD_DEFINITIONS("-DBOOST_LOG_DYN_LINK")
add_library(TargetIdentification src/target_identifier.cpp src/object_detector.cpp src/canny_contour_creator.cpp src/k_means_filter.cpp src/filter.cpp src/qr_identifier.cpp src/contour_comparison.cpp)
target_link_libraries(TargetIdentification ${OpenCV_LIBS} ${Boost_LIBRARIES} ${ZBAR_LIBRARIES} zbar Core)
message("ZBAR: " + ${ZBAR_LIBRARIES})
add_library(TargetIdentification src/target_identifier.cpp src/object_detector.cpp src/canny_contour_creator.cpp src/k_means_filter.cpp src/filter.cpp src/qr_identifier.cpp src/histogram.cpp src/contour_comparison.cpp)
target_link_libraries(TargetIdentification ${OpenCV_LIBS} ${Boost_LIBRARIES} ${ZBAR_LIBRARIES} zbar Core ImageImport)
target_compile_features(TargetIdentification PRIVATE cxx_range_for)
add_subdirectory("test")
64 changes: 64 additions & 0 deletions modules/targetid/include/histogram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
This file is part of WARG's computer-vision

Copyright (c) 2015, Waterloo Aerial Robotics Group (WARG)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Usage of this code MUST be explicitly referenced to WARG and this code
cannot be used in any competition against WARG.
4. Neither the name of the WARG nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.

THIS SOFTWARE IS PROVIDED BY WARG ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARG BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef HISTOGRAM_H_INCLUDED
#define HISTOGRAM_H_INCLUDED
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "frame.h"
#include "pictureimport.h"
#include <iostream>
#include <stdio.h>
#include <vector>
#include "filter.h"
#include <cmath>

class target_range{
public:
void input_hue(int a);
bool belong(cv::Vec3b c);
private:
std::vector<int> hue;
};

class hist_filter : public Filter{
public:
hist_filter();
~hist_filter();
cv::Mat * filter(const cv::Mat & src);
private:
int count=0;
double avg_hue[18],hue_multi[18];
// double avg_sat[16],sat_multi[16];
int avg_brightness=0;
};
#endif // HISTOGRAM_H_INCLUDED
97 changes: 97 additions & 0 deletions modules/targetid/src/histogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include "histogram.h"
using namespace std;
using namespace cv;

void target_range::input_hue(int a){
hue.push_back(a);
}

bool target_range::belong(Vec3b c){
for(int i=0;i<hue.size();i++){
if(hue.at(i)==c.val[0]){
return true;
}
}
return false;
}

hist_filter::hist_filter()
:Filter(){
}

hist_filter::~hist_filter(){
}

Mat* hist_filter::filter(const Mat & src){
Mat bgr_img=src.clone();
int brightness;
for(int y=0;y<bgr_img.rows;y++){
for(int x=0;x<bgr_img.cols;x++){
Vec3b colour = bgr_img.at<Vec3b>(Point(x, y));
brightness+=colour.val[0];
brightness+=colour.val[1];
brightness+=colour.val[2];
}
}
brightness/=(bgr_img.rows*bgr_img.cols*3);
avg_brightness=(avg_brightness*count+brightness)/(count+1);
for(int y=0;y<bgr_img.rows;y++){
for(int x=0;x<bgr_img.cols;x++){
bgr_img.at<Vec3b>(Point(x, y)).val[0]=bgr_img.at<Vec3b>(Point(x, y)).val[0]+avg_brightness-brightness;
if(bgr_img.at<Vec3b>(Point(x, y)).val[0]>255) bgr_img.at<Vec3b>(Point(x, y)).val[0]=255;
if(bgr_img.at<Vec3b>(Point(x, y)).val[0]<0) bgr_img.at<Vec3b>(Point(x, y)).val[0]=0;
bgr_img.at<Vec3b>(Point(x, y)).val[1]=bgr_img.at<Vec3b>(Point(x, y)).val[1]+avg_brightness-brightness;
if(bgr_img.at<Vec3b>(Point(x, y)).val[1]>255) bgr_img.at<Vec3b>(Point(x, y)).val[1]=255;
if(bgr_img.at<Vec3b>(Point(x, y)).val[1]<0) bgr_img.at<Vec3b>(Point(x, y)).val[1]=0;
bgr_img.at<Vec3b>(Point(x, y)).val[2]=bgr_img.at<Vec3b>(Point(x, y)).val[2]+avg_brightness-brightness;
if(bgr_img.at<Vec3b>(Point(x, y)).val[2]>255) bgr_img.at<Vec3b>(Point(x, y)).val[2]=255;
if(bgr_img.at<Vec3b>(Point(x, y)).val[2]<0) bgr_img.at<Vec3b>(Point(x, y)).val[2]=0;
}
}
Mat* hsv_img=new Mat;
cvtColor(bgr_img,*hsv_img,CV_BGR2HSV);
blur(*hsv_img,*hsv_img,Size(3,3));
int hueSize = 18;
int satSize=16;
vector<Mat> bgr_planes;
split(*hsv_img,bgr_planes);
float hue_range[]={0,180};
const float* hueRange={hue_range};
// float sat_range[]={0,256};
// const float* satRange={sat_range};
bool uniform = true; bool accumulate = false;
Mat hue_hist;
// Mat sat_hist;
calcHist( &bgr_planes[0], 1, 0, Mat(), hue_hist, 1, &hueSize, &hueRange, uniform, accumulate );
// calcHist( &bgr_planes[1], 1, 0, Mat(), sat_hist, 1, &satSize, &satRange, uniform, accumulate );
for( int i = 0; i < hueSize; i++ ){
avg_hue[i]=(avg_hue[i]*count+cvRound(hue_hist.at<float>(i)))/(count+1);
}
/* for( int i = 0; i < satSize; i++ ){
avg_sat[i]=(avg_sat[i]*count+cvRound(sat_hist.at<float>(i)))/(count+1);
}*/
count++;
int tmp;
for( int i = 0; i < hueSize; i++ ){
tmp=cvRound(hue_hist.at<float>(i)-avg_hue[i]);
if(tmp<0) tmp=0;
hue_multi[i]=pow(tmp/hue_hist.at<float>(i),2);
}
/* for( int i = 0; i < satSize; i++ ){
tmp=cvRound(sat_hist.at<float>(i)-avg_sat[i]);
if(tmp<0) tmp=0;
sat_multi[i]=pow(tmp/sat_hist.at<float>(i),3);
}*/
Mat* result=new Mat;
cvtColor(*hsv_img,*result,CV_HSV2BGR);
for(int y=0;y<hsv_img->rows;y++){
for(int x=0;x<hsv_img->cols;x++){
Vec3b colour = hsv_img->at<Vec3b>(Point(x, y));
result->at<Vec3b>(Point(x, y)).val[0]=result->at<Vec3b>(Point(x, y)).val[0]*hue_multi[colour.val[0]/10]*colour.val[1]/256;
result->at<Vec3b>(Point(x, y)).val[1]=result->at<Vec3b>(Point(x, y)).val[1]*hue_multi[colour.val[0]/10]*colour.val[1]/256;
result->at<Vec3b>(Point(x, y)).val[2]=result->at<Vec3b>(Point(x, y)).val[2]*hue_multi[colour.val[0]/10]*colour.val[1]/256;
}
}
return result;
}

3 changes: 3 additions & 0 deletions modules/targetid/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ if(Boost_FOUND AND OpenCV_FOUND)
add_definitions("-DBOOST_LOG_DYN_LINK")
add_executable(qr_test qr_test.cpp)
add_executable(targetid_test_contour test.cpp)
add_executable(hist_test hist_test.cpp)
target_link_libraries(targetid_test_contour ${OpenCV_LIBS} ${Boost_LIBRARIES} Core TargetIdentification)
target_link_libraries(qr_test ${OpenCV_LIBS} ${Boost_LIBRARIES} ${ZBAR_LIBRARIES} Core TargetIdentification)
target_link_libraries(hist_test ${OpenCV_LIBS} ${Boost_LIBRARIES} Core TargetIdentification ImageImport)
add_executable(targetid_test_interactive interactive.cpp)
target_link_libraries(targetid_test_interactive ${OpenCV_LIBS} ${Boost_LIBRARIES} Core TargetIdentification)

Expand All @@ -14,4 +16,5 @@ if(Boost_FOUND AND OpenCV_FOUND)
# Tests
add_test("SimpleTarget" targetid_test_contour ${TESTDATA_DIR}/photos/IMG_1644.jpg --log_format=XML --log_sink=TEST-boost.xml --log_level=all --report_level=no)
add_test("qr_test" qr_test ${TESTDATA_DIR} --log_format=XML --log_sink=TEST-QR.xml --log_level=all --report_level=no)
add_test("hist_test" hist_test ${TESTDATA_DIR}/photos ${TESTDATA_DIR}/pictureimport_test_csv.csv --log_format=XML --log_sink=TEST-HIST.xml --log_level=all --report_level=no)
endif()
63 changes: 63 additions & 0 deletions modules/targetid/test/hist_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE TargetIdentification
#include "histogram.h"
#include <boost/test/unit_test.hpp>
#include <boost/log/trivial.hpp>
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "target_identifier.h"
#include "canny_contour_creator.h"
#include "k_means_filter.h"
#include "frame.h"
#include "benchmark.h"
#include "contour_comparison.h"
using namespace std;
using namespace cv;
using namespace boost;

BOOST_AUTO_TEST_CASE(hist_test){
vector<int> n;
n.push_back(3);
string telemetry_path=boost::unit_test::framework::master_test_suite().argv[2];
string filePath=boost::unit_test::framework::master_test_suite().argv[1];
ofstream fout(telemetry_path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of things I don't like about outputting this csv.
First of all It's not actually necessary for a test that's got nothing to do with the metadata.
But also you're putting it in a fixed-path source directory (and then of course the generated file gets included in the commit), if anything it should go with the generated build files. I would point out however that we do have the actual telemetry logs for the photos in the testdata directory. It would make more sense to include them and use them instead of generating placeholder files.

fout<<"time,timeError,lat,lon,latError,lonError,pitch,roll,pitchRate,rollRate,yawRate,altitude,heading,altError,headingError,photonum"<<endl;
for(int i=0;i<25;i++)
{
for(int j=0;j<15;j++){
fout<<j<<",";
}
fout<<i<<endl;
}
hist_filter test_filter;
PictureImport* input=new PictureImport(telemetry_path,filePath,n);
Frame* src;
for(src=input->next_frame();src!=NULL;src=input->next_frame()){
const Mat buffer=src->get_img();
test_filter.filter(buffer);
}
delete input;
PictureImport* in=new PictureImport(telemetry_path,filePath,n);
Mat* show;
for(src=in->next_frame();src!=NULL;src=in->next_frame()){
const Mat buffer=src->get_img();
show=test_filter.filter(buffer);
KMeansFilter filter;
CannyContourCreator ccreator;
Mat * filtered = filter.filter(buffer);
vector<vector<Point> > * results = ccreator.get_contours(*filtered);
vector<vector<Point> > * expected = ccreator.get_contours(*show);
double diff = compare_contours(*results, *expected);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not terribly useful to be comparing the results to the k_means filter results since they are largely unknown. You want to be comparing them to a contour that we know to be more or less correct, i.e. one determined manually.

I also think that we should allow the threshold for the contour creator to be set by its constructor (or set up a way of doing on the fly adjustments such as what I added to the filter class). From the results I was seeing on Thursday this filter will probably benefit from a lower threshold than the kmeans segmentation filter.

BOOST_CHECK(diff > 0.01);
BOOST_TEST_MESSAGE("RESULT: " << diff);
/* namedWindow("original",WINDOW_NORMAL);
resizeWindow("original", 900, 900);
imshow("original",buffer);
namedWindow("display",WINDOW_NORMAL);
resizeWindow("display", 900, 900);
imshow("display",*show);
waitKey(0);*/
}
}
Binary file added testdata/hist_testdata/image1 (10th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (11th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (12th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (13th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (14th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (15th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (16th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (17th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (18th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (3rd copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (4th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (5th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (6th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (7th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (8th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (9th copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (another copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1 (copy).JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image1.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/hist_testdata/image2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions testdata/pictureimport_test_csv.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time,timeError,lat,lon,latError,lonError,pitch,roll,pitchRate,rollRate,yawRate,altitude,heading,altError,headingError,photonum
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,0
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,1