import numpy as np import cv2 def my_roberts(slika): kernalX = np.array([[1,0],[0,-1]]) kernalY = np.array([[0,1],[-1,0]]) slika_robov = np.zeros(slika.shape) height, width = slika.shape for y in range(0, height-2): for x in range(0, width-2): subImg = slika[y:y+2, x:x+2] kernalResultX = np.sum(kernalX * subImg) kernalResultY = np.sum(kernalY * subImg) kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY ) slika_robov[y, x] = kernalResult slika_robov = np.uint8(slika_robov) return slika_robov def my_prewitt(slika): kernalX = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]) kernalY = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]) slika_robov = np.zeros(slika.shape) height, width = slika.shape for y in range(1, height-2): for x in range(1, width-2): subImg = slika[y-1:y+2, x-1:x+2] kernalResultX = np.sum(kernalX * subImg) kernalResultY = np.sum(kernalY * subImg) kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY ) slika_robov[y, x] = np.absolute(kernalResult) slika_robov = np.uint8(slika_robov) return slika_robov def my_sobel(slika): kernalX = np.array([[-1, -3, -1], [0, 0, 0], [1, 3, 1]]) kernalY = np.array([[-1, 0, 1], [-3, 0, 3], [-1, 0, 1]]) slika_robov = np.zeros(slika.shape) height, width = slika.shape for y in range(1, height-2): for x in range(1, width-2): subImg = slika[y-1:y+2, x-1:x+2] kernalResultX = np.sum(kernalX * subImg) kernalResultY = np.sum(kernalY * subImg) kernalResult = np.absolute( kernalResultX ) + np.absolute( kernalResultY ) slika_robov[y, x] = np.absolute(kernalResult) slika_robov = np.uint8(slika_robov) return slika_robov def canny(slika): slika_robov = cv2.Canny(slika, 0, 0) return slika_robov def spremeni_kontrast(slika, alfa, beta): return cv2.convertScaleAbs(slika, alpha=alfa, beta=beta) img = cv2.imread('lenna.png', 0) cv2.imshow('lenna', img) # #imgRoberts = my_roberts(img) #cv2.imshow('lenna_roberts', imgRoberts) # #imgPrewitt = my_prewitt(img) #cv2.imshow('lenna_prewitt', imgPrewitt) # #imgSobel = my_sobel(img) #cv2.imshow('lenna_sobel', imgSobel) img2 = spremeni_kontrast(img, 1.5, 0) cv2.imshow('lenna2i', img2) cv2.imshow('lenna2', my_sobel(img2)) img3 = spremeni_kontrast(img, 0.5, 1) cv2.imshow('lenna3i', img3) cv2.imshow('lenna3', my_sobel(img3)) windowName = 'lenna_canny' val1 = 0 val2 = 0 def on_change1(val): global val1 val1 = val imgCanny = cv2.Canny(img, val1, val2) cv2.imshow(windowName, imgCanny) def on_change2(val): global val2 val2 = val imgCanny = cv2.Canny(img, val1, val2) cv2.imshow(windowName, imgCanny) imgCanny = canny(img) cv2.imshow(windowName, imgCanny) cv2.createTrackbar('threshold1', windowName, 0, 255, on_change1) cv2.createTrackbar('threshold2', windowName, 0, 255, on_change2) cv2.waitKey(0) cv2.destroyAllWindows()