做網(wǎng)站有哪些公司好友鏈出售
RLE 編碼
RLE(Run-Length Encoding)是一種簡單而有效的無損數(shù)據(jù)壓縮和編碼方法。它的基本思想是將連續(xù)相同的數(shù)據(jù)值序列用一個值和其連續(xù)出現(xiàn)的次數(shù)來表示,從而減少數(shù)據(jù)的存儲或傳輸量。
在圖像分割領(lǐng)域(如 COCO 數(shù)據(jù)集中),RLE 通常被用來表示二進制掩碼。二進制掩碼是由 0 和 1 組成的矩陣,其中 0 表示背景,1 表示前景(或某個特定的物體)。使用 RLE 編碼可以有效地表示連續(xù)的相同值序列。
RLE 編碼的基本思想是按照連續(xù)的相同值(通常是 0 或 1)計算它們的重復(fù)次數(shù),然后將這個值和重復(fù)次數(shù)以一定的格式編碼。具體格式可以有多種,但在 COCO 數(shù)據(jù)集中,RLE 通常是以一系列數(shù)字的形式表示的,每兩個數(shù)字表示一個重復(fù)次數(shù)和對應(yīng)的值。
coco api 中 的 encode 將binary mask 轉(zhuǎn)為 rle。decode將 rle 轉(zhuǎn)換為 binary mask.
mmdetction 中的maskrcnn 預(yù)測輸出的 segmentation 也是 rle格式.
將 polygon 轉(zhuǎn)換為 rle
def polygon_to_rle(polygon, image_height, image_width):# 創(chuàng)建一個 Shapely 多邊形對象poly = Polygon(polygon)# 獲取多邊形的輪廓exterior = list(poly.exterior.coords)# 創(chuàng)建 COCO 格式的分割掩碼segmentation = [int(coord) for xy in exterior for coord in xy]# 將分割掩碼編碼為 RLErle_encoded = mask_utils.frPyObjects([segmentation], image_height, image_width)return rle_encoded# 示例使用
polygon = [[10, 10], [50, 10], [50, 50], [10, 50]] # 一個簡單的正方形
image_height, image_width = 100, 100 # 圖像的高度和寬度,根據(jù)實際情況設(shè)置rle_encoded = polygon_to_rle(polygon, image_height, image_width)
print(rle_encoded)
binary_mask = mask_utils.decode(rle_encoded)
plt.imshow(binary_mask)
將 polygon 轉(zhuǎn)換為 mask
import cv2
def poly2mask(points, width, height):mask = np.zeros((width, height), dtype=np.int32)obj = np.array([points], dtype=np.int32)cv2.fillPoly(mask, obj, 1)return maskpolygon= [[10, 10], [50, 10], [50, 50], [10, 50]]mask = poly2mask(polygon, 100, 100)plt.imshow(mask)