首先,我将视频帧分为三个不同的帧。然后我将该图像转换为灰度图像。转换后,我需要找到哪一帧比其他两帧的黑色最多。我不知道如何找到图像中存在多少黑色。我已经在互联网上使用了一些方法,但我不知道如何实现它们?import cv2import syssys.path.append('/usr/local/lib/python2.7/site-packages')import cv2import numpy as npimport timeimport copycap = 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) 我需要知道如何检测图像中的黑色并将它们与其他两个图像相关联,并找出哪一个具有最多的黑色
1 回答
FFIVE
TA贡献1797条经验 获得超6个赞
如果像素的相应值为零,则认为黑白图像中的 Hi 为黑色。因此,假设您使用 open cv 和 numpy 数组进行处理,我们可以做这样的事情。
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]将是图像中的黑色像素数,这个数字越多,图像越黑。
添加回答
举报
0/150
提交
取消