我在使用OpenCV(cv2)时遇到以下问题。我不知道如何解决。 我正在使用VS 2019,最新的OpenCV和最新的Python环境
错误代码: OpenCV(4.1.1)C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window.cpp:627:错误:(-2:未指定错误)该功能未实现。在Windows,GTK + 2.x或Cocoa支持下重建库。如果您使用的是Ubuntu或Debian,请安装libgtk2.0-dev和pkg-config,然后重新运行cmake或在功能'cvShowImage'中配置脚本
'''
Copyright 2017 by Satya Mallick ( Big Vision LLC )
http://www.learnopencv.com
'''
import cv2
import numpy as np
def fillHoles(mask):
'''
This hole filling algorithm is decribed in this post
https://www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/
'''
maskFloodfill = mask.copy()
h,w = maskFloodfill.shape[:2]
maskTemp = np.zeros((h+2,w+2),np.uint8)
cv2.floodFill(maskFloodfill,maskTemp,(0,0),255)
mask2 = cv2.bitwise_not(maskFloodfill)
return mask2 | mask
if __name__ == '__main__' :
# Read image
img = cv2.imread("red_eyes.jpg",cv2.IMREAD_COLOR)
# Output image
imgOut = img.copy()
# Load HAAR cascade
eyesCascade = cv2.Cascadeclassifier("haarcascade_eye.xml")
# Detect eyes
eyes = eyesCascade.detectMultiScale(img,scaleFactor=1.3,minNeighbors=4,minSize=(100,100))
# For every detected eye
for (x,y,w,h) in eyes:
# Extract eye from the image
eye = img[y:y+h,x:x+w]
# Split eye image into 3 channels
b = eye[:,:,0]
g = eye[:,1]
r = eye[:,2]
# Add the green and blue channels.
bg = cv2.add(b,g)
# Simple red eye detector.
mask = (r > 150) & (r > bg)
# Convert the mask to uint8 format.
mask = mask.astype(np.uint8)*255
# Clean mask -- 1) File holes 2) Dilate (expand) mask.
mask = fillHoles(mask)
mask = cv2.dilate(mask,None,anchor=(-1,-1),iterations=3,borderType=1,borderValue=1)
# Calculate the mean channel by averaging
# the green and blue channels
mean = bg / 2
mask = mask.astype(np.bool)[:,np.newaxis]
mean = mean[:,np.newaxis]
# Copy the eye from the original image.
eyeOut = eye.copy()
# Copy the mean image to the output image.
#np.copyto(eyeOut,mean,where=mask)
eyeOut = np.where(mask,eyeOut)
# Copy the fixed eye to the output image.
imgOut[y:y+h,x:x+w,:] = eyeOut
# Display Result
cv2.imshow('Red Eyes',img)
cv2.imshow('Red Eyes Removed',imgOut)
cv2.waitKey(0)