Created
January 3, 2014 06:52
-
-
Save usagi/8233920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 2.8.10) | |
project(cv-osoi) | |
find_package(OpenCV REQUIRED) | |
if(CMAKE_SYSTEM_NAME STREQUAL "Linux") | |
find_library(OpenCL_LIBS OpenCL) | |
if(NOT OpenCL_LIBS) | |
message(FATAL "libOpenCL.so is not found") | |
else() | |
message(STATUS "OpenCL_LIBS: ${OpenCL_LIBS}") | |
endif() | |
endif() | |
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") | |
add_executable(cv-osoi cv-osoi.cxx) | |
target_link_libraries(cv-osoi | |
${OpenCV_LIBS} | |
${OpenCL_LIBS} | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdexcept> | |
#include <vector> | |
#include <forward_list> | |
#include <list> | |
#include <chrono> | |
#include <iostream> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
inline const std::chrono::microseconds time(const std::function<void()>& f) | |
{ | |
const auto begin = std::chrono::high_resolution_clock::now(); | |
f(); | |
return std::chrono::duration_cast<std::chrono::microseconds> | |
(std::chrono::high_resolution_clock::now() - begin); | |
} | |
int main(const int an, const char* const* const as) | |
{ | |
if(an < 2) | |
throw std::runtime_error("invlid arguments."); | |
auto video_filename = as[1]; | |
auto video = cv::VideoCapture(video_filename); | |
if(!video.isOpened()) | |
throw std::runtime_error("cannot open video file."); | |
const size_t frame_count = video.get(CV_CAP_PROP_FRAME_COUNT); | |
std::vector<cv::Mat> frames(frame_count); | |
std::cout | |
<< "time_cv_iterator" << " " | |
<< "time_native_pointer" << " " | |
<< "time_std_list" << " " | |
<< "time_std_forward_list" << " " | |
<< "time_std_vector" << " " | |
<< "\n" | |
; | |
for(auto& frame : frames) | |
{ | |
cv::Mat source; | |
video >> source; | |
source.copyTo(frame); | |
} | |
for(auto& frame : frames) | |
{ | |
auto time_cv_iterator = time([&]() | |
{ | |
auto i = frame.begin<cv::Point3_<uint8_t>>(); | |
const auto e = frame.end<cv::Point3_<uint8_t>>(); | |
while(i != e) | |
{ | |
(*i).x = 255; | |
++i; | |
} | |
}); | |
auto time_native_pointer = time([&]() | |
{ | |
auto p = reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data); | |
const auto e = p + frame.cols * frame.rows; | |
while(p < e) | |
{ | |
p->x = 255; | |
++p; | |
} | |
}); | |
auto list = std::list<cv::Point3_<uint8_t>>(reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data), reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data)+frame.cols * frame.rows); | |
auto time_std_list = time([&]() | |
{ | |
/* | |
for(auto& a : list) | |
a.x = 255; | |
*/ | |
auto i = list.begin(); | |
const auto e = list.end(); | |
while(i != e) | |
{ | |
i->x = 255; | |
++i; | |
} | |
}); | |
auto forward_list = std::forward_list<cv::Point3_<uint8_t>>(reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data), reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data)+frame.cols * frame.rows); | |
auto time_std_forward_list = time([&]() | |
{ | |
/* | |
for(auto& a : forward_list) | |
a.x = 255; | |
*/ | |
auto i = forward_list.begin(); | |
const auto e = forward_list.end(); | |
while(i != e) | |
{ | |
i->x = 255; | |
++i; | |
} | |
}); | |
auto vector = std::vector<cv::Point3_<uint8_t>>(reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data), reinterpret_cast<cv::Point3_<uint8_t>*>(frame.data)+frame.cols * frame.rows); | |
auto time_std_vector = time([&]() | |
{ | |
/* | |
for(auto& a : vector) | |
a.x = 255; | |
*/ | |
auto i = vector.begin(); | |
const auto e = vector.end(); | |
while(i != e) | |
{ | |
i->x = 255; | |
++i; | |
} | |
}); | |
std::cout | |
<< time_cv_iterator.count() << " " | |
<< time_native_pointer.count() << " " | |
<< time_std_list.count() << " " | |
<< time_std_forward_list.count() << " " | |
<< time_std_vector.count() << " " | |
<< "\n" | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment