opencv / opencv/opencv-python

cap.read returns black image after adjusting focus

Open
#1,180 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.4k
Forks
1k
Avg merge
22h 17m
Merged PRs (30d)
3

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported Python reproducer, especially VideoCapture(0, cv.CAP_DSHOW), the CAP_PROP_FOCUS updates, and cap.read(). Reproduce the focus loop while inspecting ret and the returned frame after each setting; done requires determining why later reads are black despite ret being True and documenting a reproducible fix or confirmed backend limitation.

Written by the indexing model from the issue text.

Assessment

Tech stack
opencv, python
Domain
computer-vision
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.