diff --git a/public/test.jpg b/public/test.jpg index 014c9e0..e983e5f 100644 Binary files a/public/test.jpg and b/public/test.jpg differ diff --git a/public/test1.jpg b/public/test1.jpg new file mode 100644 index 0000000..014c9e0 Binary files /dev/null and b/public/test1.jpg differ diff --git a/public/test3.jpg b/public/test3.jpg new file mode 100644 index 0000000..80cfc71 Binary files /dev/null and b/public/test3.jpg differ diff --git a/src/lib/ImageShow.svelte b/src/lib/ImageShow.svelte index b304c8a..646220e 100644 --- a/src/lib/ImageShow.svelte +++ b/src/lib/ImageShow.svelte @@ -1,21 +1,26 @@ @@ -166,6 +245,7 @@ bind:innerHeight={window_height} bind:innerWidth={window_width} on:resize={on_window_resize} + on:keydown={keyboard_move_roi} />
@@ -181,9 +261,7 @@
  • { - active = item.id; if (item.handler) { item.handler(); } @@ -201,7 +279,16 @@
  • - +
    + {#if mask_tag} + + {/if} + +
    diff --git a/src/lib/ImageShow copy.svelte b/src/lib/ImageShowWithFabric.svelte similarity index 79% rename from src/lib/ImageShow copy.svelte rename to src/lib/ImageShowWithFabric.svelte index ac3fa9d..40b25e2 100644 --- a/src/lib/ImageShow copy.svelte +++ b/src/lib/ImageShowWithFabric.svelte @@ -1,16 +1,12 @@ @@ -129,9 +137,7 @@
  • { - active = item.id; if (item.handler) { item.handler(); } diff --git a/src/lib/Mask.svelte b/src/lib/Mask.svelte new file mode 100644 index 0000000..ed71b44 --- /dev/null +++ b/src/lib/Mask.svelte @@ -0,0 +1,283 @@ + + +{#each mask_rects as r, i} + +
    { + set_crop_rect(e, i); + }} + on:mousedown={() => { + if (i == 4) { + crop_tag = true; + } + }} + on:mouseup={() => { + crop_tag = false; + }} + > +
    +
    +{/each} + + diff --git a/src/lib/Navbar.svelte b/src/lib/Navbar.svelte index 2485db1..3566a12 100644 --- a/src/lib/Navbar.svelte +++ b/src/lib/Navbar.svelte @@ -5,7 +5,7 @@ let user; let guide = new RouterGuide(); - + function logout() { guide.push("/login"); } diff --git a/src/lib/utils.js b/src/lib/utils.js index 5899611..a0b82b6 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -1,5 +1,8 @@ import procedure from "../assets/Procedure.json"; +const cv = window.cv; +const move_step = 3; + export function Operation(step) { this.step = step; @@ -18,11 +21,187 @@ export function Operation(step) { if (!s) { return procedure.find((e) => e.id === this.step).name; } - console.log(s); return procedure.find((e) => e.id === s).name; }; this.get_color = () => { return procedure.find((e) => e.id === this.step).color; }; -} \ No newline at end of file +} + +export function ImageProcess() { + let angle = 0; + + this.roi_tag = false; + this.roi_scale_ratio = 1; + + this.rotate_tag = -1; + + this.ROI_rect = new cv.Rect(0, 0, 0, 0); + this.crop_rect = null; + + this.recover = () => { + angle = 0; + this.roi_tag = false; + this.roi_scale_ratio = 1; + + this.rotate_tag = -1; + + this.ROI_rect = new cv.Rect(0, 0, 0, 0); + this.crop_rect = null; + }; + + this.roi_init = () => { + this.roi_tag = false; + this.roi_scale_ratio = 1; + + this.ROI_rect = new cv.Rect(0, 0, 0, 0); + }; + + this.roi_move = ({ x, y }) => { + if (x >= 0) { + this.roi_right(x); + } else { + this.roi_left(-x); + } + if (y >= 0) { + this.roi_down(y); + } else { + this.roi_down(-y); + } + }; + + this.roi_up = (step = move_step) => { + let x = this.ROI_rect.x; + let y = this.ROI_rect.y >= step ? this.ROI_rect.y - step : 0; + let width = this.ROI_rect.width; + let height = this.ROI_rect.height; + + this.ROI_rect = new cv.Rect(x, y, width, height); + }; + + this.roi_down = (rows, step = move_step) => { + let x = this.ROI_rect.x; + let y = + this.ROI_rect.y <= rows - this.ROI_rect.height - step + ? this.ROI_rect.y + step + : rows - this.ROI_rect.height - step; + let width = this.ROI_rect.width; + let height = this.ROI_rect.height; + this.ROI_rect = new cv.Rect(x, y, width, height); + }; + + this.roi_left = (step = move_step) => { + let x = this.ROI_rect.x >= step ? this.ROI_rect.x - step : 0; + let y = this.ROI_rect.y; + let width = this.ROI_rect.width; + let height = this.ROI_rect.height; + + this.ROI_rect = new cv.Rect(x, y, width, height); + }; + + this.roi_right = (cols, step = move_step) => { + let x = + this.ROI_rect.x + this.ROI_rect.width + step <= cols + ? this.ROI_rect.x + step + : cols - this.ROI_rect.width - step; + let y = this.ROI_rect.y; + let width = this.ROI_rect.width; + let height = this.ROI_rect.height; + this.ROI_rect = new cv.Rect(x, y, width, height); + }; + + this.roi = (rows, cols, center, delta_wheel, scale_ratio = 0.05) => { + // Transition coordinate system at first. + let c_at_image = new cv.Point( + center.x * this.roi_scale_ratio + this.ROI_rect.x, + center.y * this.roi_scale_ratio + this.ROI_rect.y + ); + + let ratio = this.roi_scale_ratio + (delta_wheel / 144) * scale_ratio; + this.roi_scale_ratio = ratio <= 1 ? ratio : 1; + + if (!this.roi_tag) { + this.ROI_rect = new cv.Rect(0, 0, cols, rows); + this.roi_tag = true; + } + + let roi_width = cols * this.roi_scale_ratio; + let roi_height = rows * this.roi_scale_ratio; + + if (Math.min(roi_width / cols, roi_height / rows) <= 0.3) { + this.roi_scale_ratio = 0.3; + return; + } + + let x = + c_at_image.x - + (c_at_image.x * roi_width) / this.ROI_rect.width + + (this.ROI_rect.x * roi_width) / this.ROI_rect.width; + let y = + c_at_image.y - + (c_at_image.y * roi_height) / this.ROI_rect.height + + (this.ROI_rect.y * roi_height) / this.ROI_rect.height; + + if (roi_width + x <= cols && roi_height + y <= rows) { + this.ROI_rect = new cv.Rect( + x >= 0 ? x : 0, + y >= 0 ? y : 0, + roi_width, + roi_height + ); + } else if (roi_width + x <= cols && roi_height + y >= rows) { + this.ROI_rect = new cv.Rect( + x >= 0 ? x : 0, + rows - roi_height, + roi_width, + roi_height + ); + } else if (roi_width + x >= cols && roi_height + y >= rows) { + this.ROI_rect = new cv.Rect( + cols - roi_width, + rows - roi_height, + roi_width, + roi_height + ); + } + }; + + this.turn_left = () => { + this.roi_init(); + angle = angle + 270; + let k = angle % 360; + + if (k == 90) { + this.rotate_tag = 0; + } else if (k == 180) { + this.rotate_tag = 1; + } else if (k == 270) { + this.rotate_tag = 2; + } else { + this.rotate_tag = -1; + } + }; + this.turn_right = () => { + this.roi_init(); + + angle = angle + 90; + let k = angle % 360; + if (k == 90) { + this.rotate_tag = 0; + } else if (k == 180) { + this.rotate_tag = 1; + } else if (k == 270) { + this.rotate_tag = 2; + } else { + this.rotate_tag = -1; + } + }; + + this.set_roi = (rect) => { + this.ROI_rect = rect; + }; + this.set_crop_roi = (rect) => { + this.crop_rect = rect; + }; +} diff --git a/src/route/Route.js b/src/route/Route.js index a0c974e..f695b6d 100644 --- a/src/route/Route.js +++ b/src/route/Route.js @@ -4,6 +4,7 @@ import { setContext } from "svelte"; import Main from "../lib/Main.svelte"; import Login from "../lib/Login.svelte"; import Image from "../lib/ImageShow.svelte"; +// import Image from "../lib/ImageShowWithFabric.svelte"; const RouterMap = { index: Main,