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
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")
55 changes: 55 additions & 0 deletions modules/targetid/include/histogram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
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 HistFilter : public Filter{
public:
HistFilter();
~HistFilter();
cv::Mat * filter(const cv::Mat & src);
private:
int count=0;
double avg_hue[18],hue_multi[18];
int avg_brightness=0;
};
#endif // HISTOGRAM_H_INCLUDED
72 changes: 72 additions & 0 deletions modules/targetid/src/histogram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "histogram.h"
using namespace std;
using namespace cv;

HistFilter::HistFilter()
:Filter(){
}

HistFilter::~HistFilter(){
}

Mat* HistFilter::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};
bool uniform = true; bool accumulate = false;
Mat hue_hist;
calcHist( &bgr_planes[0], 1, 0, Mat(), hue_hist, 1, &hueSize, &hueRange, uniform, accumulate );
for( int i = 0; i < hueSize; i++ ){
avg_hue[i]=(avg_hue[i]*count+cvRound(hue_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);
}
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()
145 changes: 145 additions & 0 deletions modules/targetid/test/hist_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#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"
#include <dirent.h>
using namespace std;
using namespace cv;
using namespace boost;

BOOST_AUTO_TEST_CASE(hist_test){
string filePath=boost::unit_test::framework::master_test_suite().argv[1];
HistFilter test_filter;
DIR* dr;
dr=opendir(filePath.c_str());
struct dirent* drnt;
for(drnt=readdir(dr);drnt!=NULL;drnt=readdir(dr)){
Mat src;
string true_path=filePath+'/'+drnt->d_name;
src=imread(true_path,CV_LOAD_IMAGE_COLOR);
while(src.empty()){
drnt=readdir(dr);
if(drnt==NULL){
break;
}
if(strcmp(drnt->d_name,"..")==0||strcmp(drnt->d_name,".")==0){
continue;
}
string true_path=filePath+'/'+drnt->d_name;
src=imread(true_path,CV_LOAD_IMAGE_COLOR);
}
if(drnt==NULL){
break;
}
const Mat buffer=src;
test_filter.filter(buffer);
}
Mat* show;
DIR* dir;
dir=opendir(filePath.c_str());
struct img_vertices{
vector<vector<Point>> vertices;
string name;
};
img_vertices expected_contours[24];
expected_contours[0].vertices.push_back({vector<Point>({Point(44,242), Point(59,250), Point(50,265), Point(37,256)})});
expected_contours[0].name="IMG_1547.jpg";
expected_contours[1].name="IMG_1552.jpg";
expected_contours[2].name="IMG_1557.jpg";
expected_contours[3].name="IMG_1559.jpg";
expected_contours[4].name="IMG_1577.jpg";
expected_contours[5].vertices.push_back({vector<Point>({Point(44,242), Point(59,250), Point(50,265), Point(37,256)})});
expected_contours[5].name="IMG_1579.jpg";
expected_contours[6].name="IMG_1581.jpg";
expected_contours[7].name="IMG_1583.jpg";
expected_contours[8].name="IMG_1595.jpg";
expected_contours[9].vertices.push_back({vector<Point>({Point(634,1316), Point(653,1350), Point(581,1388), Point(566,1347)})});
expected_contours[9].vertices.push_back({vector<Point>({Point(629,1334), Point(636,1346), Point(591,1369), Point(582,1355)})});
expected_contours[9].vertices.push_back({vector<Point>({Point(1274,957), Point(1283,965), Point(1266,980), Point(1257,970)})});
expected_contours[9].name="IMG_1598.jpg";
expected_contours[10].vertices.push_back({vector<Point>({Point(635,481), Point(686,572), Point(586,626), Point(536,533)})});
expected_contours[10].vertices.push_back({vector<Point>({Point(628,499), Point(670,568), Point(589,612), Point(552,538)})});
expected_contours[10].vertices.push_back({vector<Point>({Point(616,535), Point(623,540), Point(619,550), Point(631,558), Point(625,567),Point(614,562), Point(603,576), Point(595,570), Point(600,557), Point(591,550),Point(598,540), Point(607,543)})});
expected_contours[10].name="IMG_1616.jpg";
expected_contours[11].name="IMG_1616(1).jpg";
expected_contours[12].name="IMG_1631.jpg";
expected_contours[13].vertices.push_back({vector<Point>({Point(1781,1246), Point(1824,1294), Point(1780,1338), Point(1736,1288)})});
expected_contours[13].vertices.push_back({vector<Point>({Point(1781,1263), Point(1807,1294), Point(1782,1318), Point(1755,1288)})});
expected_contours[13].name="IMG_1632.jpg";
expected_contours[14].vertices.push_back({vector<Point>({Point(197,303), Point(246,337), Point(205,381), Point(156,343)})});
expected_contours[14].vertices.push_back({vector<Point>({Point(200,319), Point(226,340), Point(201,364), Point(175,343)})});
expected_contours[14].name="IMG_1635.jpg";
expected_contours[15].vertices.push_back({vector<Point>({Point(612,657), Point(637,659), Point(636,681), Point(610,677)})});
expected_contours[15].name="IMG_1642.jpg";
expected_contours[16].name="IMG_1644.jpg";
expected_contours[17].vertices.push_back({vector<Point>({Point(972,50), Point(1009,85), Point(934,143), Point(895,110)})});
expected_contours[17].vertices.push_back({vector<Point>({Point(969,67), Point(988,84), Point(935,126), Point(917,109)})});
expected_contours[17].vertices.push_back({vector<Point>({Point(1936,138), Point(1956,158), Point(1912,189), Point(1888,171)})});
expected_contours[17].vertices.push_back({vector<Point>({Point(1927,155), Point(1932,160), Point(1916,173), Point(1910,168)})});
expected_contours[17].vertices.push_back({vector<Point>({Point(1548,508), Point(1550,515), Point(1538,524), Point(1528,526),Point(1528,516),Point(1535,510)})});
expected_contours[17].name="IMG_1670.jpg";
expected_contours[18].name="IMG_1673.jpg";
expected_contours[19].name="IMG_1888.jpg";
expected_contours[20].vertices.push_back({vector<Point>({Point(1634,1112), Point(1655,1147), Point(1590,1197), Point(1565,1158)})});
expected_contours[20].vertices.push_back({vector<Point>({Point(1628,1130), Point(1634,1140), Point(1592,1174), Point(1584,1163)})});
expected_contours[20].name="IMG_1894.jpg";
expected_contours[21].vertices.push_back({vector<Point>({Point(1202,182), Point(1294,184), Point(1302,269), Point(1209,265)})});
expected_contours[21].vertices.push_back({vector<Point>({Point(1214,192), Point(1287,195), Point(1292,258), Point(1221,255)})});
expected_contours[21].vertices.push_back({vector<Point>({Point(1244,209), Point(1254,217), Point(1263,212), Point(1271,218), Point(1262,228),Point(1268,234), Point(1262,239), Point(1254,233), Point(1246,238), Point(1239,231),Point(1244,223), Point(1239,216)})});
expected_contours[21].name="IMG_1899.jpg";
expected_contours[22].vertices.push_back({vector<Point>({Point(1261,970), Point(1302,1025), Point(1251,1059), Point(1216,1006)})});
expected_contours[22].vertices.push_back({vector<Point>({Point(820,1084), Point(943,1418), Point(932,1428), Point(813,1191)})});
expected_contours[22].vertices.push_back({vector<Point>({Point(596,1329), Point(716,1499), Point(695,1499), Point(587,1337)})});
expected_contours[22].vertices.push_back({vector<Point>({Point(776,1333), Point(785,1343), Point(781,1353), Point(797,1361), Point(788,1374),Point(780,1367), Point(768,1383), Point(760,1376), Point(764,1362), Point(756,1354),Point(760,1348), Point(768,1351)})});
expected_contours[22].name="IMG_1908.jpg";
expected_contours[23].name="IMG_1913.jpg";
for(drnt=readdir(dir);drnt!=NULL;drnt=readdir(dir)){
Mat src;
string true_path=filePath+'/'+drnt->d_name;
src=imread(true_path,CV_LOAD_IMAGE_COLOR);
while(src.empty()){
drnt=readdir(dir);
if(drnt==NULL){
break;
}
if(strcmp(drnt->d_name,"..")==0||strcmp(drnt->d_name,".")==0){
continue;
}
string true_path=filePath+'/'+drnt->d_name;
src=imread(true_path,CV_LOAD_IMAGE_COLOR);
}
if(drnt==NULL){
break;
}
vector<vector<Point>> expected;
for(int i=0;i<24;i++){
if(expected_contours[i].name.compare(drnt->d_name)==0)
expected=expected_contours[i].vertices;
}
const Mat buffer=src;
show=test_filter.filter(buffer);
CannyContourCreator ccreator;
vector<vector<Point> > * results = ccreator.get_contours(*show);
BOOST_TEST_MESSAGE("Now testing " << drnt->d_name);
if(results->size()==0){
BOOST_CHECK(expected.size()==0);
BOOST_TEST_MESSAGE("No Target Found");
}
else{
double diff = compare_contours(*results, expected);
BOOST_CHECK(diff > 0.01);
BOOST_TEST_MESSAGE("RESULT: " << diff);
}
}
}