跳至內容
測試伺服器
開發伺服器

Compression Beyond#

利用 Compression Beyond camera feature,您可以壓縮相機輸出的影像資料。

壓縮影像資料可以降低相機的頻寬使用量、提高畫面播放速率,或是兩者兼具。

如需詳細資訊,請參閱 Basler website 上的功能說明。

使用此功能#

運作原理#

只要進行以下操作,就會自動執行壓縮: ImageCompressionMode 參數已設定為 BaslerCompressionBeyond.

啟用後,該功能一律會盡可能壓縮影像。這會降低相機輸出的資料量(「payload」),也就是其頻寬使用量。這對於多相機設定中的 Basler GigE 相機特別有用。

如果您希望相機同時最佳化畫面播放速率,則需要進行進一步的設定。這是因為相機在曝光開始之前,無法「預知」能夠將影像壓縮多少。

為了解決這個問題,您必須告知相機影像 至少 能夠壓縮多少。這可透過指定壓縮比(BslImageCompressionRatio parameter)來達成。

例如,將 BslImageCompressionRatio parameter 設定為 90,即表示相機可以將任何影像壓縮至其原始大小的至少 90%。有了這項資訊,相機就能將畫面播放速率提高最多 10%。

不過,如果您將數值設定得太低,亦即相機無法達到預估的壓縮比,則會發生掉格現象。在這種情況下,您可以變更 ImageCompressionRateOption parameter 自 Lossless 設定為 FixRatio。這可允許更低的壓縮率,但會降低影像的 Quality。

資訊

  • 在影像擷取期間,您可以透過讀取 BslImageCompressionLastRatio 和 BslImageCompressionLastSize parameter values,來決定最後擷取之影像的 payload 大小與壓縮比。
  • 啟用 Compression Beyond 時,pylon Viewer會在影像顯示區域的狀態列中顯示有關該功能的資訊:

    Compression Beyond Status Bar

  • 當您實作自己的影像擷取常式時,必須使用 CImageDecompressor 類別來解壓縮擷取的影像。如需詳細資訊,請參閱 pylon API Documentation 中的 Image Decompression。

設定 Compression Beyond#

如何設定 Compression Beyond 取決於您的需求。

最佳化頻寬#

假設您希望相機執行以下操作:

  • 降低頻寬使用量
  • 提高畫面播放速率
  • 維持最佳的影像 Quality

若要達到此目的:

  1. 確保相機處於閒置狀態,即未擷取影像。
  2. 將 ImageCompressionMode 參數設為 BaslerCompressionBeyond.
  3. 將 ImageCompressionRateOption 參數設為 Lossless.
  4. 將 BslImageCompressionRatio parameter 設為 100。

透過這些設定,相機可在不犧牲 image Quality 的情況下盡可能壓縮影像。

使用無損壓縮最佳化頻寬與影格率#

假設您希望相機執行以下操作:

  • 降低頻寬使用量
  • 提高畫面播放速率
  • 維持最佳的影像 Quality

若要達到此目的:

  1. 確保相機處於閒置狀態,即未擷取影像。
  2. 將 ImageCompressionMode 參數設為 BaslerCompressionBeyond.
  3. 將 ImageCompressionRateOption 參數設為 Lossless.
  4. 將 BslImageCompressionRatio parameter 設為低於 100 的值,例如 90。
  5. 取得 BslResultingAcquisitionFrameRate parameter 以決定預估的畫面更新率。
  6. 如果畫面更新率不夠高而無法滿足您的應用需求,請使用較低的參數重複步驟 4 和 5。 BslImageCompressionRatio 參數值。
  7. 開始影像擷取。如果相機在擷取影像時發生掉幀現象,表示無法達到所需的畫面更新率。請嘗試以下方法:
    • 如果能接受較低的畫面更新率,請提高 BslImageCompressionRatio parameter 的值,直到相機能夠連續擷取影像為止。
    • 如果能接受較低的影像 Quality,請執行以下詳細步驟。

使用有損壓縮最佳化頻寬與影格率#

資訊

請務必優先嘗試 lossless compression。只有在無法透過 lossless compression 達到所需結果時,才使用 lossy compression。

假設您希望相機執行以下操作:

  • 降低頻寬使用量
  • 提高畫面播放速率
  • 維持最佳的影像 Quality

