Ask Your Question

How to detect a perfect black image?

asked 2016-12-31 08:01:12 -0500

Harikishore gravatar image

Hi, This is my first post in this forum. I've written a color detection program for detecting Magenta color(only). But now I want to print a text when the magenta color is not present.Which means I want to detect a perfect black image for that.How can I do that?

import cv2 import numpy as np cap = cv2.VideoCapture(0) while(1):

Take each frame

_, frame =

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of blue color in HSV
lower_blue = np.array([140,100, 140], dtype=np.uint8)
upper_blue = np.array([150,255,255], dtype=np.uint8)

# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)

# Bitwise-AND mask and original image
res = cv2.bitwise_and(frame,frame, mask= mask)

k = cv2.waitKey(5) & 0xFF
if k == 27:


edit retag flag offensive close merge delete


I don't know python. perfect black image means that image minimum is 0. see minMaxLoc

LBerger gravatar imageLBerger ( 2016-12-31 09:41:23 -0500 )edit

I think you mean image maximum

sturkmen gravatar imagesturkmen ( 2016-12-31 09:54:40 -0500 )edit

thanks @sturkmen I don't know python. perfect black image means that image maximum is 0. see minMaxLoc

LBerger gravatar imageLBerger ( 2016-12-31 09:57:00 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-12-31 11:16:13 -0500

Tetragramm gravatar image
edit flag offensive delete link more


I'm getting an error error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\stat.cpp:1297: error: (-215) cn == 1 in function cv::countNonZero

Harikishore gravatar imageHarikishore ( 2016-12-31 22:04:35 -0500 )edit

That's annoying, I may fix that. For now, cv2.countNonZero(image.reshape(1))

That makes the image one channel, and since you're looking for totally black, that doesn't break anything.

Tetragramm gravatar imageTetragramm ( 2017-01-01 00:27:07 -0500 )edit

Question Tools

1 follower


Asked: 2016-12-31 08:01:12 -0500

Seen: 6,635 times

Last updated: Dec 31 '16