|
| 1 | +import 'package:dart_opencv_sdk/src/filter/grayscale.dart'; |
| 2 | +import 'package:image/image.dart'; |
| 3 | + |
| 4 | +import '../core/functions.dart'; |
| 5 | +import '../smoothing/guassian.dart'; |
| 6 | +import '../filter/filter.dart'; |
| 7 | +import 'dart:math'; |
| 8 | + |
| 9 | +class CannyEdgeFilter implements ImageFilter { |
| 10 | + final CoreFunctions coreFunctions; |
| 11 | + final double lowThreshold; |
| 12 | + final double highThreshold; |
| 13 | + final int kernelSize; |
| 14 | + final double sigma; |
| 15 | + |
| 16 | + CannyEdgeFilter({ |
| 17 | + this.lowThreshold = 20, |
| 18 | + this.highThreshold = 50, |
| 19 | + this.kernelSize = 3, |
| 20 | + this.sigma = 1.4, |
| 21 | + }) : coreFunctions = CoreFunctions(); |
| 22 | + |
| 23 | + @override |
| 24 | + Image applyFilter(Image image) { |
| 25 | + final grayscaleImage = GrayscaleFilter().applyFilter(image); |
| 26 | + final blurredImage = |
| 27 | + GaussianBlurFilter(sigma: sigma, radius: kernelSize ~/ 2) |
| 28 | + .applyFilter(grayscaleImage); |
| 29 | + final gradients = calculateGradients(blurredImage); |
| 30 | + final suppressedImage = applyNonMaximaSuppression(blurredImage, gradients); |
| 31 | + final thresholdedImage = |
| 32 | + applyDoubleThreshold(suppressedImage, lowThreshold, highThreshold); |
| 33 | + final edgeImage = applyEdgeTrackingByHysteresis(thresholdedImage); |
| 34 | + |
| 35 | + return edgeImage; |
| 36 | + } |
| 37 | + |
| 38 | + List<List<double>> calculateGradients(Image image) { |
| 39 | + final sobelX = [ |
| 40 | + [-1, 0, 1], |
| 41 | + [-2, 0, 2], |
| 42 | + [-1, 0, 1], |
| 43 | + ]; |
| 44 | + |
| 45 | + final sobelY = [ |
| 46 | + [-1, -2, -1], |
| 47 | + [0, 0, 0], |
| 48 | + [1, 2, 1], |
| 49 | + ]; |
| 50 | + |
| 51 | + final gradients = [[]] as List<List<double>>; |
| 52 | + for (var x = 0; x < image.width; x++) { |
| 53 | + gradients[x] = [] as List<double>; |
| 54 | + for (var y = 0; y < image.height; y++) { |
| 55 | + gradients[x][y] = 0; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + for (var y = 1; y < image.height - 1; y++) { |
| 60 | + for (var x = 1; x < image.width - 1; x++) { |
| 61 | + var gx = 0.0; |
| 62 | + var gy = 0.0; |
| 63 | + |
| 64 | + for (var j = -1; j <= 1; j++) { |
| 65 | + for (var i = -1; i <= 1; i++) { |
| 66 | + final pixel = image.getPixelSafe(x + i, y + j); |
| 67 | + final luminance = pixel.r; |
| 68 | + gx += sobelX[i + 1][j + 1] * luminance; |
| 69 | + gy += sobelY[i + 1][j + 1] * luminance; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + gradients[x][y] = sqrt(gx * gx + gy * gy); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + return gradients; |
| 78 | + } |
| 79 | + |
| 80 | + Image applyNonMaximaSuppression(Image image, List<List<double>> gradients) { |
| 81 | + final suppressedImage = Image(width: image.width, height: image.height); |
| 82 | + |
| 83 | + for (var y = 1; y < image.height - 1; y++) { |
| 84 | + for (var x = 1; x < image.width - 1; x++) { |
| 85 | + final pixel = image.getPixel(x, y); |
| 86 | + final pixelValue = pixel.r; |
| 87 | + |
| 88 | + final angle = atan2(gradients[x][y], gradients[x - 1][y]); |
| 89 | + final direction = (angle * (180 / pi) + 180) % 180; |
| 90 | + |
| 91 | + final neighbor1 = coreFunctions.getPixelSafer(image, x - 1, y); |
| 92 | + final neighbor2 = coreFunctions.getPixelSafer(image, x + 1, y); |
| 93 | + |
| 94 | + final neighbor1Value = neighbor1.r; |
| 95 | + final neighbor2Value = neighbor2.r; |
| 96 | + |
| 97 | + final interpolatedValue = interpolate(pixelValue.toInt(), |
| 98 | + neighbor1Value.toInt(), neighbor2Value.toInt(), direction); |
| 99 | + suppressedImage.setPixel(x, y, |
| 100 | + getColor(interpolatedValue, interpolatedValue, interpolatedValue)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return suppressedImage; |
| 105 | + } |
| 106 | + |
| 107 | + int interpolate(int center, int neighbor1, int neighbor2, double direction) { |
| 108 | + final weight = direction % 45 / 45; |
| 109 | + final interpolatedValue = |
| 110 | + ((1 - weight) * neighbor1 + weight * neighbor2).round(); |
| 111 | + |
| 112 | + if (center >= interpolatedValue) { |
| 113 | + return center; |
| 114 | + } else { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + Image applyDoubleThreshold( |
| 120 | + Image image, double lowThreshold, double highThreshold) { |
| 121 | + final thresholdedImage = Image(width: image.width, height: image.height); |
| 122 | + |
| 123 | + for (var y = 0; y < image.height; y++) { |
| 124 | + for (var x = 0; x < image.width; x++) { |
| 125 | + final pixel = image.getPixel(x, y); |
| 126 | + final pixelValue = pixel.r; |
| 127 | + |
| 128 | + if (pixelValue >= highThreshold) { |
| 129 | + thresholdedImage.setPixel(x, y, getColor(255, 255, 255)); |
| 130 | + } else if (pixelValue >= lowThreshold) { |
| 131 | + thresholdedImage.setPixel(x, y, getColor(128, 128, 128)); |
| 132 | + } else { |
| 133 | + thresholdedImage.setPixel(x, y, getColor(0, 0, 0)); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return thresholdedImage; |
| 139 | + } |
| 140 | + |
| 141 | + Image applyEdgeTrackingByHysteresis(Image image) { |
| 142 | + final edgeImage = Image(width: image.width, height: image.height); |
| 143 | + |
| 144 | + for (var y = 1; y < image.height - 1; y++) { |
| 145 | + for (var x = 1; x < image.width - 1; x++) { |
| 146 | + final pixel = image.getPixel(x, y); |
| 147 | + final pixelValue = pixel.r; |
| 148 | + |
| 149 | + if (pixelValue == 128) { |
| 150 | + final neighbor1 = coreFunctions.getPixelSafer(image, x - 1, y); |
| 151 | + final neighbor2 = coreFunctions.getPixelSafer(image, x + 1, y); |
| 152 | + final neighbor3 = coreFunctions.getPixelSafer(image, x, y - 1); |
| 153 | + final neighbor4 = coreFunctions.getPixelSafer(image, x, y + 1); |
| 154 | + |
| 155 | + final neighbor1Value = neighbor1.r; |
| 156 | + final neighbor2Value = neighbor2.r; |
| 157 | + final neighbor3Value = neighbor3.r; |
| 158 | + final neighbor4Value = neighbor4.r; |
| 159 | + |
| 160 | + if (neighbor1Value == 255 || |
| 161 | + neighbor2Value == 255 || |
| 162 | + neighbor3Value == 255 || |
| 163 | + neighbor4Value == 255) { |
| 164 | + edgeImage.setPixel(x, y, getColor(255, 255, 255)); |
| 165 | + } else { |
| 166 | + edgeImage.setPixel(x, y, getColor(0, 0, 0)); |
| 167 | + } |
| 168 | + } else { |
| 169 | + edgeImage.setPixel(x, y, getColor(0, 0, 0)); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return edgeImage; |
| 175 | + } |
| 176 | + |
| 177 | + @override |
| 178 | + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 179 | +} |
0 commit comments