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)
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)
target_link_libraries(TargetIdentification ${OpenCV_LIBS} ${Boost_LIBRARIES} ${ZBAR_LIBRARIES} zbar Core ImageImport)
target_compile_features(TargetIdentification PRIVATE cxx_range_for)
add_subdirectory("test")
65 changes: 65 additions & 0 deletions modules/targetid/include/histogram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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"

class target_range{
public:
void input_b(int a);
void input_g(int a);
void input_r(int a);
bool belong(cv::Vec3b c);
private:
std::vector<int> blue;
std::vector<int> green;
std::vector<int> red;
};

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

void target_range::input_b(int a){
blue.push_back(a);
}

void target_range::input_g(int a){
green.push_back(a);
}

void target_range::input_r(int a){
red.push_back(a);
}

bool target_range::belong(Vec3b c){
for(int i=0;i<blue.size();i++){
if(blue.at(i)==c.val[0]){
for(int j=0;j<green.size();j++){
if(green.at(j)==c.val[1]){
for(int k=0;k<red.size();k++){
if(red.at(k)==c.val[2])
return true;
}
}
}
}
}
return false;
}

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

hist_filter::~hist_filter(){
}

Mat* hist_filter::filter(const Mat & src){
int histSize = 256;
vector<Mat> bgr_planes;
split(src,bgr_planes);
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat* b_hist=new Mat;
Mat* g_hist=new Mat;
Mat* r_hist=new Mat;
calcHist( &bgr_planes[0], 1, 0, Mat(), *b_hist, 1, &histSize, &histRange, uniform, accumulate );
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 );
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(*b_hist, *b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(*g_hist, *g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(*r_hist, *r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
for( int i = 0; i < histSize; i++ ){
avg_b[i]=(avg_b[i]*count+cvRound(b_hist->at<float>(i)))/(count+1);
avg_g[i]=(avg_g[i]*count+cvRound(g_hist->at<float>(i)))/(count+1);
avg_r[i]=(avg_r[i]*count+cvRound(r_hist->at<float>(i)))/(count+1);
}
count++;
int tmp;
int minimal=300;
target_range* pack=new target_range;
for( int i = 0; i < histSize; i++ ){
tmp=cvRound(b_hist->at<float>(i)-avg_b[i]);
if(tmp>minimal){
pack->input_b(i);
}
tmp=cvRound(g_hist->at<float>(i)-avg_g[i]);
if(tmp>minimal){
pack->input_g(i);
}
tmp=cvRound(r_hist->at<float>(i)-avg_r[i]);
if(tmp>minimal){
pack->input_r(i);
}
}
Mat* result=new Mat(src);
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't copy the image. You want the Mat::clone function. The Mat object itself is just a container around an internal data structure and the copy constructor just makes a shallow copy in this case, meaning that you're modifying the original image.

for(int y=0;y<result->rows;y++){
for(int x=0;x<result->cols;x++){
Vec3b colour = result->at<Vec3b>(Point(x, y));
if(!pack->belong(colour)){
result->at<Vec3b>(Point(x, y)).val[0]=0;
result->at<Vec3b>(Point(x, y)).val[1]=0;
result->at<Vec3b>(Point(x, y)).val[2]=0;
}
}
}
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()
46 changes: 46 additions & 0 deletions modules/targetid/test/hist_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#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>
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;
}
PictureImport* input=new PictureImport(telemetry_path,filePath,n);
hist_filter test_filter;
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);
namedWindow("display",WINDOW_NORMAL);
resizeWindow("display", 1000, 1000);
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