def test_farid_vertical(): """Farid on a vertical edge should be a vertical line.""" i, j = np.mgrid[-5:6, -5:6] image = (j >= 0).astype(float) result = filters.farid(image) * np.sqrt(2) assert (np.all(result[j == 0] == result[j == 0][0])) assert_allclose(result[np.abs(j) > 2], 0, atol=1e-10)
def test_farid_horizontal(): """Farid on a horizontal edge should be a horizontal line.""" i, j = np.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.farid(image) * np.sqrt(2) # Check if result match transform direction assert (np.all(result[i == 0] == result[i == 0][0])) assert_allclose(result[np.abs(i) > 2], 0, atol=1e-10)
def test_farid_horizontal(): """Farid on a horizontal edge should be a horizontal line.""" i, j = np.mgrid[-5:6, -5:6] image = (i >= 0).astype(float) result = filters.farid(image) * np.sqrt(2) # Fudge the eroded points i[np.abs(j) == 5] = 10000 assert (np.all(result[i == 0] == result[i == 0][0])) assert_allclose(result[np.abs(i) > 2], 0, atol=1e-10)
def test_farid_mask(): """Farid on a masked array should be zero.""" result = filters.farid(np.random.uniform(size=(10, 10)), np.zeros((10, 10), dtype=bool)) assert (np.all(result == 0))
def test_farid_zeros(): """Farid on an array of all zeros.""" result = filters.farid(np.zeros((10, 10)), np.ones((10, 10), dtype=bool)) assert (np.all(result == 0))
def test_farid_mask(dtype): """Farid on a masked array should be zero.""" result = filters.farid(np.random.uniform(size=(10, 10)).astype(dtype), mask=np.zeros((10, 10), dtype=bool)) assert result.dtype == _supported_float_type(dtype) assert (np.all(result == 0))
def image_pre_process(image): # Apply otsu filter to find the thresholds print('starting with multi otsu filter') thresholds = threshold_multiotsu(image, classes=THO_N + 1) print('thresholds are', thresholds) regions = np.digitize(image, bins=thresholds) otsu_regions_img = img_as_ubyte(regions) # Binary threshold methods # https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html ret, img_binary = cv2.threshold(image, thresholds[0], 255, cv2.THRESH_BINARY) # Dilation and erosion # https://www.youtube.com/watch?v=WQK_oOWW5Zo kernel = np.ones((3, 3), np.uint8) erosion = cv2.erode(img_binary, kernel, iterations=1) dilation = cv2.dilate(img_binary, kernel, iterations=1) opening = cv2.morphologyEx(img_binary, cv2.MORPH_OPEN, kernel) # Label image # https://www.youtube.com/watch?v=u3nG5_EjfM0&list=PLZsOBAyNTZwbIjGnolFydAN33gyyGP7lT&index=119 #label_image = measure.label(otsu_regions_img, connectivity=img_binary.ndim) label_image = measure.label(opening, connectivity=img_binary.ndim) image_label_overlay = label2rgb(label_image, image=otsu_regions_img) # Get different regions from the labeled image # https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_label.html i = 0 regions = [] bbx = [] for region in measure.regionprops(label_image): i += 1 print('i is', i) print('region is', region.image) regions.append(region.image) bbx.append(region.bbox) figure = plt.figure() plt.imshow(region.image) plt.title('region ' + np.str(i)) plt.savefig( 'G:\\My Drive\\Project\\IntraOral Scanner Registration\\Results\\Segmentation test\\region ' + np.str(i)) plt.close() print('shape of regions is', np.shape(regions)) for bbx_ind in bbx: #figure = plt.figure() #plt.imshow(regions[-3]) #plt.title('label image check') print('region bounding box is', bbx_ind) # Edge detection # https://www.youtube.com/watch?v=Oy4duAOGdWQ&list=PLZsOBAyNTZwbIjGnolFydAN33gyyGP7lT&index=105 robert_image = roberts(img_binary) sobel_image = sobel(img_binary) scharr_image = scharr(img_binary) prewitt_image = prewitt(img_binary) farid_image = farid(img_binary) print('detected edge is', sobel_image) return otsu_regions_img, image_label_overlay, sobel_image
kernel = np.ones((3, 3), np.uint8) erosion = cv2.erode(img_binary, kernel, iterations=1) dilation = cv2.dilate(img_binary, kernel, iterations=1) # Label image # https://www.youtube.com/watch?v=u3nG5_EjfM0&list=PLZsOBAyNTZwbIjGnolFydAN33gyyGP7lT&index=119 label_image = measure.label(output, connectivity=img_binary.ndim) image_label_overlay = label2rgb(label_image, image=output) # Edge detection # https://www.youtube.com/watch?v=Oy4duAOGdWQ&list=PLZsOBAyNTZwbIjGnolFydAN33gyyGP7lT&index=105 robert_image = roberts(img_binary) sobel_image = sobel(img_binary) scharr_image = scharr(img_binary) prewitt_image = prewitt(img_binary) farid_image = farid(img_binary) #img_mutso = multiOtsu(3, img) plt.figure() plt.imshow(img_gray, cmap='gray') plt.title("Grayscale original image") plt.figure() plt.imshow(output, cmap='gray') plt.title("Ostu image") plt.figure() plt.imshow(img_segment, cmap='gray') plt.title("Segment image") plt.figure()
from skimage import io, filters, feature import matplotlib.pyplot as plt from skimage.color import rgb2gray import cv2 import numpy as np img = cv2.imread('images/sandstone.tif', 0) #Edge detection from skimage.filters import roberts, sobel, scharr, prewitt, farid roberts_img = roberts(img) sobel_img = sobel(img) scharr_img = scharr(img) prewitt_img = prewitt(img) farid_img = farid(img) cv2.imshow("Roberts", roberts_img) cv2.imshow("Sobel", sobel_img) cv2.imshow("Scharr", scharr_img) cv2.imshow("Prewitt", prewitt_img) cv2.imshow("Farid", farid_img) cv2.waitKey(0) cv2.destroyAllWindows()