I'm trying to scatter all white pixels of this gradient image in matplotlib.pyplot:
import numpy as np
from PIL import Image
import cv2
from matplotlib import pyplot
img = Image.open(
"/root/.../aec.png").convert("L")
img = np.array(img)
kernel = np.ones((2, 2), np.uint8)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
ox, oy = np.where(gradient == 255)
pyplot.plot(ox, oy, ".k")
pyplot.show()
The original picture (...) has a resolution of 2123x1269 and looks like this:
My graph in pyplot shows my gradient picture 270° rotated clockwise and I don't understand why.
I tried pyplot.plot(oy, ox, ".k")
, then it's flipped to the x-axis compared to the original image. Rotating the original image with gradient = cv2.rotate(gradient, cv2.cv2.ROTATE_90_CLOCKWISE)
gives me coordinates different from the pixel coordinates of my orginal image. ( The xy-pixel-coordinates have to be the ones of the gradient image. ) Also the resolution of 2123x1269 should remain and the program should run as fast as possible.
How can I display the pixel coordinates of the gradient image in matplotlib.pyplot correctly?
That is because origin in opencv is at the top-left. Try reverting y axis on pyplot and exchange x and y.
EDIT: Just use plt.imshow(), it is the right function to display image data.
For anyone who ever encounters this problem, this is my final code:
import numpy as np
from PIL import Image
import cv2
from matplotlib import pyplot
img = Image.open(
"/root/.../aec.png").convert("L")
img = np.array(img)
kernel = np.ones((2, 2), np.uint8)
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
ox, oy = np.where(gradient == 255)
pyplot.plot(oy, ox, ".k")
pyplot.imshow(gradient)
pyplot.show()
Zoomed in version of the plot: correct plot , until now it servers my purpose.
Thanks @frab
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.