Created
November 3, 2017 02:13
-
-
Save AyumiKasagi/f8d421a56e5d428f9ebe353c87f40882 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
#include <boost/interprocess/managed_shared_memory.hpp> | |
#include <boost/interprocess/sync/interprocess_mutex.hpp> | |
#include <boost/interprocess/containers/vector.hpp> | |
#include <boost/interprocess/allocators/allocator.hpp> | |
#include <boost/interprocess/sync/scoped_lock.hpp> | |
#include <unistd.h> | |
#include <opencv2/opencv.hpp> | |
using namespace boost::interprocess; | |
typedef allocator<unsigned char, managed_shared_memory::segment_manager> shmalloc; | |
typedef vector<unsigned char, shmalloc> shmimage; | |
int main( int argc, char* argv[] ) | |
{ | |
managed_shared_memory shm(open_only, "NAME"); | |
shmimage *image = shm.find< shmimage >("Image").first; | |
cv::Mat_<cv::Vec3b> im(480, 640); | |
shmimage& imref = *image; | |
memcpy(im.data, &imref[0], 640*480*3); | |
cv::imwrite("received.jpg", im); | |
usleep(1000*10); | |
} |
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 <boost/interprocess/managed_shared_memory.hpp> | |
#include <boost/interprocess/sync/interprocess_mutex.hpp> | |
#include <boost/interprocess/containers/vector.hpp> | |
#include <boost/interprocess/allocators/allocator.hpp> | |
#include <boost/interprocess/sync/scoped_lock.hpp> | |
#include <unistd.h> | |
#include <opencv2/opencv.hpp> | |
using namespace boost::interprocess; | |
typedef allocator < unsigned char, managed_shared_memory::segment_manager> shmalloc; | |
typedef vector< unsigned char, shmalloc > shmimage; | |
int main( int argc, char* argv[] ) | |
{ | |
shared_memory_object::remove("NAME"); | |
managed_shared_memory shm(open_or_create, "NAME", 640*480*3+1024); | |
const shmalloc alloc_inst (shm.get_segment_manager()); | |
shmimage * image = shm.construct < shmimage >("Image")(alloc_inst); | |
shmimage & imref = * image; | |
imref.resize(640*480*3); | |
cv::Mat_<cv::Vec3b> im = cv::imread("testsend.jpg"); | |
memcpy(&imref[0], im.data, 640*480*3); | |
usleep(1000*10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment