Ask Your Question
0

error bitwise operation

asked 2014-08-18 15:41:56 -0600

updated 2014-08-20 02:28:45 -0600

berak gravatar image

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()

image description

and the real color of the picture image description

edit retag flag offensive close merge delete

Comments

1

imagen1 is in BGR and imagen is in HSV. I think you need to have the same color format before applying the bitwise operation.

hadoofi gravatar imagehadoofi ( 2014-08-18 16:04:55 -0600 )edit
2

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 :

imagen1= cv2.imread('playa2.jpg')

so it comes straight from a disk image (hsv is the HSV image)

berak gravatar imageberak ( 2014-08-18 16:09:37 -0600 )edit

@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!

hadoofi gravatar imagehadoofi ( 2014-08-18 19:30:31 -0600 )edit
1

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)

Victor Sandoval gravatar imageVictor Sandoval ( 2014-08-19 12:00:07 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2014-08-20 02:24:54 -0600

berak gravatar image

updated 2014-08-20 02:26:44 -0600

no idea why, but this does the trick:

res = cv2.bitwise_not(~image1,image,mask=mask)

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2014-08-18 15:41:56 -0600

Seen: 1,726 times

Last updated: Aug 20 '14