**
OpenCV入门学习20220708
运行软件:Jupyter
语言:python
**
代码例子源于OpenCV官网的学习手册:https://opencv.org/
例子:
加载一个图像,使用4种不同的滤波器(blur();GaussianBlur();medianBlur();bilateralFilter()),并依次显示滤波后的图像
源码:
import sys
import cv2 as cv
import numpy as np
# Global Variables
DELAY_CAPTION = 1500
DELAY_BLUR = 100
MAX_KERNEL_LENGTH = 31
src = None
dst = None
window_name = 'Smoothing Demo'
def main(argv):
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
# Load the source image
imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
global src
src = cv.imread(cv.samples.findFile(imageName))
if src is None:
print ('Error opening image')
print ('Usage: smoothing.py [image_name -- default ../data/lena.jpg] \n')
return -1
if display_caption('Original Image') != 0:
return 0
global dst
dst = np.copy(src)
if display_dst(DELAY_CAPTION) != 0:
return 0
# Applying Homogeneous blur
if display_caption('Homogeneous Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.blur(src, (i, i))
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Gaussian blur
if display_caption('Gaussian Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.GaussianBlur(src, (i, i), 0)
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Median blur
if display_caption('Median Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.medianBlur(src, i)
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Bilateral Filter
if display_caption('Bilateral Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.bilateralFilter(src, i, i * 2, i / 2)
if display_dst(DELAY_BLUR) != 0:
return 0
# Done
display_caption('Done!')
return 0
def display_caption(caption):
global dst
dst = np.zeros(src.shape, src.dtype)
rows, cols, _ch = src.shape
cv.putText(dst, caption,
(int(cols / 4), int(rows / 2)),
cv.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))
return display_dst(DELAY_CAPTION)
def display_dst(delay):
cv.imshow(window_name, dst)
c = cv.waitKey(delay)
if c >= 0 : return -1
return 0
if __name__ == "__main__":
main(sys.argv[1:])
直接运行多次报错,记录如下:
报错1:AttributeError: module 'cv2' has no attribute 'namedWindow'
解决1:当前默认环境中没有安装这个属性,是因为没有激活tensorflow环境(通过anaconda激活,或者用cmd激活)
报错2:AttributeError: module 'cv2.cv2' has no attribute 'samples
解决2:python与opencv-python版本不匹配。我的python是3.7.7,opencv-python是3.4.2.16,之后我卸载重装了最新版本opencv-contrib-python 4.6.0.66。opencv-python也要卸载重装(不注明版本时,默认最新版本)。注意要激活tensorflow环境,在tensorflow环境下安装。
安装过程如下:
卸载安装过程中报错:
原因是权限不够,需要管理员权限(用管理员身份打开cmd):
报错3:error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\utils\samples.cpp:64: error: (-2:Unspecified error) OpenCV samples: Can't find required data file: -f in function 'cv::samples::findFile'
如下图所示:
解决3:将代码行src = cv.imread(cv.samples.findFile(imageName))
改为src=cv2.imread('newlena.jpg',cv2.IMREAD_COLOR)”
,引入import cv2
问题4:可以运行了,但运行结果是找不到图像,如下所示:
解决4:用cmd运行:在源码文件夹路径下打开命令提示符,从命令提示符中进入tensorflow环境运行代码,可以成功运行。如下图所示:
运行结果如下图所示:
最终代码:
import sys
import cv2 as cv
import cv2
import numpy as np
# Global Variables
DELAY_CAPTION = 1500
DELAY_BLUR = 100
MAX_KERNEL_LENGTH = 31
src = None
dst = None
window_name = 'Smoothing Demo'
def main(argv):
cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
# Load the source image
imageName = argv[0] if len(argv) > 0 else 'newlena.jpg'
global src
src=cv2.imread('newlena.jpg',cv2.IMREAD_COLOR)
#src = cv.imread(cv.samples.findFile(imageName))
if src is None:
print ('Error opening image')
print ('Usage: smoothing.py [image_name -- default ../data/newlena.jpg] \n')
return -1
if display_caption('Original Image') != 0:
return 0
global dst
dst = np.copy(src)
if display_dst(DELAY_CAPTION) != 0:
return 0
# Applying Homogeneous blur
if display_caption('Homogeneous Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.blur(src, (i, i))
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Gaussian blur
if display_caption('Gaussian Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.GaussianBlur(src, (i, i), 0)
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Median blur
if display_caption('Median Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.medianBlur(src, i)
if display_dst(DELAY_BLUR) != 0:
return 0
# Applying Bilateral Filter
if display_caption('Bilateral Blur') != 0:
return 0
for i in range(1, MAX_KERNEL_LENGTH, 2):
dst = cv.bilateralFilter(src, i, i * 2, i / 2)
if display_dst(DELAY_BLUR) != 0:
return 0
# Done
display_caption('Done!')
return 0
def display_caption(caption):
global dst
dst = np.zeros(src.shape, src.dtype)
rows, cols, _ch = src.shape
cv.putText(dst, caption,
(int(cols / 4), int(rows / 2)),
cv.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))
return display_dst(DELAY_CAPTION)
def display_dst(delay):
cv.imshow(window_name, dst)
c = cv.waitKey(delay)
if c >= 0 : return -1
return 0
if __name__ == "__main__":
main(sys.argv[1:])