如何在 python 中绘制逻辑矩阵

How to plot logical matrix in python

如何在python中绘制一个逻辑数组,例如,我有这样的代码:

import numpy as np
import matplotlib.pyplot as plt

filename = r"test.jpg"
img = cv2.imread(filename)
cv2.imshow("image", img)

b, g, r = cv2.split(img)
cv2.imshow('Green', g)

g_dominant = (g > b) & (g > r)

如何绘制并查看“g_dominant”。 Matlab 将其视为二进制图像,但我无法将其绘制在 cv2.imshow.

谢谢。

你多半是,需要附上条件语句

代码:

img = cv2.imread('parrot.jpg')
b, g, r = cv2.split(img)

# create 2-channel image of the same shape
# here is where we will display the result
mask = np.zeros((img.shape[0], img.shape[1]), np.uint8)

# Coordinates where green pixels are dominant are made 255 (white)
mask[(g > r) & (g > b)] = 255
cv2.imshow('Result', mask)
cv2.waitKey(0)

示例:

输入

结果

输入

输出