OpenCV with Python - 在网络摄像头源头上放置一个帽子图像

时间:2015-05-07 19:28:46

标签: python opencv computer-vision webcam

我正在尝试在网络摄像头源头上放置一个帽子的png图像。我试图检测一张脸并将图像放在它上面。到目前为止这是我的代码 -

import cv2
import numpy as np

face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')

imghat = cv2.imread('hat.png', -1)

print imghat is None

imghatGray = cv2.cvtColor(imghat, cv2.COLOR_BGR2GRAY)

ret, orig_mask = cv2.threshold(imghatGray, 0, 255, cv2.THRESH_BINARY)
orig_mask_inv = cv2.bitwise_not(orig_mask)

# Convert hat image to BGR
# and save the original image size (used later when re-sizing the image)
imghat = imghat[:,:,0:3]
origHatHeight, origHatWidth = imghat.shape[:2]

video_capture = cv2.VideoCapture(0)

while True:

    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(gray, 1.3, 5, flags=cv2.cv.CV_HAAR_SCALE_IMAGE)

    for (x, y, w, h) in faces:
        print "x : %d , y : %d, w: %d, h: %d " %(x,y,w,h)
        cv2.rectangle(frame, (x,y), (x+w, y+h), (255,0,0), 2)
        cv2.rectangle(frame, (x-15,y-h), (x+w+15, y), (255,255,0), 2)

        print w
        print h
        hatWidth = w
        hatHeight = hatWidth * origHatHeight / origHatWidth

        roi_gray = gray[y-hatHeight:y, x-15:x+w+15]
        roi_color = frame[y-hatHeight:y, x-15:x+w+15]

        # Center the hat
        x1 = x - 15
        y1 = y - h
        x2 = x + w +15
        y2 = y

        cv2.rectangle(frame, (x1,y1), (x2, y2), (0,255,0), 2)

        # Check for clipping
        if x1 < 0:
            x1 = 0
        if y1 < 0:
            y1 = 0
        if x2 > w:
            x2 = w
        if y2 > h:
            y2 = h

        # Re-calculate the width and height of the hat image
        hatWidth = x2 - x1
        hatHeight = y2 - y1

        # Re-size the original image and the masks to the hat sizes
        # calcualted above
        hat = cv2.resize(imghat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
        mask = cv2.resize(orig_mask, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
        mask_inv = cv2.resize(orig_mask_inv, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

        # take ROI for hat from background equal to size of hat image
        roi = roi_color[y1:y2, x1:x2]

        # roi_bg contains the original image only where the hat is not
        # in the region that is the size of the hat.
        roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

        # roi_fg contains the image of the hat only where the hat is
        roi_fg = cv2.bitwise_and(hat,hat,mask = mask)

        # join the roi_bg and roi_fg
        dst = cv2.add(roi_bg,roi_fg)

        # place the joined image, saved to dst back over the original image
        roi_color[y1:y2, x1:x2] = dst

        break


    # Display the resulting frame
        cv2.imshow('Video', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

video_capture.release()
cv2.destroyAllWindows()

我收到此错误 - OpenCV错误:每次运行时,setSize 中的断言失败(s&gt; = 0)。网络摄像头突然开始和关闭。错误发生在 -

            hat = cv2.resize(imghat, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
            mask = cv2.resize(orig_mask, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)
            mask_inv = cv2.resize(orig_mask_inv, (hatWidth,hatHeight), interpolation = cv2.INTER_AREA)

hatWidth和hatHeight的值为负数。但我在坐标分配中找不到错误。是因为该计划的投资回报率?

2 个答案:

答案 0 :(得分:0)

在代码 x1 = x - 15 x2 = x + w +15 中,您有:

    # Check for clipping
    if x1 < 0:
        x1 = 0
    if x2 > w:
        x2 = w

其中x1和x2似乎是帽子的水平边界。

然后,几行后,没有x1和x2改变值你有

x + w + 15

此代码将始终修改x2,因为x2的定义大于w,事实上它是hatWidth = x2 - x1 。这可能不是你想要的。

您可以将hatWidth设置为

w

此时,由于上面的“剪辑”代码,x2始终为cv2.resize( ... )

因此,如果x2小于x1,这将使得hatWidth为负,这会导致您在x - 15中看到的问题。

x2何时小于x1?那么,x2是w而x1是var React = require('react-native'); var { AppRegistry, StyleSheet, TouchableHighlight, Image, Text, Component, AlertIOS, View, } = React; var Video = require('react-native-video'); class Mogul extends Component { constructor() { this.playBroadchurch = this.playBroadchurch.bind(this) this.state = {showBroadchurch: false}; } render() { var videoDisplay; if (this.state.showBroadchurch) { // Switch between showing video and placeholder image videoDisplay = <Video source={{uri: "broadchurch"}} // Can be a URL or a local file. rate={1} // 0 is paused, 1 is normal. volume={1} // 0 is muted, 1 is normal. muted={false} // Mutes the audio entirely. // Pauses playback entirely. resizeMode={'contain'} // Fill the whole screen at aspect ratio. repeat={false} // Repeat forever. onLoad={this.setDuration} // Callback when video loads onProgress={this.setTime} // Callback every ~250ms with currentTime onEnd={this.onEnd} // Callback when playback finishes style={styles.video} />; } else { videoDisplay = <Image style={{ height:150, width: 150 }} source={{uri: 'http://www.covermesongs.com/wp-content/uploads/2014/05/NotoriousBIG.jpg'}} />; } return ( <View style={styles.container}> <TouchableHighlight onPress={this.playBroadchurch}> {videoDisplay} </TouchableHighlight> <TouchableHighlight onPress={this.showAlert}> <Image style={{ height:150, width: 150 }} source={{uri: 'http://www.covermesongs.com/wp-content/uploads/2014/05/NotoriousBIG.jpg'}} /> </TouchableHighlight> </View> ); } playBroadchurch() { this.setState({showBroadchurch: true}); // Update state to show video } showAlert() { AlertIOS.alert('Notorious BIG', 'It was all a DREAM', [ {text: 'Yep', onPress: () => console.log('Yep Pressed!')}, {text: 'Nope', onPress: () => console.log('Nope Pressed!')}, ] ) } }; 所以每当w < (x-15),其为w + 15&lt; x即每当检测到的面部的位置比面部的宽度加上15时更正;这似乎是经常发生的事情,实际上不应该是一个问题。

我怀疑你的剪辑代码应该检查x2是否大于图像的宽度,而不是面宽。

剪辑y2时遇到类似的问题

答案 1 :(得分:0)

我遇到了同样的错误。通过绘制坐标值找到解决方案。一个这样的观点是在图像的维度之外。所以得到了错误。我稍后修复它并设置边界条件,以便ROI尺寸不超过图像尺寸。不管怎么说,还是要谢谢你。