Home>

The grayscale array obtained by dividing the BGR image (* 1 derived code in the supplementary information) and the mode value of the image in which the image (* 2 see supplementary information) is saved and reloaded are different.
I want to know the cause and solution.

The problem i am having
The grayscale array (* 1) that divides the BGR image and the mode of the image that saved the image (* 2) and reloaded it are different.
Corresponding source code
import cv2
import numpy as np
from scipy import stats
#Mode of brightness 1 or higher (obtained from array)
RGB = cv2.split (img_green_masked)
Blue = RGB [0]
Brow = np.array (Blue) .flatten ()
Bmode = stats.mode (Brow [Brow>0]) [0] [0]
# ----------- Brow in Bmode does not change even in Blue.
# ----------- img_green_masked see supplementary information
#Mode of brightness 1 or higher (obtained from Blue imwite image)
Brow_im = np.array (Brow_imwrite_picture) .flatten ()
Bmode = stats.mode (Brow_im [Brow_im>0]) [0] [0]
Supplementary information (FW/tool version, etc.)

(* 1 Grayscale array code)
RGB = cv2.split (img_green_masked)
Blue = RGB [0]
Brow = np.array (Blue) .flatten ()

(* 2 Blue saved image)

(* 3 img_green_masked)

  • Answer # 1

    The jpeg format is lossy, so if you save and read it, it may be different from the original. Therefore, naive (sensitive to small changes in the image) numerical statistics, especially the mode, are likely to be different from the original.

    On the other hand, png and tiff formats are reversible, so this does not happen.

    I have tried it with the following code, so please check it.

    import cv2
    import numpy as np
    from scipy import stats
    img_green_masked = cv2.imread ('img_green_masked.jpeg')
    RGB = cv2.split (img_green_masked)
    Blue = RGB [0]
    cv2.imwrite ('sample.jpeg', Blue)
    cv2.imwrite ('sample.png', Blue)
    cv2.imwrite ('sample.tiff', Blue)
    Brow = np.array (Blue) .flatten ()
    Bmode = stats.mode (Brow [Brow>0]) [0] [0]
    print ("origin's bmode =", Bmode)
    Brow_imwrite_picture = cv2.imread ('sample.jpeg')
    Brow_im = np.array (Brow_imwrite_picture) .flatten ()
    Bmode = stats.mode (Brow_im [Brow_im>0]) [0] [0]
    print ("jpeg's bmode =", Bmode)
    Brow_imwrite_picture = cv2.imread ('sample.png')
    Brow_im = np.array (Brow_imwrite_picture) .flatten ()
    Bmode = stats.mode (Brow_im [Brow_im>0]) [0] [0]
    print ("png's bmode =", Bmode)
    Brow_imwrite_picture = cv2.imread ('sample.tiff')
    Brow_im = np.array (Brow_imwrite_picture) .flatten ()
    Bmode = stats.mode (Brow_im [Brow_im>0]) [0] [0]
    print ("tiff's bmode =", Bmode)

    Execution result

    origin's bmode = 2
    jpeg's bmode = 1
    png's bmode = 2
    tiff's bmode = 2

Related articles