python - 如何在 Visual Studio 2019 中解决 OpenCV 的问题
问题描述
我对 OpenCV (cv2) 有以下问题。我不知道如何解决它。我正在使用 VS 2019,最新的 OpenCV 和最新的 Python 环境
错误代码:OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:627: error: (-2:Unspecified error) 函数未实现。使用 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, mean, 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)
解决方案
推荐阅读
- php - 从数组创建分页
- javascript - 如何导航和设置角度下拉列表中检查的值
- javascript - 如何在编辑时隐藏输入字段的中间部分?
- botframework - 通过机器人框架将气泡发送到团队会议中
- typescript - 在 TypeScript 中动态导入 SCSS
- php - 每次在 Laravel 上离开页面时调用一个方法
- javascript - 为什么某些 iPhone 设备编译 javascript new Date() 的方式不同?
- javascript - 由于反斜杠转义,无法使 JavaScript 和 Python 字符串相同
- javascript - 这是哪种字符串编码?
- javascript - 如何在输入验证工具提示消息中插入新行?当悬停它工作正常但我点击发送它没有