def to_binary_adaptive(img):
sigma = 1.0
m = max(img.shape)
if m > 2000:
block_size = 80
elif m > 1500:
block_size = 50
elif m > 1000:
block_size = 35
else:
block_size = 20
bimg = gaussian_filter(img, sigma=sigma)
bimg = threshold_adaptive(bimg, block_size, offset=2 / 255)
bimg = np.array(bimg, 'uint8') * 255
return bimg
python类threshold_adaptive()的实例源码
def binarisation(src_image):
if len(src_image.shape) == 3:
image = (src_image.sum(axis=2) / 3).astype('ubyte')
else:
image = src_image
thresh = threshold_otsu(image)
binary = (image > thresh).astype('ubyte')
binary1 = 1 - binary
im = 255 - np.multiply(255 - image, binary1)
block_size = 35
binary = threshold_adaptive(image, block_size, offset=20)
binary = binary.astype('ubyte')
return binary
def binarisation(src_image):
if len(src_image.shape) == 3:
image = (src_image.sum(axis=2) / 3).astype('ubyte')
else:
image = src_image
thresh = threshold_otsu(image)
binary = (image > thresh).astype('ubyte')
binary1 = 1 - binary
im = 255 - np.multiply(255 - image, binary1)
block_size = 35
binary = threshold_adaptive(image, block_size, offset=20)
binary = binary.astype('ubyte')
return binary
# 100
def binarisation(src_image):
if len(src_image.shape) == 3:
image = (src_image.sum(axis=2) / 3).astype('ubyte')
else:
image = src_image
thresh = threshold_otsu(image)
binary = (image > thresh).astype('ubyte')
binary1 = 1 - binary
im = 255 - np.multiply(255 - image, binary1)
block_size = 35
binary = threshold_adaptive(im, block_size, offset=20)
binary = binary.astype('ubyte')
return binary
def binarisation1(src_image):
if len(src_image.shape) == 3:
image = (src_image.sum(axis=2) / 3).astype('ubyte')
else:
image = src_image
block_size = 35
binary = threshold_adaptive(image, block_size, offset=20)
binary = binary.astype('ubyte')
return binary
def cropped_image(image, contours, bw=True, show=False):
ratio = image.shape[0] / float(scale_factor)
warped = four_point_transform(image, contours.reshape(4, 2) * ratio)
if bw:
# convert the warped image to grayscale, then threshold it to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 251, offset = 10)
warped = warped.astype("uint8") * 255
if show: #this is for debugging puposes
cv2.imshow("Payload", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
return warped
def cropped_image(image, contours, bw=True, show=False):
ratio = image.shape[0] / float(scale_factor)
warped = four_point_transform(image, contours.reshape(4, 2) * ratio)
if bw:
# convert the warped image to grayscale, then threshold it to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = threshold_adaptive(warped, 251, offset = 10)
warped = warped.astype("uint8") * 255
if show: #this is for debugging puposes
cv2.imshow("Payload", warped)
cv2.waitKey(0)
cv2.destroyAllWindows()
return warped
def iter_blob_extremes(image, n=5):
original_shape = image.shape[::-1]
if max(original_shape) < 2000:
size = (500, 500)
y_scale = original_shape[0] / 500
x_scale = original_shape[1] / 500
else:
size = (1000, 1000)
y_scale = original_shape[0] / 1000
x_scale = original_shape[1] / 1000
img = resize(image, size)
bimg = gaussian_filter(img, sigma=1.0)
bimg = threshold_adaptive(bimg, 20, offset=2/255)
bimg = -bimg
bimg = ndi.binary_fill_holes(bimg)
label_image = label(bimg, background=False)
label_image += 1
regions = regionprops(label_image)
regions.sort(key=attrgetter('area'), reverse=True)
iter_n = 0
for region in regions:
try:
iter_n += 1
if iter_n > n:
break
# Skip small images
if region.area < int(np.prod(size) * 0.05):
continue
coords = get_contours(add_border(label_image == region.label,
size=label_image.shape,
border_size=1,
background_value=False))[0]
coords = np.fliplr(coords)
top_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x)))[0]
top_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], 0]))[0]
bottom_left = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [0, img.shape[0]]))[0]
bottom_right = sorted(coords, key=lambda x: np.linalg.norm(np.array(x) - [img.shape[1], img.shape[0]]))[0]
scaled_extremes = [(int(x[0] * y_scale), int(x[1]*x_scale)) for x in (top_left, top_right, bottom_left, bottom_right)]
yield scaled_extremes
except Exception:
pass
raise SudokuExtractError("No suitable blob could be found.")