1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| def scanImageWithWindowSizeAutoResize( image, width, height, return_direction=False, threshold=0.1, ): shape = image.shape assert len(shape) == 3 ih, iw, channels = shape targetWidth = max(width, math.floor(iw * height / ih)) targetHeight = max(height, math.floor(ih * width / iw)) resized = cv2.resize( image, (targetWidth, targetHeight), interpolation=cv2.INTER_CUBIC )
imageSeries = [] if targetWidth / targetHeight == width / height: imageSeries = [resized] direction = None elif targetWidth / targetHeight < width / height: direction = "vertical"
index = 0 while True: start, end = height * index, height * (index + 1) if start < targetHeight: if end > targetHeight: if 1 - (end - targetHeight) / targetHeight >= threshold: end = targetHeight start = targetHeight - height else: break
else: break cropped = resized[start:end, :, :] imageSeries.append(cropped) index += 1 else: direction = "horizontal" index = 0 while True: start, end = width * index, width * (index + 1) if start < targetWidth: if end > targetWidth: if 1 - (end - targetWidth) / targetWidth >= threshold: end = targetWidth start = targetWidth - width else: break
else: break cropped = resized[:, start:end, :] imageSeries.append(cropped) index += 1 if return_direction: return imageSeries, direction else: return imageSeries def resizeImageWithPadding( image, width, height, border_type: Literal["constant_black", "replicate"] = "constant_black", ): shape = image.shape assert len(shape) == 3 ih, iw, channels = shape targetWidth = min(width, math.floor(iw * height / ih)) targetHeight = min(height, math.floor(ih * width / iw)) resized = cv2.resize( image, (targetWidth, targetHeight), interpolation=cv2.INTER_CUBIC ) BLACK = [0] * channels top = max(0, math.floor((height - targetHeight) / 2)) bottom = max(0, height - targetHeight - top) left = max(0, math.floor((width - targetWidth) / 2)) right = max(0, width - targetWidth - left) if border_type == "constant_black": padded = cv2.copyMakeBorder( resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK ) elif border_type == "replicate": padded = cv2.copyMakeBorder( resized, top, bottom, left, right, cv2.BORDER_REPLICATE, value=BLACK ) else: raise Exception("unknown border_type: %s" % border_type) return padded
|