cv2 阈值在第二张图像上无法正常工作
cv2 treshold does not work correctly on second image
我是 python 的新手,我正在尝试使用背景减法来可视化前和 post 更改图像中的变化。
我使用 cv2
库编写了一个简短的脚本:
#!/usr/bin/env python
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#GRAYSCALE ONLY FOR TESTING
#Test with person appearing in image
img1 = cv.imread("images/1.jpg", 0)
img2 = cv.imread("images/2.jpg", 0)
img3 = cv.subtract(img1, img2)
ret,thresh1 = cv.threshold(img3,90,255,cv.THRESH_BINARY)
#Test with satelite image of japan landslide changes after earthquake
jl_before = cv.imread("images/japan_earthquake_before.jpg",0)
jl_after = cv.imread("images/japan_earthquake_after.jpg",0)
jl_subtraction = cv.subtract(jl_before, jl_after)
ret,thresh2 = cv.threshold(img3,20,255,cv.THRESH_BINARY)
images = [img1, img2, thresh1, jl_before, jl_after, thresh2]
titles = ["Image1", "Image2", "Changes", "Japan_Before", "Japan_After", "Japan_Changes" ]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
结果如下所示:
为什么第二组图像的掩膜中出现了第一组图像变化的掩膜?
我使用了不同的变量,thresh1
和 thresh2
。
任何帮助将不胜感激,因为我似乎找不到问题所在。
因为你在复制粘贴时漏掉了修改:
ret,thresh2 = cv.threshold(img3,20,255,cv.THRESH_BINARY)
^^^^
我是 python 的新手,我正在尝试使用背景减法来可视化前和 post 更改图像中的变化。
我使用 cv2
库编写了一个简短的脚本:
#!/usr/bin/env python
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
#GRAYSCALE ONLY FOR TESTING
#Test with person appearing in image
img1 = cv.imread("images/1.jpg", 0)
img2 = cv.imread("images/2.jpg", 0)
img3 = cv.subtract(img1, img2)
ret,thresh1 = cv.threshold(img3,90,255,cv.THRESH_BINARY)
#Test with satelite image of japan landslide changes after earthquake
jl_before = cv.imread("images/japan_earthquake_before.jpg",0)
jl_after = cv.imread("images/japan_earthquake_after.jpg",0)
jl_subtraction = cv.subtract(jl_before, jl_after)
ret,thresh2 = cv.threshold(img3,20,255,cv.THRESH_BINARY)
images = [img1, img2, thresh1, jl_before, jl_after, thresh2]
titles = ["Image1", "Image2", "Changes", "Japan_Before", "Japan_After", "Japan_Changes" ]
for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
结果如下所示:
为什么第二组图像的掩膜中出现了第一组图像变化的掩膜?
我使用了不同的变量,thresh1
和 thresh2
。
任何帮助将不胜感激,因为我似乎找不到问题所在。
因为你在复制粘贴时漏掉了修改:
ret,thresh2 = cv.threshold(img3,20,255,cv.THRESH_BINARY)
^^^^