def segmentation(_image, typeOfFruit):
#_denoisedImage = rank.median(_image, disk(2));
#_elevationMap = sobel(_denoisedImage);
#print(_image);
_elevationMap = sobel(_image);
#print(_image);
_marker = np.zeros_like(_image);
if (typeOfFruit == 'Counting'):
_marker[_image < 1998] = 1;
_marker[_image > 61541] = 2;
elif (typeOfFruit == 'Temperature'):
#print("Printing Image");
#print(_image < 10);
_marker[_image < 30] = 1;
#print(_marker);
_marker[_image > 150] = 2;
#_marker = rank.gradient(_denoisedImage, disk(5)) < 10;
#_marker = ndi.label(_marker)[0];
#_elevationMap = rank.gradient(_denoisedImage, disk(2));
_segmentation = watershed(_elevationMap, _marker);
#print(_segmentation);
return _segmentation;
python类sobel()的实例源码
def segmentation(_image, typeOfFruit):
#_denoisedImage = rank.median(_image, disk(2));
#_elevationMap = sobel(_denoisedImage);
#print(_image);
_elevationMap = sobel(_image);
#print(_image);
_marker = np.zeros_like(_image);
if (typeOfFruit == 'Counting'):
_marker[_image < 1998] = 1;
_marker[_image > 61541] = 2;
elif (typeOfFruit == 'Temperature'):
#print("Printing Image");
#print(_image < 10);
_marker[_image < 30] = 1;
#print(_marker);
_marker[_image > 150] = 2;
#_marker = rank.gradient(_denoisedImage, disk(5)) < 10;
#_marker = ndi.label(_marker)[0];
#_elevationMap = rank.gradient(_denoisedImage, disk(2));
_segmentation = watershed(_elevationMap, _marker);
#print(_segmentation);
return _segmentation;
def edges_each(image):
return filters.sobel(image)
def edges_hsv(image):
return filters.sobel(image)
def edges_hsv_uint(image):
with expected_warnings(['precision loss']):
return img_as_uint(filters.sobel(image))
def test_gray_scale_image():
# We don't need to test both `hsv_value` and `each_channel` since
# `adapt_rgb` is handling gray-scale inputs.
assert_allclose(edges_each(GRAY_IMAGE), filters.sobel(GRAY_IMAGE))
def test_each_channel():
filtered = edges_each(COLOR_IMAGE)
for i, channel in enumerate(np.rollaxis(filtered, axis=-1)):
expected = img_as_float(filters.sobel(COLOR_IMAGE[:, :, i]))
assert_allclose(channel, expected)
def test_hsv_value_with_non_float_output():
# Since `rgb2hsv` returns a float image and the result of the filtered
# result is inserted into the HSV image, we want to make sure there isn't
# a dtype mismatch.
filtered = edges_hsv_uint(COLOR_IMAGE)
filtered_value = color.rgb2hsv(filtered)[:, :, 2]
value = color.rgb2hsv(COLOR_IMAGE)[:, :, 2]
# Reduce tolerance because dtype conversion.
assert_allclose(filtered_value, filters.sobel(value), rtol=1e-5, atol=1e-5)
def edgeDetector(_imageFile):
#_edge = feature.canny((_imageFile/255.), sigma = 3);
_edge = filters.sobel(_imageFile);
return _edge
def edgeDetector(_imageFile):
#_edge = feature.canny((_imageFile/255.), sigma = 3);
_edge = filters.sobel(_imageFile);
return _edge
def seam_carve(img):
"""
Seam carve image
:param img: PIL image object
:return: PIL image object
"""
# Convert to skimage image
img_to_convert = img.copy()
img_to_convert = pil_to_skimage(img_to_convert)
# Energy Map, used to determine which pixels will be removed
eimg = filters.sobel(color.rgb2gray(img_to_convert))
# (height, width)
img_dimensions = img_to_convert.shape
# Squish width if width >= height, squish height if height > width
# Number of pixels to keep along the outer edges (5% of largest dimension)
# Number of seams to be removed, (1 to 10% of largest dimension)
if img_dimensions[1] >= img_dimensions[0]:
mode = "horizontal"
border = round(img_dimensions[1] * 0.05)
num_seams = random.randint(1, round(0.1*img_dimensions[1]))
else:
mode = "vertical"
border = round(img_dimensions[0] * 0.05)
num_seams = random.randint(1, round(0.1*img_dimensions[0]))
try:
img_to_convert = transform.seam_carve(img_to_convert, eimg, mode, num_seams, border)
except Exception as e:
print("Unable to seam_carve: " + str(e))
# Convert back to PIL image
img_to_convert = skimage_to_pil(img_to_convert)
return img_to_convert
def run(self, ips, snap, img, para = None):
edge = sobel(snap)
img[:] = 0
img[snap>para['thr2']] = 2
img[snap<para['thr1']] = 1
ips.lut = self.buflut
mark = watershed(edge, img, line=True)
if para['type'] == 'line':
img[mark==0] = ips.range[1]
elif para['type'] == 'up area':
img[mark!=1] = ips.range[1]
elif para['type'] == 'down area':
img[mark!=2] = ips.range[1]
prepare-images.py 文件源码
项目:kaggle-dstl-satellite-imagery-feature-detection
作者: alno
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def compute_filters(data):
filter_data = np.zeros((1, data.shape[1], data.shape[2]), dtype=np.float32)
filter_data[0] = sobel(np.clip(data[0] / 600.0, 0, 1)) + sobel(np.clip(data[1] / 600.0, 0, 1)) + sobel(np.clip(data[2] / 600.0, 0, 1))
return filter_data
def main():
"""Load image, apply sobel (to get x/y gradients), plot the results."""
img = data.camera()
sobel_y = np.array([
[-1, -2, -1],
[0, 0, 0],
[1, 2, 1]
])
sobel_x = np.rot90(sobel_y) # rotates counter-clockwise
# apply x/y sobel filter to get x/y gradients
img_sx = signal.correlate(img, sobel_x, mode="same")
img_sy = signal.correlate(img, sobel_y, mode="same")
# combine x/y gradients to gradient magnitude
# scikit-image's implementation divides by sqrt(2), not sure why
img_s = np.sqrt(img_sx**2 + img_sy**2) / np.sqrt(2)
# create binarized image
threshold = np.average(img_s)
img_s_bin = np.zeros(img_s.shape)
img_s_bin[img_s > threshold] = 1
# generate ground truth (scikit-image method)
ground_truth = skifilters.sobel(data.camera())
# plot
util.plot_images_grayscale(
[img, img_sx, img_sy, img_s, img_s_bin, ground_truth],
["Image", "Sobel (x)", "Sobel (y)", "Sobel (magnitude)",
"Sobel (magnitude, binarized)", "Sobel (Ground Truth)"]
)
def gridsize(original, segdst=SEGMENT_DISTANCE):
global im
# read the image from disk
im = original.copy()
add_image('Original')
# edge detection
im = sobel(im)
# blurring
im = filters.gaussian(im, sigma=5)
# thresholding: convert to binary rage
loc = threshold_local(im, 31)
im = im > loc
if (DEBUG): add_image('Threshold')
# detect straight lines longer than 150px
segs = probabilistic_hough_line(
im,
threshold=30,
line_length=250,
line_gap=7)
#segs = [seg for seg in segs if vertical(seg)]
# draw the segments
im[:] = 0 # set image to black
for seg in segs:
((x1, y1), (x2, y2)) = seg
rr,cc = draw.line(y1,x1,y2,x2)
im[rr, cc] = 1
if (DEBUG): add_image('Hough Lines')
hh, vv = process_segments(segs)
# draw the segments
im[:] = 0 # set image to black
num = 0
for yy in hh:
for yyy in yy:
(_,y),_ = yyy
rr,cc = draw.line(y,0,y,999)
im[rr, cc] = 1
num += 1
for xx in vv:
for xxx in xx:
(x,_),_ = xxx
rr,cc = draw.line(0,x,999,x)
im[rr, cc] = 1
num += 1
if (DEBUG):
add_image('Filtered Segs')
# finally save the result
displ()
return len(vv)-1, len(hh)-1