0

i'm new to openCv, i just wrote a little program in python for a real time camera capture. I wanted to change the frame size in px, from the standard 640x480 to 1280x720. My Logitech camera has a HD resolution (720p) so i know that that's not a camera problem...

This is the code:

import numpy as np
from time import sleep
import cv2
import time

cap = cv2.VideoCapture(0)

cap.set(3,1280)
cap.set(4,720)

sleep(2)


while(True):

    ret, s = cap.read()

    cv2.imshow('frame', s)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break


cap.release()
cv2.destroyAllWindows()

but when i run it, i get this error:

================== RESTART: C:/Users/Mattia/Desktop/fgs.py ==================

    Traceback (most recent call last):
      File "C:/Users/Mattia/Desktop/fgs.py", line 18, in <module>
        cv2.imshow('frame', s)
    cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

I'm using python 3.6 and openCv 4.0

Can someone help me? Thanks!

5
  • Could it be that ret is False but that you're still trying to display s as an image in that case? Have you tried continueing if ret is False? Commented Feb 4, 2019 at 18:45
  • Yes, i tried, any way the code works if i remove cap.set(3,1280) cap.set(4,720). Thanks for answering btw. Commented Feb 4, 2019 at 19:42
  • @MattiaPeiretti so... do you still have a problem? Commented Feb 4, 2019 at 20:26
  • Yes, i do. Because i need to change the resolution, 480p(default) is too low.. So i need to find a way to make it 720p Commented Feb 4, 2019 at 20:31
  • Try installing v4l2-utils sudo apt-get install v4l-utils and run v4l2-ctl --list-formats-ext to check supported resolutions. Commented Feb 5, 2019 at 9:40

1 Answer 1

1

I found the problem, openCV 4.0.0.21 has a bug, it just displays the error without any reason, I just changed to an older openCV version, and it just worked!

To uninstall openCV (With Pip, on Windows 10, using CMD Administrator window):

pip uninstall opencv-contrib-python

The command to install an older version:

pip install opencv-python==(Version you wanna install, i installed 3.1.0.5)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.