Ask Your Question
0

How to detect a perfect black image?

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

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 = cap.read()

# 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)

cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.imshow('res',res)
k = cv2.waitKey(5) & 0xFF
if k == 27:
    break

cv2.destroyAllWindows()

edit retag flag offensive close merge delete

Comments

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 -0600 )edit
1

I think you mean image maximum

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

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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

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

Tetragramm gravatar image
edit flag offensive delete link more

Comments

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 -0600 )edit
1

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 8,586 times

Last updated: Dec 31 '16