error bitwise operation
Hello, I am a engineer student in Universidad Católica de Temuco (Chile) and currently I'm working on my thesis. I'm making a prototype in real time chroma key on linux (ubuntu 11|.04). So this prototype is being developed in python with opencv. As can see I could apply a mask with a range of green colors (light and dark green) - this showing in the window called res resulting from the mask denied (leaving in black green hues ). the problem is the background image shown in negative any suggestions? Regards
#green objects detected and an image replacement
#also eliminates noise
import cv2
import numpy as np
from PIL import Image
#Initialize camera
capture = cv2.VideoCapture(0)
image1= cv2.imread('playa2.jpg')
capture.set(3,600)
capture.set(4,400)
while(1):
#Capture image from camera and I make RGB -> HSV
_, image = capture.read()
_,camera = capture.read()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#I APPLY THE COLOR RANGE TO BE DETECTED. IN THIS CASE LIGTH GREEN AND DARK GREEN
DARK_GREEN = np.array([49,50,50], dtype=np.uint8)
LIGTH_GREEN = np.array([80, 255, 211], dtype=np.uint8)
#I CREATE A MASK WITH THE COLOR RANGE I WANT
mask = cv2.inRange(hsv, verde_bajos, verde_altos)
# kernel CREATION 4x4 TO ELIMINATE NOISE
kernel = np.ones((3,3),np.uint8)
#kernel1 = np.ones((3,3), np.uint8)
#I apply erosion
mask = cv2.erode(mask,kernel,iterations = 2)
#I apply dilation
mask = cv2.dilate(mask,kernel,iterations = 2)
mask_inv = cv2.bitwise_not(mask)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel, iterations = 1)
mask= cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel, iterations = 1)
res = cv2.bitwise_not(image1,image,mask=mask)
#show binary mask
cv2.imshow('mask', mask)
#show default camera
cv2.imshow('Camara', camara)
#show result
cv2.imshow('res', res)
tecla = cv2.waitKey(5) & 0xFF
if tecla == 27:
break
cv2.destroyAllWindows()
and the real color of the picture
imagen1 is in BGR and imagen is in HSV. I think you need to have the same color format before applying the bitwise operation.
just a hint, - if you need help from the internet, use (broken) english for names/comments, not your native language.
@hadoofi, - not really. admittedly the waves look pretty much like hsv, but :
so it comes straight from a disk image (hsv is the HSV image)
@berak: I agree with you, I even couldn't continue reading the code! You're asking for help, you should make it easier for others to help you!
ok my apologies, I edit the post and I hope you can understand. the problem is in the line res = cv2.bitwise_not(image1,image,mask=mask)
I think it hits the negative image1 (beach)