CV2形态打开未按预期工作

2024-05-15 04:50:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我对OpenCV有一个问题,其中函数形态学打开未按预期工作。如下图所示,图像的形态学打开会导致阈值模式向下和向右移动

你知道为什么会发生这种移动吗

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

img = cv2.imread("originalImg.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret,thresh1 = cv2.threshold(img,0,255,cv2.THRESH_TRIANGLE)
print("Calculated threshold", ret)

f, axarr = plt.subplots(1,5)
kernel = np.ones((2,2),np.uint8)
thresh = thresh/255
opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

axarr[0].imshow(img,cmap='gray')
axarr[0].set_title('original image')
axarr[1].imshow(thresh1,cmap='gray')
axarr[1].set_title('threshold triangle')
axarr[2].imshow(opened,cmap='gray')
axarr[2].set_title('morphology open')
plt.show()

![https://imgur.com/a/N6RMy4T](https://imgur.com/a/N6RMy4T)

生成的图像:https://imgur.com/a/N6RMy4T


Tags: 图像importimgthresholdtitlenppltcv2

热门问题