若要達到此目的:

  1. 確保相機處於閒置狀態,即未擷取影像。
  2. 將 ImageCompressionMode 參數設為 BaslerCompressionBeyond.
  3. 將 ImageCompressionRateOption 參數設為 FixRatio.
  4. 將 BslImageCompressionRatio 參數設為 100.
  5. 至少擷取一張影像。
  6. 取得 BslImageCompressionLastRatio 參數的值。
    此數值表示無損與有損影像擷取之間的閾值。
  7. 將 BslImageCompressionRatio parameter 設為低於 BslImageCompressionLastRatio 參數的值。
    參數值越低,影像 Quality 就越低。
  8. 開始影像擷取。如果相機在擷取影像時發生掉幀現象,請提高 BslImageCompressionRatio parameter 的值,直到相機能夠擷取影像為止。

透過這些設定,相較於 lossless compression,您或許能降低頻寬使用量並提高畫面更新率。不過,這會降低影像 Quality。

範例程式碼#

// Enable Compression Beyond with lossless compression
camera.ImageCompressionMode.SetValue(ImageCompressionMode_BaslerCompressionBeyond);
camera.ImageCompressionRateOption.SetValue(ImageCompressionRateOption_Lossless);
// To increase the frame rate, tell the camera that it will be able
// to compress any image to at least 70 % of its original size
camera.BslImageCompressionRatio.SetValue(70);
// Get the resulting acquisition frame rate
double resultingFrameRate = camera.BslResultingAcquisitionFrameRate.GetValue();
if (resultingFrameRate < desiredFrameRate) {
    // Frame rate isn't high enough for the application.
    // Therefore, enable lossy compression. This allows even
    // lower compression rates, but reduces image quality.
    camera.ImageCompressionRateOption.SetValue(ImageCompressionRateOption_FixRatio);
    camera.BslImageCompressionRatio.SetValue(50);
}
// Start image acquisition
// ...
// (Implement your own image grabbing routine here.
// For example, the InstantCamera class provides the StartGrabbing() method.
// Note that you must decompress grabbed images using the CImageDecompressor class.)
// ...
// Determine the compression ratio of the last acquired image
double lastRatio = camera.BslImageCompressionLastRatio.GetValue();
// Determine the payload size of the last acquired image in bytes
int64_t lastSize = camera.BslImageCompressionLastSize.GetValue();
INodeMap& nodemap = camera.GetNodeMap();
// Enable Compression Beyond with lossless compression
CEnumParameter(nodemap, "ImageCompressionMode").SetValue("BaslerCompressionBeyond");
CEnumParameter(nodemap, "ImageCompressionRateOption").SetValue("Lossless");
// To increase the frame rate, tell the camera that it will be able
// to compress any image to at least 70 % of its original size
CIntegerParameter(nodemap, "BslImageCompressionRatio").SetValue(70);
// Get the resulting acquisition frame rate
double resultingFrameRate = CFloatParameter(nodemap, "BslResultingAcquisitionFrameRate").GetValue();
if(resultingFrameRate < desiredFrameRate){    
    // Frame rate isn't high enough for the application.
    // Therefore, enable lossy compression. This allows even
    // lower compression rates, but reduces image quality.
    CEnumParameter(nodemap, "ImageCompressionRateOption").SetValue("FixRatio");
    CIntegerParameter(nodemap, "BslImageCompressionRatio").SetValue(50);
}
// Start image acquisition
// ...
// (Implement your own image grabbing routine here.
// For example, the InstantCamera class provides the StartGrabbing() method.
// Note that you must decompress grabbed images using the CImageDecompressor class.)
// ...
// Determine the compression ratio of the last acquired image
double lastRatio = CFloatParameter(nodemap, "BslImageCompressionLastRatio").GetValue();
// Determine the payload size of the last acquired image in bytes
int64_t lastSize = CIntegerParameter(nodemap, "BslImageCompressionLastSize").GetValue();
// Enable Compression Beyond with lossless compression
camera.Parameters[PLCamera.ImageCompressionMode].SetValue(PLCamera.ImageCompressionMode.BaslerCompressionBeyond);
camera.Parameters[PLCamera.ImageCompressionRateOption].SetValue(PLCamera.ImageCompressionRateOption.Lossless);
// To increase the frame rate, tell the camera that it will be able
// to compress any image to at least 70 % of its original size
camera.Parameters[PLCamera.BslImageCompressionRatio].SetValue(70);
// Get the resulting acquisition frame rate
double resultingFrameRate = camera.Parameters[PLCamera.BslResultingAcquisitionFrameRate].GetValue();
if(resultingFrameRate < desiredFrameRate){    
    // Frame rate isn't high enough for the application.
    // Therefore, enable lossy compression. This allows even
    // lower compression rates, but reduces image quality.
    camera.Parameters[PLCamera.ImageCompressionRateOption].SetValue(PLCamera.ImageCompressionRateOption.FixRatio);
    camera.Parameters[PLCamera.BslImageCompressionRatio].SetValue(50);
}
// Start image acquisition
// ...
// (Implement your own image grabbing routine here.
// For example, the InstantCamera class provides the StartGrabbing() method.
// Note that you must decompress grabbed images using the CImageDecompressor class.)
// ...
// Determine the compression ratio of the last acquired image
double lastRatio = camera.Parameters[PLCamera.BslImageCompressionLastRatio].GetValue();
// Determine the payload size of the last acquired image in bytes
Int64 lastSize = camera.Parameters[PLCamera.BslImageCompressionLastSize].GetValue();
/* Macro to check for errors */
#define CHECK(errc) if (GENAPI_E_OK != errc) printErrorAndExit(errc)
GENAPIC_RESULT errRes = GENAPI_E_OK;  /* Return value of pylon methods */
double resultingFrameRate = 0;
double lastRatio = 0;
int64_t lastSize = 0;
/* Enable Compression Beyond with lossless compression */
errRes = PylonDeviceFeatureFromString(hdev, "ImageCompressionMode", "BaslerCompressionBeyond");
CHECK(errRes);
errRes = PylonDeviceFeatureFromString(hdev, "ImageCompressionRateOption", "Lossless");
CHECK(errRes);
/* To increase the frame rate, tell the camera that it will be able */
/* to compress any image to at least 70 % of its original size */
errRes = PylonDeviceSetIntegerFeature(hdev, "BslImageCompressionRatio", 70);
CHECK(errRes);
/* Get the resulting acquisition frame rate */
errRes = PylonDeviceGetFloatFeature(hdev, "BslResultingAcquisitionFrameRate", &resultingFrameRate);
CHECK(errRes);
if(resultingFrameRate < desiredFrameRate){    
    /* Frame rate isn't high enough for the application. */
    /* Therefore, enable lossy compression. This allows even */
    /* lower compression rates, but reduces image quality. */
    errRes = PylonDeviceFeatureFromString(hdev, "ImageCompressionRateOption", "FixRatio");
    CHECK(errRes);
    errRes = PylonDeviceSetIntegerFeature(hdev, "BslImageCompressionRatio", 50);
    CHECK(errRes);
}
/* Start image acquisition */
/* ... */
/* (Implement your own image grabbing routine here. */
/* For example, the InstantCamera class provides the StartGrabbing() method. */
/* Note that you must decompress grabbed images using the CImageDecompressor class.) */
/* ... */
/* Determine the compression ratio of the last acquired image */
errRes = PylonDeviceGetFloatFeature(hdev, "BslImageCompressionLastRatio", &lastRatio);
CHECK(errRes);
/* Determine the payload size of the last acquired image in bytes */
errRes = PylonDeviceGetIntegerFeature(hdev, "BslImageCompressionLastSize", &lastSize);
CHECK(errRes);
# Enable Compression Beyond with lossless compression
camera.ImageCompressionMode.Value = "BaslerCompressionBeyond"
camera.ImageCompressionRateOption.Value = "Lossless"
# To increase the frame rate, tell the camera that it will be able
# to compress any image to at least 70 % of its original size
camera.BslImageCompressionRatio.Value = 70
# Get the resulting acquisition frame rate
resultingFrameRate = camera.BslResultingAcquisitionFrameRate.Value
if resultingFrameRate < desiredFrameRate:
    # Frame rate isn't high enough for the application.
    # Therefore, enable lossy compression. This allows even
    # lower compression rates, but reduces image quality.
    camera.ImageCompressionRateOption.Value = "FixRatio"
    camera.BslImageCompressionRatio.Value = 50
# Start image acquisition
# ...
# (Implement your own image grabbing routine here.
# For example, the InstantCamera class provides the StartGrabbing() method.
# Note that you must decompress grabbed images using the CImageDecompressor class.)
# ...
# Determine the compression ratio of the last acquired image
lastRatio = camera.BslImageCompressionLastRatio.Value
# Determine the payload size of the last acquired image in bytes
lastSize = camera.BslImageCompressionLastSize.Value

您也可以使用 pylon Viewer 來輕鬆設定參數。