在OpenCV中选择具有最高强度的像素

任何人都可以帮助我找出在opencv中灰度图像位置最高的1%(或称前100像素)最亮的像素。 因为cvMinMaxLoc()只给出最明亮的像素位置。

任何帮助是极大的赞赏。


请尝试使用cvThreshold。


这是一个简单而不够/愚蠢的做法:

for i=1:100
  get brightest pixel using cvMinMaxLoc 
  store location
  set it to a value of zero
end

如果你不介意效率这应该工作。

您还应该检查cvInRangeS以查找定义低阈值和高阈值的其他类似值的像素。


您需要从直方图中计算亮度阈值。 然后你遍历像素以获得足够亮的位置以满足阈值。 下面的程序会将阈值应用于图像并显示结果用于演示目的:

#!/usr/bin/env python3

import sys
import cv2
import matplotlib.pyplot as plt

if __name__ == '__main__':
    if len(sys.argv) != 2 or any(s in sys.argv for s in ['-h', '--help', '-?']):
        print('usage: {} <img>'.format(sys.argv[0]))
        exit()
    img = cv2.imread(sys.argv[1], cv2.IMREAD_GRAYSCALE)
    hi_percentage = 0.01 # we want we the hi_percentage brightest pixels
    # * histogram
    hist = cv2.calcHist([img], [0], None, [256], [0, 256]).flatten()
    # * find brightness threshold
    # here: highest thresh for including at least hi_percentage image pixels,
    #       maybe you want to modify it for lowest threshold with for including
    #       at most hi_percentage pixels
    total_count = img.shape[0] * img.shape[1]  # height * width
    target_count = hi_percentage * total_count # bright pixels we look for
    summed = 0
    for i in range(255, 0, -1):
        summed += int(hist[i])
        if target_count <= summed:
            hi_thresh = i
            break
    else:
        hi_thresh = 0
    # * apply threshold & display result for demonstration purposes:
    filtered_img = cv2.threshold(img, hi_thresh, 0, cv2.THRESH_TOZERO)[1]
    plt.subplot(121)
    plt.imshow(img, cmap='gray')
    plt.subplot(122)
    plt.imshow(filtered_img, cmap='gray')
    plt.axis('off')
    plt.tight_layout()
    plt.show()
链接地址: http://www.djcxy.com/p/2577.html

上一篇: Selecting the pixels with highest intensity in OpenCV

下一篇: git: better way for git revert without additional reverted commit