Snowflake
Snowflake

Reputation: 49

Plotting one row of an image

How to plot only one row of an image with matplotlib in Python3?

I load an image using opencv and want to plot only one row of it.

For now I have only this code to read and show the whole image.

import cv2
import matplotlib.pyplot as plt
import numpy as np

image = cv2.imread("test.png", 3)

cv2.imshow("Original", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Upvotes: 1

Views: 1102

Answers (1)

Shir
Shir

Reputation: 1649

Here is a solution:

import cv2
import matplotlib.pyplot as plt
import numpy as np

image = cv2.imread(r"content.jpg", 3)

print(image.shape)


cv2.imshow("Original", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

row = 100
mask = np.zeros_like(image)
mask[row,:,:] = 1
image[mask == 0] = 0

cv2.imshow("Masked", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Original Image:

enter image description here

Masked Image:

enter image description here


Note that reading images with openCV yields numpy arrays, on which you may perform numpy operations such as slicing and masking.

Upvotes: 2

Related Questions