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

Compression Beyond#

「Compression Beyond 」相機功能可讓您壓縮相機輸出的影像資料。

壓縮影像資料可降低相機的頻寬使用量、提高幀率,或兩者兼具。

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

使用功能#

運作原理#

每當……時,系統便會自動執行壓縮。 ImageCompressionMode 參數設為 BaslerCompressionBeyond.

啟用此功能後,系統將始終盡可能地壓縮影像。此舉可降低相機輸出的資料量(即「有效載荷」),也就是說,能減少其帶寬使用量。對於多相機配置中的 Basler GigE 相機而言,此功能特別實用。

若希望相機也能優化幀率,則需進行進一步的設定。這是因為在曝光開始之前,相機並「不知道」自己能將影像壓縮到什麼程度。

要解決這個問題,您必須告訴相機它將 至少 能夠壓縮圖片。請透過指定壓縮比來執行此操作(BslImageCompressionRatio 參數)。

舉例來說,設定 BslImageCompressionRatio 將此參數設為 90,即告知相機它能夠將任何影像壓縮至原始大小的至少 90%。根據這項資訊,相機的幀率最高可提升 10%。

然而,若將該值設定得太低,也就是說,若攝影機無法達到預估的壓縮比,則會跳過某些幀。在這種情況下,您可以變更 ImageCompressionRateOption 參數來自 Lossless 設為 FixRatio. 這樣一來,雖然可以實現更低的壓縮率,但會降低影像品質。

資訊

  • 在影像擷取過程中,您可以透過讀取 BslImageCompressionLastRatio 進行明確的生命週期管理即呼叫 BslImageCompressionLastSize 參數數值。
  • 當「Compression Beyond 」功能啟用時, pylon Viewer 會在影像顯示區域的狀態列中顯示有關此功能的資訊:

    Compression Beyond 狀態列

  • 在實作自訂的影像擷取例程時,必須使用 CImageDecompressor 類別來解壓縮擷取到的影像。如需更多資訊,請參閱《pylon API 》文件中的「影像解壓縮」章節。

設定Compression Beyond#

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

優化頻寬#

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

  • 降低頻寬使用量
  • 提高幀率
  • 維持最佳影像品質

要達成此目標:

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

採用這些設定時,相機會在不犧牲影像品質的前提下,將影像壓縮至最大程度。

透過無損壓縮來優化頻寬與幀率#

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

  • 降低頻寬使用量
  • 提高幀率
  • 維持最佳影像品質

要達成此目標:

  1. 確保相機處於閒置狀態,即未擷取影像。
  2. 將相機中的 ImageCompressionMode 參數設為 BaslerCompressionBeyond.
  3. 將相機中的 ImageCompressionRateOption 參數設為 Lossless.
  4. 將相機中的 BslImageCompressionRatio 將該參數設定為小於 100 的數值,例如 90。
  5. 取得 BslResultingAcquisitionFrameRate 用於確定預估幀率的參數。
  6. 如果幀率對於您的應用而言不夠高,請以較低的設定重複執行步驟 4 和 5 BslImageCompressionRatio 參數值。
  7. 開始擷取影像。若攝影機無法在不跳幀的情況下擷取影像,則無法達到預期的幀率。請嘗試以下方法:
    • 如果可以接受較低的幀率,請將 BslImageCompressionRatio 請調整此參數,直到相機能夠連續擷取影像為止。
    • 若能接受較低的影像品質,請依照以下詳細步驟操作。

透過有損壓縮來優化頻寬與幀率#

資訊

請務必先嘗試無損壓縮。只有在無法透過無損壓縮達到所需效果時,才使用有損壓縮。

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

  • 降低頻寬使用量
  • 提高幀率
  • 維持最佳影像品質

要達成此目標:

  1. 確保相機處於閒置狀態,即未擷取影像。
  2. 將相機中的 ImageCompressionMode 參數設為 BaslerCompressionBeyond.
  3. 將相機中的 ImageCompressionRateOption 參數設為 FixRatio.
  4. 將相機中的 BslImageCompressionRatio 參數設為 100.
  5. 取得至少一張圖片。
  6. 取得 BslImageCompressionLastRatio 參數中選擇性地指定 HTML 記錄檔 (*.html) 的路徑與檔名。
    此數值表示無損與有損影像擷取之間的閾值。
  7. 將相機中的 BslImageCompressionRatio 將參數設定為低於該值 BslImageCompressionLastRatio 參數中選擇性地指定 HTML 記錄檔 (*.html) 的路徑與檔名。
    參數值越低,影像品質就越差。
  8. 開始擷取影像。若攝影機無法在不跳幀的情況下擷取影像,請增加 BslImageCompressionRatio 該參數,直到相機能夠擷取影像為止。

採用這些設定後,與無損壓縮相比,您可能能夠降低頻寬使用量並提高幀率。不過,這會降低畫質。

範例程式碼#

// 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

您也可以 use the pylon Viewer 來輕鬆設定參數。