#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <image_transport/image_transport.h>
#include <ros/ros.h>
#include <vector>
#include <iostream>
using namespace std;
using namespace cv;
struct Subscribe_And_Publish{
ros::NodeHandle n;
ros::Subscriber getImage;
ros::Publisher pubLaneImage;
bool isRun;
Subscribe_And_Publish(bool isrun): isRun(isrun){
getImage = n.subscribe("/carla/ego_vehicle/camera/rgb/front/image_color", 10, &Subscribe_And_Publish::callback, this);
pubLaneImage = n.advertise<sensor_msgs::Image>("laneImage",10);
}
void callback(const sensor_msgs::Image img){
pubLaneImage.publish(img);
}
};
int main(int argc, char **argv){
cout << "Start laneDetect node!\\n";
ros::init(argc, argv, "laneDetect");
Subscribe_And_Publish laneDetect(1);
ros::spin();
}
cmake_minimum_required(VERSION 3.0.2)
project(withCarla)
find_package(catkin REQUIRED COMPONENTS # catkin 변수에 components 패키지들이 추가됨.
roscpp # 즉 catkin_INCLUDE_DIRS에 catkin 뿐만 아니라 components로 추가된 패키지 경로까지 한번에 저장됨 => 관리가 쉬워짐
rospy
cv_bridge
std_msgs
sensor_msgs
image_transport
)
find_package(OpenCV REQUIRED) # 단, opencv 같이 런타임에 실행되는 패키지는 components로 쓸 수 없음 => OpenCV_INCLUDE_DIRS에 경로 저장됨
catkin_package( # 패키지의 정보를 다른 패키지들에 export 하기 위한 용도로 사용
# INCLUDE_DIRS include : 생성된 헤더 파일들이 위치하는 디렉토리
# LIBRARIES $ {PROJECT_NAME} : 프로젝트에 해당되는 라이브러리들이 위치하는 디렉토리
# CATKIN_DEPENDS roscpp rospy cv_bridge std_msgs sensor_msgs image_transport : 프로젝트의 catkin 의존성 패키지들
# DEPENDS opencv : catkin에 포함되지 않은 의존성 패키지들
)
include_directories(
# include
${catkin_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
add_executable(laneDetect src/laneDetect.cpp)
target_link_libraries(laneDetect
${catkin_LIBRARIES}
${OpenCV_LIBRARIES}
)
<?xml version="1.0"?>
<package format="2">
<name>withCarla</name>
<version>0.0.0</version>
<description>The withCarla package</description>
<maintainer email="[email protected]">hihiroo</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend> <!-- 빌드 툴 = catkin -->
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>cv_bridge</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_depend>image_transport</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>cv_bridge</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<build_export_depend>image_transport</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>cv_bridge</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
<exec_depend>image_transport</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <image_transport/image_transport.h>
#include <ros/ros.h>
#include <vector>
#include <iostream>
using namespace std;
using namespace cv;
struct Subscribe_And_Publish{
ros::NodeHandle n;
image_transport::ImageTransport it;
image_transport::Subscriber getImage;
image_transport::Publisher pubLaneImage;
bool isRun;
Subscribe_And_Publish(bool isrun): isRun(isrun), it(n){
getImage = it.subscribe("/carla/ego_vehicle/camera/rgb/front/image_color", 1, &Subscribe_And_Publish::callback, this);
pubLaneImage = it.advertise("/laneImage",1);
}
void callback(const sensor_msgs::ImageConstPtr& img){
// sensor_msgs/Image -> opencv Mat type images
cv_bridge::CvImagePtr cv_ptr = cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::MONO8); // img를 인코딩하여 opencv Image타입으로 변환
//Canny(cv_ptr->image, cv_ptr->image, 100, 200);
pubLaneImage.publish(cv_ptr->toImageMsg()); // opencv Mat type -> sensor_msgs/Image type
}
};
int main(int argc, char **argv){
cout << "Start laneDetect node!\\n";
ros::init(argc, argv, "laneDetect");
Subscribe_And_Publish laneDetect(1);
ros::spin();
}
canny 함수로 엣지 검출하여 publish한 결과
toCvCopy함수 이미지 인코딩 방법
#include <cv_bridge/cv_bridge.h>
#include <opencv2/highgui/highgui.hpp>
#include <image_transport/image_transport.h>
#include <ros/ros.h>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
using namespace cv;
int i=0;
void callback(const sensor_msgs::ImageConstPtr& img){
cv_bridge::CvImagePtr cv_ptr;
cv_ptr = cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::BGR8);
string path = "/home/hihiroo/bagfiles/image1/carlaImage";
path += to_string(i) + ".png";
if(i++ % 10 == 0) imwrite(path, cv_ptr->image);
}
int main(int argc, char **argv){
cout << "Start laneDetect node!\\n";
ros::init(argc, argv, "saveImage");
ros::NodeHandle n;
image_transport::ImageTransport it(n);
image_transport::Subscriber sub = it.subscribe("/carla/ego_vehicle/camera/rgb/front/image_color", 10, callback);
ros::spin();
}