python - 找出哪个图像具有最多的黑色

标签 python opencv colors

首先我将一个视频帧分成三个不同的帧。然后我将该图像转换为灰度图像。转换后,我需要找出哪个帧比其他两个帧具有最多的黑色。我不知道如何找出图像中存在多少黑色。

看了网上的一些方法,但是不知道怎么实现?

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

我需要知道如何检测图像中的黑色并将它们与其他两幅图像相关联并找出哪一幅图像的黑色最多

最佳答案

如果像素的相应值为零,则认为黑白图像中的 Hi 为黑色。 因此,假设您使用开放式 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] 将是图像中黑色像素的数量,这个数字越多,图像的黑色就越多。

关于python - 找出哪个图像具有最多的黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705602/

相关文章:

python - 卡尔曼滤波器预测 t+2 时间步长

python - 如何以数字方式订购 Django QuerySet 字符串属性?

c++ - OpenCV imencode pgm 没有编码正确的格式

xcode - 将 Xcode 色彩空间与 Digital Color Meter 相匹配

javascript - 为图像上的特定区域着色

python - 如何更改 ARMAX.predict 的 maxlag?

python - 如何使用 pandas 或 numpy 计算真阳性的出现?

iphone - 结构分配上的 EXC_BAD_ACCESS——不知道为什么会这样

python - 你能详细描述一下下面的python opencv函数吗?

html - 是否可以在文本颜色上设置渐变?