Hướng dẫn how do you merge two masks in python? - làm thế nào để bạn hợp nhất hai mặt nạ trong python?

m1 = [0,1,1,3]
m2 = [0,0,1,1]
data = [10,20,30,40]

Tôi muốn làm điều gì đó như thế này:

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30

Lưu ý, ví dụ này dẫn đến một lỗi

Hướng dẫn how do you merge two masks in python? - làm thế nào để bạn hợp nhất hai mặt nạ trong python?

Jaime

63,9K17 Huy hiệu vàng120 Huy hiệu bạc158 Huy hiệu đồng17 gold badges120 silver badges158 bronze badges

Đã hỏi ngày 22 tháng 3 năm 2013 lúc 20:24Mar 22, 2013 at 20:24

1

Bạn đang sử dụng danh sách Python thay vì các mảng Numpy. Thay vào đó hãy thử điều này:

import numpy as np

m1 = np.array([0,1,1,3])
m2 = np.array([0,0,1,1])

mask = (m1 == 1) & (m2 == 1)
data[mask]
# returns array([30])

Trong ví dụ của bạn, khi

import numpy as np

m1 = np.array([0,1,1,3])
m2 = np.array([0,0,1,1])

mask = (m1 == 1) & (m2 == 1)
data[mask]
# returns array([30])
8 là một danh sách,
import numpy as np

m1 = np.array([0,1,1,3])
m2 = np.array([0,0,1,1])

mask = (m1 == 1) & (m2 == 1)
data[mask]
# returns array([30])
9 được đánh giá là
>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
0 (giống nhau đối với
>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
1), vì vậy mặt nạ là
>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
0 và
>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
3.

Đã trả lời ngày 22 tháng 3 năm 2013 lúc 20:27Mar 22, 2013 at 20:27

Tiagotiagotiago

21.8K11 Huy hiệu vàng71 Huy hiệu bạc86 Huy hiệu đồng11 gold badges71 silver badges86 bronze badges

6

ma.mask_or (m1, m2, copy = false, co lại = true) [nguồn]#mask_or(m1, m2, copy=False, shrink=True)[source]#

Kết hợp hai mặt nạ với toán tử

>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
4.

Kết quả có thể là chế độ xem trên M1 hoặc M2 nếu cái kia là

>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
5 (tức là sai).

Tham số1, m2array_likem1, m2array_like

Mặt nạ đầu vào.

Copybool, tùy chọnbool, optional

Nếu bản sao là sai và một trong các đầu vào là

>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
5, hãy trả về chế độ xem mặt nạ đầu vào khác. Mặc định là sai.

co lại, tùy chọnbool, optional

Có nên thu nhỏ đầu ra xuống

>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])
5 nếu tất cả các giá trị của nó là sai. Mặc định là đúng.

ReturnSmaskOutput Maskmaskoutput mask

Các giá trị mặt nạ kết quả được che trong M1 hoặc M2.

RAISEVALUEERROR

Nếu M1 và M2 có các DTYpes linh hoạt khác nhau.

Ví dụ

>>> m1 = np.ma.make_mask([0, 1, 1, 0])
>>> m2 = np.ma.make_mask([1, 0, 0, 0])
>>> np.ma.mask_or(m1, m2)
array([ True,  True,  True, False])

Sau đây là 24 ví dụ mã của mặt nạ.merge (). Bạn có thể bỏ phiếu cho những người bạn thích hoặc bỏ phiếu cho những người bạn không thích và đi đến dự án gốc hoặc tệp nguồn bằng cách theo các liên kết trên mỗi ví dụ. Bạn cũng có thể muốn kiểm tra tất cả các chức năng/lớp có sẵn của mặt nạ mô -đun hoặc thử chức năng tìm kiếm.24 code examples of mask.merge(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mask, or try the search function

Hướng dẫn how do you merge two masks in python? - làm thế nào để bạn hợp nhất hai mặt nạ trong python?
.

Ví dụ 1

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #2

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #3

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #4

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #5

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #6

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #7

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #8

def annToRLE(self, ann):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        t = self.imgs[ann['image_id']]
        h, w = t['height'], t['width']
        segm = ann['segmentation']
        if type(segm) == list:
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = mask.frPyObjects(segm, h, w)
            rle = mask.merge(rles)
        elif type(segm['counts']) == list:
            # uncompressed RLE
            rle = mask.frPyObjects(segm, h, w)
        else:
            # rle
            rle = ann['segmentation']
        return rle 

Ví dụ #9

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #10

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #11

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #12

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #13

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #14

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #15

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #16

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #17

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #18

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #19

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #20

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #21

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #22

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #23

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Ví dụ #24

mask = (m1 == 1) & (m2 == 1)
data[mask] #should return 30
2

Làm cách nào để hợp nhất hai mặt nạ trong opencv?

Chỉ cần thêm chúng và clip trong OpenCV CV.Thêm (Mask1, Mask2, ... Maskn) ...
Đọc mặt nạ và chuyển đổi thành Float trong phạm vi 0 đến 1 ..
Thêm mặt nạ bằng cách bổ sung Python và nhân với 255 ..
Kẹp mặt nạ kết quả vào phạm vi 0 đến 255 và chuyển đổi trở lại Int ..

Làm thế nào để bạn áp dụng mặt nạ mảng trong Python?

Sử dụng hàm Masked_where (): Truyền hai mảng trong hàm dưới dạng tham số sau đó sử dụng Numpy.MA.Chức năng Masked_where () trong đó vượt qua điều kiện để che giấu và mảng để được che dấu.....
Sử dụng hàm nasked_where (), getmask () và masked_array (): Truyền hai mảng trong hàm dưới dạng tham số sau đó sử dụng Numpy.MA ..