Skip to content

cap.read returns black image after adjusting focus #1180

@Typschke

Description

@Typschke

Hello,

I have this program to get the best focus for my image:

import cv2 as cv
import time

cap = cv.VideoCapture(0, cv.CAP_DSHOW)

cap.set(cv.CAP_PROP_FRAME_WIDTH, 3264)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, 2448)
cap.set(cv.CAP_PROP_BRIGHTNESS, -12)
cap.set(cv.CAP_PROP_CONTRAST, 50)
cap.set(cv.CAP_PROP_SHARPNESS, 100)
cap.set(cv.CAP_PROP_GAMMA, 100)
cap.set(cv.CAP_PROP_WHITE_BALANCE_BLUE_U, 6500)
cap.set(cv.CAP_PROP_BACKLIGHT, 0)
cap.set(cv.CAP_PROP_EXPOSURE, -5)
cap.set(cv.CAP_PROP_AUTOFOCUS, 0)
cap.set(cv.CAP_PROP_FOCUS, 240)

time.sleep(0.3)

focus_found = 0

for z in range(240, 280, 2):
 #Setting Focus
 cap.set(cv.CAP_PROP_FOCUS, z)
 time.sleep(0.3)

 #Read Data from Camera and convert to Grayscale
 ret, data = cap.read()
 gray = cv.cvtColor(orig_data, cv.COLOR_BGR2GRAY)

 ...
 #Code to determine the focus. Setting variable focus_found ether to 0 or 1
 ...

 #Break if focus  is found
 if(focus_found):
  break
  

This works now for 2-5 steps for "z", but then i only get a complete black image from cap.read() even though "ret" is returning True. Do you know what i'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions