3 回答
TA贡献1812条经验 获得超5个赞
这是一个用于区分python的[row,columns]和OpenCV的[x,y]的可视示例。
import numpy as np
import matplotlib.pyplot as plt
import cv2
img = np.zeros((5,5)) # initialize empty image as numpy array
img[0,2] = 1 # assign 1 to the pixel of row 0 and column 2
M = cv2.moments(img) # calculate moments of binary image
cX = int(M["m10"] / M["m00"]) # calculate x coordinate of centroid
cY = int(M["m01"] / M["m00"]) # calculate y coordinate of centroid
img2 = np.zeros((5,5)) # initialize another empty image
img2[cX,cY] = 1 # assign 1 to the pixel with x = cX and y = cY
img3 = np.zeros((5,5)) # initialize another empty image
img3[cY,cX] = 1 # inver x and y
plt.figure()
plt.subplot(131), plt.imshow(img, cmap = "gray"), plt.title("With [rows,cols]")
plt.subplot(132), plt.imshow(img2, cmap = "gray"), plt.title("With [x,y]")
plt.subplot(133), plt.imshow(img3, cmap= "gray"), plt.title("With [y,x]")
添加回答
举报