1 Star 0 Fork 0

CiQiBiFu/KeepRunning

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FindColor.txt 10.33 KB
一键复制 编辑 原始数据 按行查看 历史
提交于 2022-02-09 20:59 . 视觉资料
#include "GxIAPI.h"
#include"DxImageProc.h"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <iostream>
#include<sstream>
#include<fstream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv::dnn;
using namespace cv;
int hmin = 0, smin = 0, vmin = 0;
int hmax = 179, smax = 255, vmax = 255;//这里的数据需要自己提取
#define MEMORY_ALLOT_ERROR -1
char* m_rgb_image=NULL; //增加的内容
GX_DEV_HANDLE g_device = NULL; ///< 设备句柄
GX_FRAME_DATA g_frame_data = { 0 }; ///< 采集图像参数
pthread_t g_acquire_thread = 0; ///< 采集线程ID
bool g_get_image = false; ///< 采集线程是否结束的标志:true 运行;false 退出
//获取图像大小并申请图像数据空间
int PreForImage();
//释放资源
int UnPreForImage();
//采集线程函数
void *ProcGetImage(void* param);
//获取错误信息描述
void GetErrorString(GX_STATUS error_status);
int main()
{
uid_t user = 0;
user = geteuid();
if(user != 0)
{
printf("\n");
printf("Please run this application with 'sudo -E ./GxAcquireContinuous' or"
" Start with root !\n");
printf("\n");
return 0;
}
printf("\n");
printf("-------------------------------------------------------------\n");
printf("sample to show how to acquire image continuously.\n");
#ifdef __x86_64__
printf("version: 1.0.1605.8041\n");
#elif __i386__
printf("version: 1.0.1605.9041\n");
#endif
printf("-------------------------------------------------------------\n");
printf("\n");
printf("Press [x] or [X] and then press [Enter] to Exit the Program\n");
printf("Initializing......");
printf("\n\n");
usleep(2000000);
//API接口函数返回值
GX_STATUS status = GX_STATUS_SUCCESS;
uint32_t device_num = 0;
uint32_t ret = 0;
GX_OPEN_PARAM open_param;
//初始化设备打开参数,默认打开序号为1的设备
open_param.accessMode = GX_ACCESS_EXCLUSIVE;
open_param.openMode = GX_OPEN_INDEX;
open_param.pszContent = "1";
//初始化库
status = GXInitLib();
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
return 0;
}
//获取枚举设备个数
status = GXUpdateDeviceList(&device_num, 1000);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
status = GXCloseLib();
return 0;
}
if(device_num <= 0)
{
printf("<No device>\n");
status = GXCloseLib();
return 0;
}
else
{
//默认打开第1个设备
status = GXOpenDevice(&open_param, &g_device);
if(status == GX_STATUS_SUCCESS)
{
printf("<Open device success>\n");
}
else
{
printf("<Open device fail>\n");
status = GXCloseLib();
return 0;
}
}
//设置采集模式为连续采集
status = GXSetEnum(g_device, GX_ENUM_ACQUISITION_MODE, GX_ACQ_MODE_CONTINUOUS);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
status = GXCloseDevice(g_device);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
//设置触发开关为OFF
status = GXSetEnum(g_device, GX_ENUM_TRIGGER_MODE, GX_TRIGGER_MODE_OFF);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
status = GXCloseDevice(g_device);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
//为采集做准备
ret = PreForImage();
if(ret != 0)
{
printf("<Failed to prepare for acquire image>\n");
status = GXCloseDevice(g_device);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
//启动接收线程
ret = pthread_create(&g_acquire_thread, 0, ProcGetImage, 0);
if(ret != 0)
{
printf("<Failed to create the collection thread>\n");
status = GXCloseDevice(g_device);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
bool run = true;
while(run == true)
{
int c = getchar();
switch(c)
{
//退出程序
case 'X':
case 'x':
run = false;
break;
default:
break;
}
}
//为停止采集做准备
ret = UnPreForImage();
if(ret != 0)
{
status = GXCloseDevice(g_device);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
//关闭设备
status = GXCloseDevice(g_device);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
if(g_device != NULL)
{
g_device = NULL;
}
status = GXCloseLib();
return 0;
}
//释放库
status = GXCloseLib();
return 0;
}
//-------------------------------------------------
/**
\brief 获取图像大小并申请图像数据空间
\return void
*/
//-------------------------------------------------
int PreForImage()
{
GX_STATUS status = GX_STATUS_SUCCESS;
int64_t payload_size = 0;
status = GXGetInt(g_device, GX_INT_PAYLOAD_SIZE, &payload_size);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
return status;
}
g_frame_data.pImgBuf = malloc(payload_size);
if(g_frame_data.pImgBuf == NULL)
{
printf("<Failed to allot memory>\n");
return MEMORY_ALLOT_ERROR;
}
return 0;
}
//-------------------------------------------------
/**
\brief 释放资源
\return void
*/
//-------------------------------------------------
int UnPreForImage()
{
GX_STATUS status = GX_STATUS_SUCCESS;
uint32_t ret = 0;
//发送停采命令
status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_STOP);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
return status;
}
g_get_image = false;
ret = pthread_join(g_acquire_thread,NULL);
if(ret != 0)
{
printf("<Failed to release resources>\n");
return ret;
}
//释放buffer
if(g_frame_data.pImgBuf != NULL)
{
free(g_frame_data.pImgBuf);
g_frame_data.pImgBuf = NULL;
}
return 0;
}
//-------------------------------------------------
/**
\brief 采集线程函数
\param pParam 线程传入参数
\return void*
*/
//-------------------------------------------------
void *ProcGetImage(void* pParam)
{
GX_STATUS status = GX_STATUS_SUCCESS;
//接收线程启动标志
g_get_image = true;
//发送开采命令
status = GXSendCommand(g_device, GX_COMMAND_ACQUISITION_START);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
}
Mat img, imgResize;
Mat imgHsv, mask;
namedWindow("Trackbars", (640, 200));
createTrackbar("Hue min", "Trackbars", &hmin, 179);//测试色调Hue的值,此值最大为180
createTrackbar("Hue max", "Trackbars", &hmax, 179);
createTrackbar("Sat min", "Trackbars", &smin, 255);//测试饱和度Sat的值,此值最大为255
createTrackbar("Sat max", "Trackbars", &smax, 255);
createTrackbar("Val min", "Trackbars", &vmin, 255);//测试明度Val的值,此值最大为255
createTrackbar("Val max", "Trackbars", &vmax, 255);
while(g_get_image)
{
if(g_frame_data.pImgBuf == NULL)
{
continue;
}
status = GXGetImage(g_device, &g_frame_data, 100);
if(status == GX_STATUS_SUCCESS)
{
if(g_frame_data.nStatus == 0)
{
//增加的内容
Mat src;
m_rgb_image=new char[g_frame_data.nWidth*g_frame_data.nHeight*3];
src.create(g_frame_data.nHeight,g_frame_data.nWidth,CV_8UC3);
memcpy(src.data,g_frame_data.pImgBuf,g_frame_data.nWidth*g_frame_data.nHeight);
DxRaw8toRGB24(g_frame_data.pImgBuf,m_rgb_image,g_frame_data.nWidth, g_frame_data.nHeight,RAW2RGB_NEIGHBOUR,DX_PIXEL_COLOR_FILTER(BAYERBG),false);
memcpy(src.data,m_rgb_image,g_frame_data.nWidth*g_frame_data.nHeight*3);
namedWindow("test",0);
//write opencv codes
resize(src, src, Size(), 0.5, 0.5);
cvtColor(src, imgHsv, COLOR_BGR2HSV);
Scalar lower(hmin, smin, vmin);
Scalar upper(hmax, smax, vmax);
inRange(imgHsv, lower, upper, mask);
cout << hmin << "," << smin << "," << vmin << "," << hmax << "," << smax << "," << vmax << endl;
resize(src, imgResize, Size(), 0.5, 0.5);
imshow("test",src);
imshow("mask", mask);
waitKey(10);
//到这里增加结束
printf("<Successful acquisition : Width: %d Height: %d >\n", g_frame_data.nWidth, g_frame_data.nHeight);
}
}
}
}
//----------------------------------------------------------------------------------
/**
\brief 获取错误信息描述
\param emErrorStatus 错误码
\return void
*/
//----------------------------------------------------------------------------------
void GetErrorString(GX_STATUS error_status)
{
char *error_info = NULL;
size_t size = 0;
GX_STATUS status = GX_STATUS_SUCCESS;
// 获取错误描述信息长度
status = GXGetLastError(&error_status, NULL, &size);
if(status != GX_STATUS_SUCCESS)
{
GetErrorString(status);
return;
}
error_info = new char[size];
if (error_info == NULL)
{
printf("<Failed to allocate memory>\n");
return ;
}
// 获取错误信息描述
status = GXGetLastError(&error_status, error_info, &size);
if (status != GX_STATUS_SUCCESS)
{
printf("<GXGetLastError call fail>\n");
}
else
{
printf("%s\n", (char*)error_info);
}
// 释放资源
if (error_info != NULL)
{
delete []error_info;
error_info = NULL;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ciqibifu/keep-running.git
git@gitee.com:ciqibifu/keep-running.git
ciqibifu
keep-running
KeepRunning
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385