无法在 Ubuntu 中执行 pthread 程序
我编写了一个同时使用 openCV 和 Pthreads 的 C++ 程序。
代码如下,
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
using namespace cv;
using namespace std;
int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;
Mat img;
Mat display;
Mat temp;
void CannyThreshold()
{
cvtColor(img, display, COLOR_RGB2GRAY);
GaussianBlur(display, display, Size(1, 1), 1,1);
Canny(display,display,lowThreshold,3);
imshow("Canny",display);
}
void Hough()
{
Mat dip = temp.clone();
medianBlur(temp,temp,9);
Canny(temp,display,50,200);
vector<Vec2f> lines; // will hold the results of the detection
HoughLines(display, lines, 1, CV_PI/180, 150, 30, 0 ); // runs the actual detection
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line(dip, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
}
printf("Lines = %ld\n",lines.size());
imshow("Hough",dip);
}
void * transform(void * param)
{
VideoCapture cap(0);
namedWindow("Canny");
createTrackbar("Min Threshold: ","Canny",&lowThreshold,max_lowThreshold);
while(1)
{
cap.read(img);
temp = img;
CannyThreshold();
Hough();
waitKey(1);
}
cap.release();
}
int main()
{
pthread_t transformThread;
pthread_attr_t transformThread_attr;
struct sched_param rt_param;
pthread_attr_init(&transformThread_attr);
pthread_attr_setinheritsched(&transformThread_attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&transformThread_attr, SCHED_FIFO);
int rt_max_prio = sched_get_priority_max(SCHED_FIFO);
int rt_min_prio = sched_get_priority_min(SCHED_FIFO);
rt_min_prio += 1; //To avoid compiler error for unused variable
rt_param.sched_priority = rt_max_prio - 10;
pthread_attr_setschedparam(&transformThread_attr, &rt_param);
int ret = pthread_create(&transformThread,&transformThread_attr,transform,NULL);
if(ret !=0)
{
perror("pthread_create()");
}
void * res;
pthread_join(transformThread,&res);
return 0;
}
我编写的make文件如下,
CC = g++
CFLAGS = -g -Wall
SRCS = main.cpp
PROG = main
LIBS = -lrt
CPPLIBS= -L/usr/lib -lopencv_core -lopencv_flann -lopencv_video -lpthread -pthread
OPENCV = `pkg-config opencv --cflags --libs`
$(PROG):$(SRCS)
$(CC) $(CFLAGS) -o $(PROG) $(SRCS) `pkg-config --libs opencv4` $(CPPLIBS)
clean:
rm -rf main
当我执行程序时(不是当我编译它时),我收到以下错误。
pthread_create():没有这样的文件或目录
如何解决此问题?对此有何建议?
I have written a C++ program that makes use of both openCV and Pthreads.
The code is as below,
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
using namespace cv;
using namespace std;
int lowThreshold=0;
int const max_lowThreshold = 100;
int kernel_size = 3;
int ratio = 3;
Mat img;
Mat display;
Mat temp;
void CannyThreshold()
{
cvtColor(img, display, COLOR_RGB2GRAY);
GaussianBlur(display, display, Size(1, 1), 1,1);
Canny(display,display,lowThreshold,3);
imshow("Canny",display);
}
void Hough()
{
Mat dip = temp.clone();
medianBlur(temp,temp,9);
Canny(temp,display,50,200);
vector<Vec2f> lines; // will hold the results of the detection
HoughLines(display, lines, 1, CV_PI/180, 150, 30, 0 ); // runs the actual detection
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line(dip, pt1, pt2, Scalar(0,0,255), 3, LINE_AA);
}
printf("Lines = %ld\n",lines.size());
imshow("Hough",dip);
}
void * transform(void * param)
{
VideoCapture cap(0);
namedWindow("Canny");
createTrackbar("Min Threshold: ","Canny",&lowThreshold,max_lowThreshold);
while(1)
{
cap.read(img);
temp = img;
CannyThreshold();
Hough();
waitKey(1);
}
cap.release();
}
int main()
{
pthread_t transformThread;
pthread_attr_t transformThread_attr;
struct sched_param rt_param;
pthread_attr_init(&transformThread_attr);
pthread_attr_setinheritsched(&transformThread_attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&transformThread_attr, SCHED_FIFO);
int rt_max_prio = sched_get_priority_max(SCHED_FIFO);
int rt_min_prio = sched_get_priority_min(SCHED_FIFO);
rt_min_prio += 1; //To avoid compiler error for unused variable
rt_param.sched_priority = rt_max_prio - 10;
pthread_attr_setschedparam(&transformThread_attr, &rt_param);
int ret = pthread_create(&transformThread,&transformThread_attr,transform,NULL);
if(ret !=0)
{
perror("pthread_create()");
}
void * res;
pthread_join(transformThread,&res);
return 0;
}
The make file that I have written is as follows,
CC = g++
CFLAGS = -g -Wall
SRCS = main.cpp
PROG = main
LIBS = -lrt
CPPLIBS= -L/usr/lib -lopencv_core -lopencv_flann -lopencv_video -lpthread -pthread
OPENCV = `pkg-config opencv --cflags --libs`
$(PROG):$(SRCS)
$(CC) $(CFLAGS) -o $(PROG) $(SRCS) `pkg-config --libs opencv4` $(CPPLIBS)
clean:
rm -rf main
I am getting the following error when I execute the program (Not when I compile it).
pthread_create(): No such file or directory
How do I get past this issue? Any suggestions on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论