首先将视频帧分为三个不同的帧。然后,我将该图像转换为灰度图像。转换后,我需要找到哪一帧比其他两帧的黑色最多。我不知道如何查找图像中存在多少黑色。
我在互联网上已经经历过一些方法,但是我不知道如何实现它们?
import cv2
import sys
sys.path.append('/usr/local/lib/python2.7/site-packages')
import cv2
import numpy as np
import time
import copy
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
output = frame.copy()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.imshow('frame',frame)
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
gray =255-gray
ret, thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)
output, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
output = cv2.drawContours(output, contours, -1,(0,0,255),3)
cv2.imshow('frame',output)
height, width = output.shape[:2]
print (output.shape)
start_row, start_col = int(0), int(0)
# Let's get the ending pixel coordinates (bottom right of cropped top)
end_row, end_col = int(height), int(width*.3)
cropped_top = output[start_row:end_row , start_col:end_col]
print (start_row, end_row)
print (start_col, end_col)
cv2.imshow("Cropped Topp", cropped_top)
# Let's get the starting pixel coordiantes (top left of cropped bottom)
start_row, start_col = int(0), int(width*.3)
# Let's get the ending pixel coordinates (bottom right of cropped bottom)
end_row, end_col = int(height), int(width)
cropped_bot = output[start_row:end_row , start_col:end_col]
print (start_row, end_row )
print (start_col, end_col)
##cv2.imshow("Cropped Bot", cropped_bot)
##cv2.waitKey(0)
##cv2.destroyAllWindows()
start_row, start_col = int(0), int(0)
# Let's get the ending pixel coordinates (bottom right of cropped top)
end_row, end_col = int(height), int(width*.3)
cropped_top = cropped_bot[start_row:end_row , start_col:end_col]
print (start_row, end_row)
print (start_col, end_col)
cv2.imshow("Cropped Top", cropped_top)
# Let's get the starting pixel coordiantes (top left of cropped bottom)
start_row, start_col = int(0), int(width*.3)
# Let's get the ending pixel coordinates (bottom right of cropped bottom)
end_row, end_col = int(height), int(width)
cropped_mid = cropped_bot[start_row:end_row , start_col:end_col]
print (start_row, end_row )
print (start_col, end_col)
cv2.imshow("Cropped Bot", cropped_mid)
## #cv2.imwrite('plsal1.png',h)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我需要知道如何检测图像中的黑色并将其与其他两幅图像相关联,并找出哪一幅具有最大的黑色
答案 0 :(得分:0)
import cv2
import numpy as np
img = cv2.imread('pathOfImg',0) #read img as b/w as an numpy array
unique, counts = np.unique(img, return_counts=True)
mapColorCounts = dict(zip(unique, counts))
现在mapColorCounts[0]
将是图像中的黑色像素数,此数字越多,图像就越具有黑色。