Home>

I want to find the mode of a pixel that has a pixel value of 1 or greater.

The problem i am having
The mode value should be 1, but in my code it's 22 ...
I will put an image (Brow) and a histogram in the supplementary information.
Corresponding source code
import cv2
import numpy as np
from scipy import stats
Bmode = stats.mode (Brow [Brow>0]) [0] [0]
print ("B mode:" + str (B mode))
Supplementary information (FW/tool version, etc.)


img_green_masked

Blue

  • Answer # 1

    Postscript)

    1. Save the photo below as "img_green_masked.jpeg".

    2. Save the following file (appropriate name) as .py in the same folder as the image and run it.

    import cv2
    import numpy as np
    from scipy import stats
    def calc_Bmode (img):
        if len (img.shape) == 3:
            print (img.shape, "->", end = "")
            BGR = cv2.split (img_col)
            img = BGR [0]
            print (img.shape)
        else: else:
            print (img.shape)
        Brow = np.array (img) .flatten ()
        Bmode = stats.mode (Brow [Brow>0]) [0] [0]
        print ("Bmode", Bmode)
    img_col = cv2.imread ("./img_green_masked.jpeg")
    BGR = cv2.split (img_col)
    Blue = BGR [0]
    print ("--- Save once ---")
    cv2.imwrite ("./blue.jpeg", Blue)
    img_blue_loaded = cv2.imread ("./blue.jpeg")
    calc_Bmode (img_blue_loaded)
    cv2.imshow ("img_blue_loaded", cv2.resize (img_blue_loaded, None, fx = 0.25, fy = 0.25))
    cv2.waitKey (0)
    print ("--- Color separation method ---")
    calc_Bmode (Blue)
    cv2.imshow ("Blue", cv2.resize (Blue, None, fx = 0.25, fy = 0.25))
    cv2.waitKey (0)

    3. Can you see the "identification characters" in the displayed image?
    = (You can rule out the possibility of opening another file by mistake, right?

    4. Are you in the same Bmode with either method?


    It seems to be a little different from the original file, but the value is different, but it was done.
    Fixed 1 or more.
    I don't think it's because of Numpy or Scipy that it's 22.
    (Difference in image specification?)


    import cv2
    import numpy as np
    from scipy import stats
    img = cv2.cvtColor (cv2.imread ("./img.png"), cv2.COLOR_BGR2GRAY)
    print ("1 or more unconditional Numpy")
    arr, count = np.unique (img, return_counts = True)
    idx = np.argmax (count)
    print ("Maximum index [RAW]:", arr [idx])
    print ("Count: [RAW]", count [idx])
    # print (count)
    print ("Numpy with 1 or more conditions")
    arr, count = arr [1:], count [1:]
    idx = np.argmax (count)
    print ("Maximum index:", arr [idx])
    print ("Count:", count [idx])
    print ("Scipy with 1 or more conditions")
    Bmode = stats.mode (img [img>0]) [0] [0]
    print (Bmode)


  • Answer # 2

    When I tried using the image that fourteenlength gave me, "B mode: 4" was displayed.
    import numpy as np
    import cv2
    from scipy import stats
    Brow = cv2.cvtColor (cv2.imread ("./img.png"), cv2.COLOR_BGR2GRAY)
    Bmode = stats.mode (Brow [Brow>0]) [0] [0]
    print ("B mode:" + str (B mode))

    After checking, if I excluded 0, 4 was still the maximum number
    histogram, _ = np.histogram (Brow.flatten (), bins = np.arange (np.max (Brow) +2))
    print (histogram)

  • Answer # 3

    It's not an answer but a verification result.
    I tried it with a simple image, but I think there is no problem with the processing.
    From reading the original imageBrowIs there any problem in the process until it is set in the variable?

    import cv2
    import numpy as np
    from scipy import stats
    from PIL import Image, ImageDraw
    #Test image
    img = Image.new ('RGB', (500,200), (0,0,0))
    draw = ImageDraw.Draw (img)
    draw.rectangle ((50,50,150,150), fill = (200,200,200))
    draw.rectangle ((200,50,400,150), fill = (100,100,100)) # This is bigger
    img.save ('ret.png')
    img = np.array (img, dtype = np.uint8)
    Brow = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
    print (stats.mode (Brow [Brow>0]))
    #ModeResult (mode = array ([100], dtype = uint8), count = array ([20301]))
    Bmode = stats.mode (Brow [Brow>0]) [0] [0]
    print ("B mode:" + str (B mode)) # B mode: 100