Church Lenard
Posts: 34
Joined: Wed May 01, 2019 4:45 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 1:22 am

gordon77 wrote:
Thu Nov 21, 2019 9:20 am
You could change your monitor to 640x480.

Assuming you are using the GUI..

Choose Preferences from Menu
Choose Screen Configuration
Choose Screens
Choose HDMI-1
Choose Resolution
Choose 640x480
Click on the Green Tick
Choose OK
I did but the image is tiny its like maybe 30% of the screen id upload a pic but it keeps saying the file is too large

gordon77
Posts: 4375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 8:22 am

Did you include

cv2.namedWindow(winName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

Code: Select all

#!/usr/bin/env python3
import cv2
import os

invert_mask = 0

# mouse action
def point(event, x, y, flags, param):
    global invert_mask
    if event == cv2.EVENT_LBUTTONDOWN:
        if invert_mask == 0:
            invert_mask = 1
        else:
            invert_mask = 0
      
# setup pi camera 
if os.path.exists('/dev/video0') == False:
  path = 'sudo modprobe bcm2835-v4l2'
  os.system (path)
  time.sleep(1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
  
# start video
cam = cv2.VideoCapture(0)

# show video size
print (cam.get(3),cam.get(4))

# set video size
cam.set(3,640)
cam.set(4,480)

# setup window
winName = "Scope"
cv2.namedWindow(winName,cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

# setup mouse callback               
cv2.setMouseCallback('Scope',point)

# read foreground image
img2 = cv2.imread('Reticle2.jpg')
img3 = (255-img2)
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

while True:
    # take video frame
    ok, img = cam.read()
    # add the 2 images
    if invert_mask == 0:
        added_image = cv2.bitwise_and(img,img,mask = mask)
    else:
        added_image = cv2.addWeighted(img,0.99,img3,0.7,0.2)
    # show image
    cv2.imshow( winName,added_image)
    # wait
    key = cv2.waitKey(10)
    # press Esc to EXIT
    if key == 27:
       cv2.destroyWindow(winName)
       break

Church Lenard
Posts: 34
Joined: Wed May 01, 2019 4:45 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 2:44 pm

gordon77 wrote:
Fri Nov 22, 2019 8:22 am
Did you include

cv2.namedWindow(winName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

Code: Select all

#!/usr/bin/env python3
import cv2
import os

invert_mask = 0

# mouse action
def point(event, x, y, flags, param):
    global invert_mask
    if event == cv2.EVENT_LBUTTONDOWN:
        if invert_mask == 0:
            invert_mask = 1
        else:
            invert_mask = 0
      
# setup pi camera 
if os.path.exists('/dev/video0') == False:
  path = 'sudo modprobe bcm2835-v4l2'
  os.system (path)
  time.sleep(1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
  
# start video
cam = cv2.VideoCapture(0)

# show video size
print (cam.get(3),cam.get(4))

# set video size
cam.set(3,640)
cam.set(4,480)

# setup window
winName = "Scope"
cv2.namedWindow(winName,cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

# setup mouse callback               
cv2.setMouseCallback('Scope',point)

# read foreground image
img2 = cv2.imread('Reticle2.jpg')
img3 = (255-img2)
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

while True:
    # take video frame
    ok, img = cam.read()
    # add the 2 images
    if invert_mask == 0:
        added_image = cv2.bitwise_and(img,img,mask = mask)
    else:
        added_image = cv2.addWeighted(img,0.99,img3,0.7,0.2)
    # show image
    cv2.imshow( winName,added_image)
    # wait
    key = cv2.waitKey(10)
    # press Esc to EXIT
    if key == 27:
       cv2.destroyWindow(winName)
       break
Yes and i even tested it with the code you had and its still small it is fullscreen and you can see the image but the image is small

gordon77
Posts: 4375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 2:57 pm

Well I'm baffled as it works on my Pi4.

upload your code as a final check

Church Lenard
Posts: 34
Joined: Wed May 01, 2019 4:45 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 3:11 pm

gordon77 wrote:
Fri Nov 22, 2019 2:57 pm
Well I'm baffled as it works on my Pi4.

upload your code as a final check

Code: Select all

#!/usr/bin/env python3
import cv2
import os

invert_mask = 0

# mouse action
def point(event, x, y, flags, param):
    global invert_mask
    if event == cv2.EVENT_LBUTTONDOWN:
        if invert_mask == 0:
            invert_mask = 1
        else:
            invert_mask = 0
      
# setup pi camera 
if os.path.exists('/dev/video0') == False:
  path = 'sudo modprobe bcm2835-v4l2'
  os.system (path)
  time.sleep(1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
  
# start video
cam = cv2.VideoCapture(0)

# show video size
print (cam.get(3),cam.get(4))

# set video size
cam.set(3,640)
cam.set(4,480)

# setup window
winName = "Scope"
cv2.namedWindow(winName,cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

# setup mouse callback               
cv2.setMouseCallback('Scope',point)

# read foreground image
img2 = cv2.imread('Reticle2.jpg')
img3 = (255-img2)
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

while True:
    # take video frame
    ok, img = cam.read()
    # add the 2 images
    if invert_mask == 0:
        added_image = cv2.bitwise_and(img,img,mask = mask)
    else:
        added_image = cv2.addWeighted(img,0.99,img3,0.7,0.2)
    # show image
    cv2.imshow( winName,added_image)
    # wait
    key = cv2.waitKey(10)
    # press Esc to EXIT
    if key == 27:
       cv2.destroyWindow(winName)
       break
im on a pi 3b and using a 3.5 inch waveshare hdmi screen

gordon77
Posts: 4375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 3:41 pm

Well that's different. What resolution is the screen?

is it hdmi ?

Church Lenard
Posts: 34
Joined: Wed May 01, 2019 4:45 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 3:58 pm

gordon77 wrote:
Fri Nov 22, 2019 3:41 pm
Well that's different. What resolution is the screen?

is it hdmi ?
Its hdmi at 640x480

gordon77
Posts: 4375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Image overlay on live video feed

Fri Nov 22, 2019 4:09 pm

Can you provide a screenshot using cv2.namedWindow(winName), ie NO FULLSCREEN, does the image fill the screen ?

gordon77
Posts: 4375
Joined: Sun Aug 05, 2012 3:12 pm

Re: Image overlay on live video feed

Sun Nov 24, 2019 10:57 am

This version will capture a screenshot when you right click the mouse.

It will also work upto 40fps, (I still haven't worked out why the original didn't !), and allow you to set camera parameters.

Note some camera parameters are interactive, for example if you set high fps you can't set AEB.

Code: Select all

import cv2
import time
import os

invert_mask = 0

# mouse action
def point(event, x, y, flags, param):
    global invert_mask
    # Left mouse click to change mask
    if event == cv2.EVENT_LBUTTONDOWN:
        if invert_mask == 0:
            invert_mask = 1
        else:
            invert_mask = 0
    # Right mouse click to save screenshot
    if event == cv2.EVENT_RBUTTONDOWN:
        path = 'scrot screenshot.jpg'
        os.system (path)

cam = cv2.VideoCapture(0)

# set video size
cam.set(3,640)
cam.set(4,480)

if os.path.exists('/dev/video0') == False:
  path = 'sudo modprobe bcm2835-v4l2'
  os.system (path)
  time.sleep(1)

FPS          = 40
AutoExp      = 0   # 0 = auto ON , 1 = auto OFF
AEB          = 12  # range 0 - 24
ExpTime      = 1000 
brightness   = 60
ISO          = 0   # 0 = auto, 1 = 100, 2 = 200 etc
contrast     = 50
scene        = 8   # 0 = normal, 8 = night, 11 = sports
saturation   = 25
red_balance  = 1188
blue_balance = 1228

winName = "Scope"
cv2.namedWindow(winName)#
#cv2.namedWindow(winName,cv2.WINDOW_GUI_NORMAL)
#cv2.setWindowProperty(winName,cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

# setup mouse callback               
cv2.setMouseCallback('Scope',point)

# setup camera

path = 'v4l2-ctl --set-ctrl=brightness=' + str(brightness)
os.system (path)
path = 'v4l2-ctl --set-ctrl=contrast=' + str(contrast)
os.system (path)
if AutoExp == 0:
  path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
  os.system (path)
  path = 'v4l2-ctl --set-ctrl=auto_exposure_bias=' + str(AEB)
  os.system (path)
else:
  path = 'v4l2-ctl --set-ctrl=auto_exposure=1'
  os.system (path)
  if AutoExp == 1:
    path = 'v4l2-ctl --set-ctrl=exposure_time_absolute=' + str(ExpTime)
    os.system (path)
if ISO == 0:
   path = 'v4l2-ctl --set-ctrl=iso_sensitivity_auto=1'
   os.system (path)
else:
   path = 'v4l2-ctl --set-ctrl=iso_sensitivity_auto=0'
   os.system (path)
path = 'v4l2-ctl --set-ctrl=iso_sensitivity=' + str(ISO)
os.system (path)
path = 'v4l2-ctl --set-ctrl=auto_exposure_bias=' + str(AEB)
os.system (path)
path = 'v4l2-ctl -p ' + str(FPS)
os.system (path)
path = 'v4l2-ctl --set-ctrl=scene_mode=' + str(scene)
os.system (path)
path = "v4l2-ctl -c white_balance_auto_preset=0"
os.system (path)
path = "v4l2-ctl -c red_balance=" + str(red_balance) 
os.system (path)
path = "v4l2-ctl -c blue_balance=" + str(blue_balance) 
os.system (path)
path = "v4l2-ctl -c sharpness=50" 
os.system (path)
path = "v4l2-ctl -c saturation=" + str(saturation) 
os.system (path)

# read foreground image
img2 = cv2.imread('Reticle2.jpg')
img3 = (255-img2)
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)

while True:
        
     ok, img = cam.read()
     if invert_mask == 0:
        added_image = cv2.bitwise_and(img,img,mask = mask)
     else:
        added_image = cv2.addWeighted(img,0.99,img3,0.7,0.2)
     cv2.imshow( winName,added_image)
     key = cv2.waitKey(10)
     if key == 27:
        cv2.destroyWindow(winName)
        break
 
print ("Goodbye")

Church Lenard
Posts: 34
Joined: Wed May 01, 2019 4:45 pm

Re: Image overlay on live video feed

Sun Nov 24, 2019 9:10 pm

gordon77 wrote:
Fri Nov 22, 2019 4:09 pm
Can you provide a screenshot using cv2.namedWindow(winName), ie NO FULLSCREEN, does the image fill the screen ?
I would provide a screenshot but it wont take the images that im taking but the image does seem adequate for the windowed size

Return to “Camera board”