16,216
社区成员
发帖
与我相关
我的任务
分享
/*****main.cpp*****/
#include <iostream>
#include "UEyeOpenCV.hpp"
#include "opencv2/opencv.hpp"
#include <servocontroller.h>
#include <visp/vpPlot.h>
#include <visp/vpDot2.h>
#include <visp/vpDisplay.h>
#include <visp/vpImageIo.h>
#include <visp/vpImageTools.h>
#include <visp/vpDisplayOpenCV.h>
int main(){
/*导入opencv相机校正参数*/
cv::FileStorage fs("out_camera_data.xml", cv::FileStorage::READ);
cv::Mat cameraMatrix, distCoeffs;
fs["Camera_Matrix"] >> cameraMatrix;
fs["Distortion_Coefficients"] >> distCoeffs;
std::cout << "camera matrix: " << cameraMatrix << std::endl
<< "distortion coeffs: " << distCoeffs << std::endl;
cv::Mat image;
/*打开相机*/
UeyeOpencvCam cam = UeyeOpencvCam(752,480);
/*定义显示窗口*/
vpImage<unsigned char> I(480, 752); // Create a gray level image
cv::Mat grayFrame;
vpDisplayOpenCV d;
d.init(I, 0, 0, "Visual servoing on dot");
/*定义追踪点*/
vpDot2 blob;
blob.setGraphics(true);
blob.setGraphicsThickness(2);
vpImagePoint germ;
bool init_done = false;
/*设置com口*/
unsigned short target=1500;
ServoController com;
com.ServoController::createSerialPort();
com.setTarget(target*4);
while(true) {
cv::Mat frame= cam.getFrame(); //取每帧的图像
cv::cvtColor(frame,grayFrame, CV_BGR2GRAY); //转灰度图
cv::undistort(grayFrame,image,cameraMatrix,distCoeffs); //图像校正
cv::Mat image2;
cv::flip(image,image2,-1); //图像倒置
vpImageConvert::convert(image2, I); //转成ViSP库的图像
vpDisplay::display(I); //ViSP库的图像显示
// 等待鼠标点击目标点
if (! init_done) {
vpDisplay::displayText(I, vpImagePoint(10,10), "Click in the blob to initialize the tracker", vpColor::red);
if (vpDisplay::getClick(I, germ, false)) {
blob.initTracking(I, germ);
init_done = true;
}
}
else {
blob.track(I); //点追踪
vpImagePoint mcog; //点中心坐标
mcog=blob.getCog();
int x=mcog.get_i();
if (x<240){
target=target+1;
com.setTarget(target*4);
cv::waitKey(10);
}
else{
target=target-1;
com.setTarget(target*4);
cv::waitKey(10);
}
}
vpDisplay::flush(I);
}
}
//servocontroller.h
/*****servocontroller.h*****/
#ifndef SERVOCONTROLLER_H
#define SERVOCONTROLLER_H
#include <string>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <termios.h>
#include <QDebug>
#include <QString>
class ServoController
{
public:
virtual ~ServoController();
void createSerialPort();
void setTarget( unsigned short target );
private:
static const unsigned short mMinChannelValue = 4000;
static const unsigned short mMaxChannelValue = 8000;
unsigned short target;
unsigned char channelNumber;
QSerialPort* myCom;
QByteArray command;
};
#endif // SERVOCONTROLLER_H
/*****servocontroller.cpp*****/
#include "servocontroller.h"
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <iostream>
void ServoController::createSerialPort()
{
myCom= new QSerialPort();
myCom->setPortName("/dev/ttyACM0");
myCom->open(QIODevice::WriteOnly);
myCom->setBaudRate(9600);
myCom->setDataBits(QSerialPort::Data8);
myCom->setParity(QSerialPort::NoParity);
myCom->setStopBits(QSerialPort::OneStop);
myCom->setFlowControl(QSerialPort::NoFlowControl);
}
ServoController::~ServoController()
{
myCom->close();
}
void ServoController::setTarget(unsigned short target )
{
if (target<mMinChannelValue ){
target=mMinChannelValue;
}
if(target>mMaxChannelValue){
target=mMaxChannelValue;
}
command.resize(4);
command[0]=0x84;
command[1]=0x01;
command[2]=(char)(target & 0x7F);
command[3]=(char)((target >> 7) & 0x7F);
myCom-> QSerialPort::write(command);
}
/*****UEyeOpenCV.hpp*****/
#ifndef UEYEOPENCV_HPP
#define UEYEOPENCV_HPP
//#pragma once
#include <ueye.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
class UeyeOpencvCam {
public:
UeyeOpencvCam(int wdth, int heigh);
cv::Mat getFrame();
private:
HIDS hCam;
cv::Mat mattie;
int width;
int height;
};
#endif // UEYEOPENCV_HPP
/*****ueyeopencv.cpp*****/
#include <UEyeOpenCV.hpp>
#include <iostream>
#include <ueye.h>
UeyeOpencvCam::UeyeOpencvCam(int wdth, int heigh) {
width = wdth;
height = heigh;
mattie = cv::Mat(height, width, CV_8UC3);
hCam = 1;
char* ppcImgMem;
int pid;
INT nAOISupported = 0;
is_InitCamera(&hCam, NULL);
is_SetColorMode(hCam, IS_CM_BGR8_PACKED);
INT nGamma = 180;
is_Gamma(hCam, IS_GAMMA_CMD_SET, (void*) &nGamma, sizeof(nGamma));
is_ImageFormat(hCam, IMGFRMT_CMD_GET_ARBITRARY_AOI_SUPPORTED, (void*) &nAOISupported, sizeof(nAOISupported));
is_AllocImageMem(hCam, width, height, 24, &ppcImgMem, &pid);
is_SetImageMem(hCam, ppcImgMem, pid);
is_CaptureVideo(hCam, IS_WAIT);
}
cv::Mat UeyeOpencvCam::getFrame() {
VOID* pMem;
int retInt = is_GetImageMem(hCam, &pMem);
if (retInt != IS_SUCCESS) {
if (retInt == IS_INVALID_CAMERA_HANDLE){
std::cout<<"error CAMERA_HANDLE"<<std::endl;
}
else{
std::cout<<"error is_GetImageMem"<<std::endl;
}
}
else{
memcpy(mattie.ptr(), pMem, width * height * 3);
}
return mattie;
}
HEADERS += \
UEyeOpenCV.hpp \
servocontroller.h
SOURCES += \
main.cpp \
ueyeopencv.cpp \
servocontroller.cpp
# important for QSerialport
QT += serialport
# OpenCV库
INCLUDEPATH += /usr/local/include \
/usr/local/include/opencv \
/usr/local/include/opencv2
LIBS += /usr/local/lib/libopencv_core.so \
/usr/local/lib/libopencv_highgui.so \
/usr/local/lib/libopencv_imgproc.so
# UEye摄像头库
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../usr/lib/release/ -lueye_api
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../usr/lib/debug/ -lueye_api
else:unix: LIBS += -L$$PWD/../../../../usr/lib/ -lueye_api
INCLUDEPATH += $$PWD/../../../../usr/include
DEPENDPATH += $$PWD/../../../../usr/include
# ViSP库
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/release/ -lvisp
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/debug/ -lvisp
else:unix: LIBS += -L$$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu/ -lvisp
INCLUDEPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu
DEPENDPATH += $$PWD/../../ViSP-build-release/lib/x86_64-linux-gnu