Selecting the highest intensity pixels in OpenCV

Can someone help me find out the top 1% (or say the 100 best pixels) bright pixels with their gray image location in opencv. because cvMinMaxLoc () gives only the brightest pixel layout.

Any help is greatly appreciated.

+7
c ++ image-processing opencv pixels
source share
4 answers

Try cvThreshold instead.

+1
source share

this is a simple but unreasonable / stupid way to do this:

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

If you don't mind efficiency, this should work.

you should also check cvInRangeS to find other pixels with the same values ​​defining low and high threshold values.

+1
source share

You need to calculate the brightness threshold on the histogram. Then you iterate over the pixels to get those positions that are bright enough to fit the threshold. The program below applies a threshold to an image and displays the result for demonstration purposes:

 #!/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() 
+1
source share

Well, the most logical way is to iterate over the whole picture, then get the values ​​of max and min pixels. Then select a threshold that will give you the desired percentage (1% in your case). After that, try again and save the i and j coordinates for each pixel above the specified threshold. Thus, you will iterate over the matrix only twice instead of 100 (or 1% of the pixel time) and select the brightest one and delete it.

OpenCV mats are multidimensional arrays. The gray image is a two-dimensional array with values ​​from 0 to 255. You can iterate through a matrix like this. for(int i=0;i < mat.height();i++) for(int j=0;j < mat.width();j++) mat[i][j];

0
source share

All Articles