Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get 'mean' and 'std' of isic18? #64

Open
DumbGuy-AI opened this issue Jun 14, 2024 · 1 comment
Open

how to get 'mean' and 'std' of isic18? #64

DumbGuy-AI opened this issue Jun 14, 2024 · 1 comment

Comments

@DumbGuy-AI
Copy link

class myNormalize:
def init(self, data_name, train=True):
if data_name == 'isic18':
if train:
self.mean = 157.561
self.std = 26.706
else:
self.mean = 149.034
self.std = 32.022
I wonder to konw how to calculate maen and std with specific code.I need some help,plz.

@JCruan519
Copy link
Owner

JCruan519 commented Oct 22, 2024

import numpy as np
import cv2
import os
 
def calculate_means_vars(img_datasets_path, img_h=224, img_w=224):
    means, stdevs = [], []
    img_list = []
    imgs_path_list = os.listdir(img_datasets_path)
 
    len_ = len(imgs_path_list)
    i = 0
    for item in imgs_path_list:
        img = cv2.imread(os.path.join(imgs_path,item))
        img = cv2.resize(img,(img_w,img_h))
        img = img[:, :, :, np.newaxis]
        img_list.append(img)
        i += 1 

    imgs = np.concatenate(img_list, axis=3)
    imgs = imgs.astype(np.float32) / 255.
 
    for i in range(3):
        pixels = imgs[:, :, i, :].ravel()  # 拉成一行
        means.append(np.mean(pixels))
        stdevs.append(np.std(pixels))

    # BGR --> RGB , CV读取的需要转换,PIL读取的不用转换
    means.reverse()
    stdevs.reverse()
 
    print("normMean = {}".format(means))
    print("normStd = {}".format(stdevs))
    
    return means, stdevs

import os
from PIL import Image
from tqdm import tqdm
import cv2
import numpy as np
img_file_path = '/path/to/images'
images_list = os.listdir(img_file_path)
data_list = []
for i in images_list:
    data_list.append(img_file_path + i)

m_list, s_list = [], []
for img_filename in tqdm(images_list):
    img = cv2.imread(img_file_path + '/' + img_filename)
    m, s = cv2.meanStdDev(img)
    m_list.append(m.reshape((3,)))
    s_list.append(s.reshape((3,)))
m_array = np.array(m_list)
s_array = np.array(s_list)
m = m_array.mean(axis=0, keepdims=True)
s = s_array.mean(axis=0, keepdims=True)
print(m[0][::-1].mean())
print(s[0][::-1].mean())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants