Jul-09-2017, 04:34 PM
I have problem on Communication between C++ and Python using subprocess Module
it is my Python Code for getting data.(it gets data from C++ OpenCV code.) (the data is coordinate x,y)
line for communication between languages is colored by red.
[output]
it is my Python Code for getting data.(it gets data from C++ OpenCV code.) (the data is coordinate x,y)
#this is imported for Inter-Language Communication! from subprocess import Popen, PIPE #Open Connection for CPP! MyPopen = Popen(['~/ImageProcessingToGimbal/build/ZED_with_OpenCV'], shell=True, stdout=PIPE, stdin=PIPE) while(1) : result = MyPopen.stdout.readline().strip() print(result)and this is my OpenCV code in C++
line for communication between languages is colored by red.
[color=#ff3333]#include <stdio.h>
#include <string.h>
#include <iostream>
//this is imported for Communication between inter-language.[/color]
//this is imported for video processing.
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
VideoCapture cap(0); //capture the video from web cam
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
int iLowH = 0;
int iHighH = 179;
int iLowS = 0;
int iHighS = 255;
int iLowV = 0;
int iHighV = 255;
//Create trackbars in "Control" window
cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
cvCreateTrackbar("HighH", "Control", &iHighH, 179);
cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
cvCreateTrackbar("HighS", "Control", &iHighS, 255);
cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
cvCreateTrackbar("HighV", "Control", &iHighV, 255);
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
Mat revimgThresholded; //하얀색 검은색 뒤집기!
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded);
//Threshold the image
//inRange 함수는 뒤집기(하얀색이 검은색으로, 검은색이 하얀색으로)가 안되므로 Threshold 함수를 이용한다.
threshold(imgThresholded, revimgThresholded, 2, 255, 1);
/*
http://docs.opencv.org/2.4/doc/tutorials/imgproc/threshold/threshold.html
0: Binary
1: Binary Inverted
2: Threshold Truncated
3: Threshold to Zero
4: Threshold to Zero Inverted
원래
THRESH_BINARY_INV
*/
//threshold 함수는 흑백에서만 되고 컬러에서는 안된다
//threshold(imgHSV, imgThresholded, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), CV_THRESH_BINARY_INV);
Mat imgBlobbing;
//위 : 블로핑한 결과에 빨간 동그라미를 치기 위한 Mat
//morphological opening (remove small objects from the foreground)
erode(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
dilate(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//morphological closing (fill small holes in the foreground)
dilate(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
erode(revimgThresholded, revimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
//블로빙 시작
//패러미터 설정을 위해 객체를 선언한다.
SimpleBlobDetector::Params params;
//블롭 패러미터 설정!
//params.filterByColor = 1;
//params.blobColor = 0;
// Change thresholds
// params.minThreshold = 0;
// params.maxThreshold = 50;
// params.filterByArea = 1;
// params.minArea = 50;
// params.maxArea = 10000;
params.filterByArea = true;
params.minArea = 100;
params.maxArea = 100000000;
params.filterByInertia = false;
params.filterByConvexity = true;
params.minConvexity = 0.50;
//params.filterByArea = false;
//디텍터 객체를 디폴트 패러미터로서 선언한다.
SimpleBlobDetector Detector(params);
//블롭 탐색 시작
std::vector<KeyPoint> mykeypoints;
Detector.detect(revimgThresholded, mykeypoints);
//탐색된 블롭들에 빨간 동그라미들을 칩니다!
drawKeypoints(revimgThresholded, mykeypoints, imgBlobbing, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//이 코드 넣으면 Keypoint가 그려지는 것 뿐만 아니라 Threshold 이미지로 변해서 뺐음.
//drawKeypoints(revimgThresholded, keypoints, imgOriginal, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
//가장 큰 사이즈 추출단계
//사이즈, 좌표 둘 다 자료형 float이라고 가정하겠다
float maxsize = 0;
float maxx,maxy = 0;
for(std::vector<cv::KeyPoint>::iterator blobIterator = mykeypoints.begin(); blobIterator != mykeypoints.end(); blobIterator++){
//std::cout << "size of blob is: " << blobIterator->size << std::endl;
if(maxsize < blobIterator->size)
{
maxsize = blobIterator->size;
maxx = blobIterator->pt.x;
maxy = blobIterator->pt.y;
}
//std::cout << "max size : " << maxsize << endl;
//std::cout << "position is at : " << "x : " << maxx << "y : " << maxy << endl;
[color=#ff3333] std::cout << maxx << std::endl;
std::cout.flush();
std::cout << maxy << std::endl;
std::cout.flush(); [/color]
}
//이제 imshow로 출력하기만 하면 됩니다.
imshow("Thresholded Image Inverted", revimgThresholded); //show the thresholded image
imshow("Original", imgOriginal); //show the original image
imshow("imgBlobbing", imgBlobbing); //라벨링 이미지를 보여줌.
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}when I run python script , I met empty screen.[output]
