Quellcode durchsuchen

修改优化自动曝光模式下的拍照逻辑

niujiuru vor 4 Wochen
Ursprung
Commit
ead9166903
2 geänderte Dateien mit 34 neuen und 14 gelöschten Zeilen
  1. 33 13
      mvs_u_takephoto/takephoto.c
  2. 1 1
      tests/mvs_u_takephoto/main.go

+ 33 - 13
mvs_u_takephoto/takephoto.c

@@ -14,6 +14,7 @@ typedef struct
   const char *saveImgFilename;
   bool        isExposureAuto; // 自动曝光:是/否
   struct timespec expTime0;   // 曝光开始的时间
+  float       lastExpTime;    // 上次的曝光时长
   HANDLE      hESig;          // 任务结束的通知
   int         rCode;          // 任务结束返回值
 } PthotoProcCtx;
@@ -39,13 +40,25 @@ getp:
     sw_log_debug("[%s] +++GetOneFrame+++, Width[%d], Height[%d], FrameNum[%d], FrameLen[%d], PixelType[0x%08X]", MODULE_NAME, \
                   frame.stFrameInfo.nWidth, frame.stFrameInfo.nHeight, frame.stFrameInfo.nFrameNum, frame.stFrameInfo.nFrameLen, \
                   (unsigned int)frame.stFrameInfo.enPixelType);
+
     if(ctx->isExposureAuto)
-    { // 自动曝光模式下, 等待15秒后的图像清晰度才可用
+    { // 自动曝光模式下, 等待曝光稳定或超时
+      MVCC_FLOATVALUE stExposureTime = { 0 }; float curExpTime = 0;
+      ret = MV_CC_GetExposureTime(ctx->hCam, &stExposureTime);
+      if(MV_OK == ret) curExpTime = stExposureTime.fCurValue;
+
       struct timespec now; clock_gettime(CLOCK_MONOTONIC, &now);
       long elapsed = (now.tv_sec - ctx->expTime0.tv_sec)*1000 + (now.tv_nsec-ctx->expTime0.tv_nsec)/(1000*1000);
-      sw_log_debug("[%s] +++GetOneFrame+++, 等待曝光完成, elapsed=%ldms", MODULE_NAME, elapsed);
-      if(elapsed < 15*1000) { MV_CC_FreeImageBuffer(ctx->hCam, &frame); goto getp/*继续取图像*/; }
+      sw_log_debug("[%s] +++GetOneFrame+++, 等待曝光完成, ExposureTime = %.2fus, ElapsedTime = %ldms", MODULE_NAME, curExpTime, elapsed);
+
+      if((curExpTime-ctx->lastExpTime) > 100 && elapsed < 3*60*1000)
+      {
+        MV_CC_FreeImageBuffer(ctx->hCam, &frame);
+        ctx->lastExpTime = curExpTime;
+        goto getp; // 继续取图像
+      }
     }
+
     ret = SavePhoto(ctx->hCam, &frame, ctx->saveImgType, ctx->saveImgFilename);
     MV_CC_FreeImageBuffer(ctx->hCam, &frame);
   }
@@ -138,13 +151,6 @@ int TakePhoto(EImgType imgType, const char *saveImgFilename, int timeout, SImgMa
   }
 
   // 4, 开始拍照, 等待完成后输出
-  ret = MV_CC_StartGrabbing(hCam);
-  if(MV_OK != ret)
-  {
-    sw_log_error("[%s] USB口相机取流失败, errCode=0x%x!!", MODULE_NAME, ret);
-    goto end_p;
-  }
-
   MVCC_ENUMVALUE ExposureMode =  { 0 };
   ret =  MV_CC_GetExposureAutoMode(hCam, &ExposureMode);
   if(MV_OK != ret)
@@ -155,6 +161,20 @@ int TakePhoto(EImgType imgType, const char *saveImgFilename, int timeout, SImgMa
   if(ExposureMode.nCurValue != MV_EXPOSURE_AUTO_MODE_OFF) { ctx.isExposureAuto = true; clock_gettime(CLOCK_MONOTONIC, &ctx.expTime0); }
   else ctx.isExposureAuto = false;
 
+  if(ctx.isExposureAuto && sw_file_exists("exposure_time.txt"))
+  {
+    char buf[MAX_LINE_CHARS] = { 0 };
+    int ret = sw_file_load("exposure_time.txt", "r", buf, sizeof(buf));
+    if(ret > 0) MV_CC_SetExposureTime(hCam, (float)atof(buf));
+  }
+
+  ret = MV_CC_StartGrabbing(hCam);
+  if(MV_OK != ret)
+  {
+    sw_log_error("[%s] USB口相机取流失败, errCode=0x%x!!", MODULE_NAME, ret);
+    goto end_p;
+  }
+
   ctx.hCam = hCam;
   ctx.saveImgType = imgType;
   ctx.saveImgFilename = saveImgFilename;
@@ -191,9 +211,9 @@ int TakePhoto(EImgType imgType, const char *saveImgFilename, int timeout, SImgMa
   // 5, 成功拍照, 输出相机的信息
   if(MV_OK == ret)
   {
-    MVCC_FLOATVALUE fv = { 0 };
-    MV_CC_GetFloatValue(hCam, "ExposureTime", &fv);
-    imgMark.imgExposureTime = fv.fCurValue;
+    char buf[MAX_LINE_CHARS] = { 0 }; sprintf(buf, "%.2f", ctx.lastExpTime);
+    sw_file_update("exposure_time.txt", "w", buf, strlen(buf));
+    imgMark.imgExposureTime = ctx.lastExpTime;
     if(pImgMark) memcpy(pImgMark, &imgMark, sizeof(SImgMark));
   }
 

+ 1 - 1
tests/mvs_u_takephoto/main.go

@@ -31,7 +31,7 @@ func main() {
 		var err error
 
 		start := time.Now()
-		imgMark, err = camera.TakePhoto(imgType, imgFile, 180)
+		imgMark, err = camera.TakePhoto(imgType, imgFile, 5*60)
 		elapsed := time.Since(start).Milliseconds()
 
 		if err == nil {