| import torch | |
| def rgb2hsv_torch(rgb: torch.Tensor) -> torch.Tensor: | |
| cmax, cmax_idx = torch.max(rgb, dim=1, keepdim=True) | |
| cmin = torch.min(rgb, dim=1, keepdim=True)[0] | |
| delta = cmax - cmin | |
| hsv_h = torch.empty_like(rgb[:, 0:1, :, :]) | |
| cmax_idx[delta == 0] = 3 | |
| hsv_h[cmax_idx == 0] = (((rgb[:, 1:2] - rgb[:, 2:3]) / delta) % 6)[cmax_idx == 0] | |
| hsv_h[cmax_idx == 1] = (((rgb[:, 2:3] - rgb[:, 0:1]) / delta) + 2)[cmax_idx == 1] | |
| hsv_h[cmax_idx == 2] = (((rgb[:, 0:1] - rgb[:, 1:2]) / delta) + 4)[cmax_idx == 2] | |
| hsv_h[cmax_idx == 3] = 0. | |
| hsv_h /= 6. | |
| hsv_s = torch.where(cmax == 0, torch.tensor(0.).type_as(rgb), delta / cmax) | |
| hsv_v = cmax | |
| return torch.cat([hsv_h, hsv_s, hsv_v], dim=1) | |
| def hsv2rgb_torch(hsv: torch.Tensor) -> torch.Tensor: | |
| hsv_h, hsv_s, hsv_l = hsv[:, 0:1], hsv[:, 1:2], hsv[:, 2:3] | |
| _c = hsv_l * hsv_s | |
| _x = _c * (- torch.abs(hsv_h * 6. % 2. - 1) + 1.) | |
| _m = hsv_l - _c | |
| _o = torch.zeros_like(_c) | |
| idx = (hsv_h * 6.).type(torch.uint8) | |
| idx = (idx % 6).expand(-1, 3, -1, -1) | |
| rgb = torch.empty_like(hsv) | |
| rgb[idx == 0] = torch.cat([_c, _x, _o], dim=1)[idx == 0] | |
| rgb[idx == 1] = torch.cat([_x, _c, _o], dim=1)[idx == 1] | |
| rgb[idx == 2] = torch.cat([_o, _c, _x], dim=1)[idx == 2] | |
| rgb[idx == 3] = torch.cat([_o, _x, _c], dim=1)[idx == 3] | |
| rgb[idx == 4] = torch.cat([_x, _o, _c], dim=1)[idx == 4] | |
| rgb[idx == 5] = torch.cat([_c, _o, _x], dim=1)[idx == 5] | |
| rgb += _m | |
| return rgb |