glenn-jocher commited on
Commit
08d3119
1 Parent(s): d68afed

Add histogram equalization fcn (#2049)

Browse files
Files changed (3) hide show
  1. Dockerfile +2 -2
  2. models/yolo.py +1 -1
  3. utils/datasets.py +10 -4
Dockerfile CHANGED
@@ -39,13 +39,13 @@ COPY . /usr/src/app
39
  # sudo docker kill $(sudo docker ps -q)
40
 
41
  # Kill all image-based
42
- # sudo docker kill $(sudo docker ps -a -q --filter ancestor=ultralytics/yolov5:latest)
43
 
44
  # Bash into running container
45
  # sudo docker exec -it 5a9b5863d93d bash
46
 
47
  # Bash into stopped container
48
- # id=5a9b5863d93d && sudo docker start $id && sudo docker exec -it $id bash
49
 
50
  # Send weights to GCP
51
  # python -c "from utils.general import *; strip_optimizer('runs/train/exp0_*/weights/best.pt', 'tmp.pt')" && gsutil cp tmp.pt gs://*.pt
 
39
  # sudo docker kill $(sudo docker ps -q)
40
 
41
  # Kill all image-based
42
+ # sudo docker kill $(sudo docker ps -qa --filter ancestor=ultralytics/yolov5:latest)
43
 
44
  # Bash into running container
45
  # sudo docker exec -it 5a9b5863d93d bash
46
 
47
  # Bash into stopped container
48
+ # id=$(sudo docker ps -qa) && sudo docker start $id && sudo docker exec -it $id bash
49
 
50
  # Send weights to GCP
51
  # python -c "from utils.general import *; strip_optimizer('runs/train/exp0_*/weights/best.pt', 'tmp.pt')" && gsutil cp tmp.pt gs://*.pt
models/yolo.py CHANGED
@@ -107,7 +107,7 @@ class Model(nn.Module):
107
  for si, fi in zip(s, f):
108
  xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
109
  yi = self.forward_once(xi)[0] # forward
110
- # cv2.imwrite('img%g.jpg' % s, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
111
  yi[..., :4] /= si # de-scale
112
  if fi == 2:
113
  yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud
 
107
  for si, fi in zip(s, f):
108
  xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
109
  yi = self.forward_once(xi)[0] # forward
110
+ # cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
111
  yi[..., :4] /= si # de-scale
112
  if fi == 2:
113
  yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud
utils/datasets.py CHANGED
@@ -631,10 +631,16 @@ def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
631
  img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
632
  cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
633
 
634
- # Histogram equalization
635
- # if random.random() < 0.2:
636
- # for i in range(3):
637
- # img[:, :, i] = cv2.equalizeHist(img[:, :, i])
 
 
 
 
 
 
638
 
639
 
640
  def load_mosaic(self, index):
 
631
  img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
632
  cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
633
 
634
+
635
+ def hist_equalize(img, clahe=True, bgr=False):
636
+ # Equalize histogram on BGR image 'img' with img.shape(n,m,3) and range 0-255
637
+ yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
638
+ if clahe:
639
+ c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
640
+ yuv[:, :, 0] = c.apply(yuv[:, :, 0])
641
+ else:
642
+ yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
643
+ return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
644
 
645
 
646
  def load_mosaic(self, index):