我正在使用c ++和raspicam在图像像素值中遇到某些条件时拍摄图像。当条件被命中时,我在c ++代码中释放相机,调用调用raspistill的bash脚本,然后在bash脚本完成后重新打开相机。
不幸的是,当我重新打开相机时,我从相机中取回的数据卡在与释放相机之前的最后一帧相同的状态。我甚至尝试在我的init方法中慢跑相机几帧,但没有骰子。所有计算出的图像值在工作一次后都会卡在同一个vaule上。
以前有人遇到过这个问题吗?我知道我可以通过让c ++程序退出来解决它,当它找到我正在寻找的东西并将它包装在循环中的bash脚本中时,但这让我感到困扰。
作为参考,我使用Raspbian 8(jessie)并使用Raspberry Pi NoIR Camera V2。
这是我的主要方法,包括主处理循环:
int main ( int argc,char **argv ) {
raspicam::RaspiCam Camera = init_camera();
// Allocate memory for camera buffer
int bytes = Camera.getImageTypeSize ( raspicam::RASPICAM_FORMAT_GRAY );
unsigned char *data=new unsigned char[bytes];
unsigned int sum = 0;
int consec_pass = 0;
int finger_cnt = 0;
// main processing loop
while (true)
{
// Capture frame
Camera.grab();
// Extract the image
Camera.retrieve (data,raspicam::RASPICAM_FORMAT_IGNORE);
int dmin = 255;
int dmax = 0;
int nmin = 0;
int nmax = 0;
for (int i = 0; i < bytes; i++)
{
if (data[i] < dmin)
dmin = data[i];
if (data[i] > dmax)
dmax = data[i];
}
for (int i = 0; i < bytes; i++)
{
if (data[i] < dmin + 5)
nmin++;
if(data[i] > dmax - 5)
nmax++;
}
cout << "nmin, nmax, dmin, dmax " << nmin << nmax << dmin << dmax << "\n";
if ((nmin > NMIN_THRESH) && (nmax > NMAX_THRESH) && (dmin < MIN_THRESH) && (dmax < MAX_THRESH))
consec_pass++;
else
consec_pass = 0;
if (consec_pass > CONSEC_THRESH)
{
finger_cnt++;
cout << "FOUND FINGER! " << finger_cnt << "\n";
cout<<"Closing camera and calling bash script to take image... \n";
Camera.release();
std::string script_str ( "~/GetImage.sh ");
std::string num_str(std::to_string(finger_cnt));
system((script_str + num_str).c_str());
cout<<"Done! Reinitializing camera";
Camera = init_camera();
consec_pass = 0;
}
}
这是引用的init_camera()方法
raspicam::RaspiCam init_camera(){
raspicam::RaspiCam cam;
cam.setFormat(raspicam::RASPICAM_FORMAT_GRAY);
cam.setExposure(raspicam::RASPICAM_EXPOSURE_NIGHT);
// Open camera
cout<<"Opening Camera..."<<endl;
if ( !cam.open()) {cerr<<"Error opening camera"<<endl;}
// Wait until camera stabilizes
cout<<"Sleeping for 1 secs"<<endl;
usleep(1000000);
cout<<"Jogging the camera";
int bytes = cam.getImageTypeSize ( raspicam::RASPICAM_FORMAT_GRAY );
unsigned char *data=new unsigned char[bytes];
for (int i = 0; i < 5; i++)
{
// Capture frame
cam.grab();
// Extract the image
cam.retrieve (data,raspicam::RASPICAM_FORMAT_IGNORE);
}
return cam;}
最后这里是我正在使用的bash脚本(只是一个单行)
raspistill -w 400 -h 300 -co 75 -sh 100 -e png -roi 0.0,0.0,0.85,0.85 -t 2000 -o fingerprint$1.png
对于凌乱的格式,很抱歉,无法从gedit
中复制它