Code: Select all
#!/usr/bin/env python3
import cv2
import os
import time
# 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)
# setup window
winName = "Scope"
cv2.namedWindow(winName)
# read foreground image
foreground = cv2.imread('target.jpg')
while True:
# take video frame
ok, img = cam.read()
# add the 2 images
added_image = cv2.addWeighted(img,0.9,foreground,0.4,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
Code: Select all
Traceback (most recent call last):
File "/home/pi/test.py", line 27, in <module>
added_image = cv2.addWeighted(img, 0.9, foreground, 0.4, 0.2)
cv2.error: Opencv(3.4.4) /home/pi/packaging/opencv-python/opencv/modules/core/src/arithm.cpp:660:
error: (-209: sizes of input arguments do not match) The operationis neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in functio 'arithm_op'
Code: Select all
File "/home/pi/test.pi", in <module>
print (img.shape, foreground.shape)
AtributeError: 'NoneType' object has no attribute 'shape'
Code: Select all
sudo pip3 install opencv-python
sudo apt-get install libcblas-dev
sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install libqt4-test
ok so i just ran this again with opencv and heres the error i get:gordon77 wrote: ↑Thu May 02, 2019 8:28 amTry this... You need to save the target image below.
Code: Select all
#!/usr/bin/env python3 import cv2 import os import time # 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) # setup window winName = "Scope" cv2.namedWindow(winName) # read foreground image foreground = cv2.imread('target.jpg') while True: # take video frame ok, img = cam.read() # add the 2 images added_image = cv2.addWeighted(img,0.9,foreground,0.4,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
Code: Select all
Traceback (most recent call last):
File "/home/pi/test.py", line 27, in <module>
added_image = cv2.addWeighted(img,0.9,foreground,0.4,0.2)
cv2.error: OpenCV(4.1.2) /home/pi/opencv-4.1.2/modules/core/src/arithm.cpp:663:
error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array'
(where arrays have the same sized and number of channels), nor 'array op scalar', nor 'scalar op array'
in function 'arithm_op'
Code: Select all
#!/usr/bin/env python3
import cv2
import os
import time
# 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)
# read foreground image
img2 = cv2.imread('Reticle2.jpg')
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
added_image = cv2.bitwise_and(img,img,mask = mask)
# show image
cv2.imshow( winName,added_image)
# wait
key = cv2.waitKey(10)
# press Esc to EXIT
if key == 27:
cv2.destroyWindow(winName)
break
Code: Select all
#!/usr/bin/env python3
import cv2
import os
import time
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)
# 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
Code: Select all
workon cv
./ovl.py
Code: Select all
sudo apt-get install python3-opencv
sudo apt-get install libhdf5-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libjasper-dev
sudo apt-get install libqtgui4
sudo apt-get install libqt4-test
For the video size code i getgordon77 wrote: ↑Sun Nov 17, 2019 9:17 amThis will show you the video size, and set it to 640 x 480...
Also a new mask.
and a version with black or white reticle with a click of the mouse...Code: Select all
#!/usr/bin/env python3 import cv2 import os import time # 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) # read foreground image img2 = cv2.imread('Reticle2.jpg') 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 added_image = cv2.bitwise_and(img,img,mask = mask) # show image cv2.imshow( winName,added_image) # wait key = cv2.waitKey(10) # press Esc to EXIT if key == 27: cv2.destroyWindow(winName) break
Code: Select all
#!/usr/bin/env python3 import cv2 import os import time 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) # 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
Code: Select all
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
error code (-215:Assertion Failed) in function cvtCOLOR
Code: Select all
img3 = (255-img2)
TypeError: unsupported operand types for -: 'int' and 'NoneType'