Home > Software engineering >  How to plot logical matrix in python
How to plot logical matrix in python

Time:05-20

How do I plot a logical array in python, for example, I have this code:

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)

How can I plot and see "g_dominant". Matlab treats it as a binary image but I have trouble getting it plot on cv2.imshow.

Thanks.

CodePudding user response:

You are mostly there, you need to enclose the conditional statement.

Code:

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)

Examples:

Input

enter image description here

Result

enter image description here

Input

enter image description here

Output

enter image description here

  • Related