|
62 | 62 | const vegetationImageSrc = ref<string>() |
63 | 63 | const ratio = ref<number>() |
64 | 64 | const sliderValue = ref<number>(50) |
| 65 | + const polygon = ref<Polygon>() |
65 | 66 |
|
66 | 67 | const p1 = 0.95 |
67 | 68 | const p2 = 0.95 |
68 | 69 | const p3 = 20 |
69 | 70 | let maxRes = 1080 |
70 | 71 | let maxWidth = 1080 |
71 | 72 | let canvas: Canvas | undefined = undefined |
72 | | - let polygon: Polygon | undefined = undefined |
73 | 73 | let imageDimensions: Dims = { x: 0, y: 0 } |
74 | 74 | let canvasDimensions: Dims = { x: 0, y: 0 } |
75 | 75 |
|
|
97 | 97 | }) |
98 | 98 |
|
99 | 99 | function maximizePolygon () { |
100 | | - if (polygon) { |
101 | | - polygon.points = [{ x: 0, y: 0 }, { x: canvasDimensions.x, y: 0 }, { x: canvasDimensions.x, y: canvasDimensions.y }, { x: 0, y: canvasDimensions.y }] |
102 | | - polygon.setCoords() |
103 | | - polygon.canvas?.renderAll() |
| 100 | + if (polygon.value) { |
| 101 | + polygon.value.points = [{ x: 0, y: 0 }, { x: canvasDimensions.x, y: 0 }, { x: canvasDimensions.x, y: canvasDimensions.y }, { x: 0, y: canvasDimensions.y }] |
| 102 | + polygon.value.setCoords() |
| 103 | + polygon.value.canvas?.renderAll() |
104 | 104 | } |
105 | 105 | } |
106 | 106 |
|
|
110 | 110 | canvas = new Canvas(polygonCanvas.value) |
111 | 111 | const width = Math.min(maxWidth, imageDimensions.x) |
112 | 112 | const height = width * (imageDimensions.y / imageDimensions.x) |
| 113 | +
|
113 | 114 | canvasDimensions = { x: width, y: height } |
114 | 115 | canvas.setDimensions({ width, height }) |
115 | 116 | canvas.on('object:moving', (e: any) => { |
|
132 | 133 | }) |
133 | 134 | } |
134 | 135 |
|
135 | | - if (!polygon) { |
136 | | - polygon = new Polygon([{ x: 20, y: 20 }, { x: canvasDimensions.x - 40, y: 20 }, { x: canvasDimensions.x - 40, y: canvasDimensions.y - 40 }, { x: 20, y: canvasDimensions.y - 40 }], { |
| 136 | + if (!polygon.value) { |
| 137 | + polygon.value = new Polygon([{ x: 20, y: 20 }, { x: canvasDimensions.x - 40, y: 20 }, { x: canvasDimensions.x - 40, y: canvasDimensions.y - 40 }, { x: 20, y: canvasDimensions.y - 40 }], { |
137 | 138 | fill: 'white', |
138 | 139 | opacity: 0.25, |
139 | 140 | strokeWidth: 1, |
|
143 | 144 | cornerColor: '#6E1E41', |
144 | 145 | hasBorders: false, |
145 | 146 | }) |
146 | | - canvas.add(polygon) |
147 | | - polygon.controls = controlsUtils.createPolyControls(polygon) |
148 | | - Object.keys(polygon.controls).forEach(k => { |
149 | | - if (polygon) { |
150 | | - const control = polygon.controls[k] |
| 147 | + canvas.add(polygon.value) |
| 148 | + polygon.value.controls = controlsUtils.createPolyControls(polygon.value) |
| 149 | + Object.keys(polygon.value.controls).forEach(k => { |
| 150 | + if (polygon.value) { |
| 151 | + const control = polygon.value.controls[k] |
151 | 152 | const ph = control.actionHandler |
152 | 153 | control.actionHandler = (a: TPointerEvent, b: Transform, x: number, y: number) => { |
153 | 154 | const canvas = b.target.canvas |
|
159 | 160 | } |
160 | 161 | } |
161 | 162 | }) |
162 | | - canvas.setActiveObject(polygon) |
| 163 | + canvas.setActiveObject(polygon.value) |
163 | 164 | } |
164 | 165 | } |
165 | 166 | } |
|
196 | 197 | } |
197 | 198 |
|
198 | 199 | async function extractVegetation () { |
199 | | - const points = polygon?.points || [] |
| 200 | + const points = polygon.value?.points || [] |
200 | 201 |
|
201 | 202 | if (scaledImage.value && openCvCanvas.value) { |
202 | 203 | const src = cv.imread(scaledImage.value) |
|
220 | 221 | if (scaledImageSrc.value && scaledImage.value && openCvCanvas.value) { |
221 | 222 | const src = cv.imread(scaledImage.value) |
222 | 223 | const dst = new cv.Mat() |
223 | | - let dsize |
224 | 224 |
|
225 | 225 | if (src.cols > maxRes || src.rows > maxRes) { |
226 | 226 | imageDimensions = getDims(src.cols, src.rows, maxRes) |
227 | | - dsize = new cv.Size(imageDimensions.x, imageDimensions.y) |
| 227 | + const dsize = new cv.Size(imageDimensions.x, imageDimensions.y) |
228 | 228 |
|
229 | 229 | // You can try more different parameters |
230 | 230 | cv.resize(src, dst, dsize, 0, 0, cv.INTER_AREA) |
231 | 231 | cv.imshow(openCvCanvas.value, dst) |
232 | 232 | scaledImageSrc.value = openCvCanvas.value.toDataURL() |
233 | 233 | } else { |
| 234 | + imageDimensions = { x: src.cols, y: src.rows } |
234 | 235 | cv.imshow(openCvCanvas.value, src) |
235 | 236 | scaledImageSrc.value = openCvCanvas.value.toDataURL() |
236 | 237 | } |
237 | 238 |
|
238 | | - addPolygon() |
| 239 | + nextTick(() => addPolygon()) |
239 | 240 |
|
240 | 241 | deleteResources([src, dst]) |
241 | 242 | } |
|
0 commit comments