Skip to content

Commit 8beeee6

Browse files
committed
full segmentation working well- can kill ui thread on large images
1 parent 6c4350a commit 8beeee6

File tree

8 files changed

+51
-24
lines changed

8 files changed

+51
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"color-alpha": "^1.0.4",
1212
"get-image-data": "^3.0.1",
1313
"material-survey": "^1.0.34",
14-
"mmgc1-cpp": "^1.0.35",
14+
"mmgc1-cpp": "^1.0.44",
1515
"moment": "^2.23.0",
1616
"react-full-screen": "^0.2.4",
1717
"react-hotkeys": "^2.0.0",

src/ImageCanvas/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,6 @@ export const ImageCanvas = ({
260260
bottomRight: mat.clone().inverse().applyToPoint(iw, ih),
261261
}
262262

263-
const classPoints = useMemo(() => {
264-
return regions.filter((region) => region.type === "point")
265-
}, [regions])
266-
267263
const highlightedRegion = useMemo(() => regions.find((r) => r.highlighted), [
268264
regions,
269265
])
@@ -379,7 +375,7 @@ export const ImageCanvas = ({
379375
imagePosition={imagePosition}
380376
regionClsList={regionClsList}
381377
imageSrc={imageSrc}
382-
classPoints={classPoints}
378+
regions={regions}
383379
/>
384380
)}
385381
<canvas

src/ImageMask/index.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useDebounce } from "react-use"
77
import MMGC_INIT from "mmgc1-cpp"
88

99
export const ImageMask = ({
10-
classPoints,
10+
regions,
1111
regionClsList,
1212
imageSrc,
1313
imagePosition,
@@ -49,7 +49,7 @@ export const ImageMask = ({
4949
if (hide) return
5050
if (!canvasRef) return
5151
if (!sampleImageData) return
52-
if (classPoints.filter((cp) => cp.cls).length < 3) return
52+
if (regions.filter((cp) => cp.cls).length < 3) return
5353
if (!mmgc.setImageSize) return
5454
const context = canvasRef.getContext("2d")
5555

@@ -79,9 +79,11 @@ export const ImageMask = ({
7979
)
8080
return
8181
}
82-
mmgc.clearClassPoints()
82+
mmgc.clearClassElements()
83+
const classPoints = regions
84+
.filter((r) => r.type === "point")
85+
.filter((r) => r.cls)
8386
for (const classPoint of classPoints) {
84-
if (!classPoint.cls) continue
8587
if (classPoint.x < 0 || classPoint.x >= 1) continue
8688
if (classPoint.y < 0 || classPoint.y >= 1) continue
8789
const clsIndex = regionClsList.indexOf(classPoint.cls)
@@ -96,13 +98,32 @@ export const ImageMask = ({
9698
Math.floor(classPoint.x * sampleImageData.width)
9799
)
98100
}
101+
const classPolygons = regions
102+
.filter((r) => r.type === "polygon")
103+
.filter((r) => r.cls)
104+
for (const polygon of classPolygons) {
105+
const { points } = polygon
106+
const clsIndex = regionClsList.indexOf(polygon.cls)
107+
const pi = mmgc.addPolygon(clsIndex)
108+
const pointPairs = points.map((p, i) => [
109+
p,
110+
points[(i + 1) % points.length],
111+
])
112+
for (const [p1, p2] of pointPairs) {
113+
const ri1 = Math.round(p1[1] * sampleImageData.height)
114+
const ci1 = Math.round(p1[0] * sampleImageData.width)
115+
const ri2 = Math.round(p2[1] * sampleImageData.height)
116+
const ci2 = Math.round(p2[0] * sampleImageData.width)
117+
mmgc.addLineToPolygon(pi, ri1, ci1, ri2, ci2)
118+
}
119+
}
120+
99121
mmgc.computeMasks()
100122
const maskAddress = mmgc.getColoredMask()
101123
const cppImDataUint8 = new Uint8ClampedArray(
102124
mmgc.HEAPU8.buffer,
103125
maskAddress,
104126
sampleImageData.data.length
105-
// sampleImageData.width * sampleImageData.height * 4
106127
)
107128
const maskImageData = new ImageData(
108129
cppImDataUint8,
@@ -114,7 +135,7 @@ export const ImageMask = ({
114135
context.putImageData(maskImageData, 0, 0)
115136
},
116137
1000,
117-
[canvasRef, sampleImageData, classPoints, hide]
138+
[canvasRef, sampleImageData, regions, hide]
118139
)
119140

120141
const style = useMemo(() => {

src/RegionLabel/styles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
fontSize: 12,
88
cursor: "default",
99
transition: "opacity 200ms",
10-
opacity: 0.7,
10+
opacity: 0.5,
1111
"&:hover": {
1212
opacity: 0.9,
1313
cursor: "pointer",

src/RegionSelectAndTransformBoxes/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ const arePropsEqual = (prev, next) => {
2525
prev.dragWithPrimary === next.dragWithPrimary &&
2626
prev.createWithPrimary === next.createWithPrimary &&
2727
prev.zoomWithPrimary === next.zoomWithPrimary &&
28-
prev.layoutParams.current.iw === next.layoutParams.current.iw &&
29-
prev.layoutParams.current.ih === next.layoutParams.current.ih &&
3028
prev.mat === next.mat
3129
)
3230
}
@@ -42,6 +40,7 @@ export const RegionSelectAndTransformBox = memo(
4240
onBeginMovePoint,
4341
onSelectRegion,
4442
layoutParams,
43+
fullImageSegmentationMode = false,
4544
mat,
4645
onBeginBoxTransform,
4746
onBeginMovePolygonPoint,
@@ -53,7 +52,7 @@ export const RegionSelectAndTransformBox = memo(
5352
return (
5453
<Fragment>
5554
<PreventScrollToParents>
56-
{showHighlightBox && (
55+
{showHighlightBox && r.type !== "polygon" && (
5756
<HighlightBox
5857
region={r}
5958
mouseEvents={mouseEvents}
@@ -178,7 +177,7 @@ export const RegionSelectAndTransformBoxes = memo(
178177
return <RegionSelectAndTransformBox key={r.id} {...props} region={r} />
179178
})
180179
},
181-
(n, p) => n.regions === p.regions
180+
(n, p) => n.regions === p.regions && n.mat === p.mat
182181
)
183182

184183
export default RegionSelectAndTransformBoxes

src/RegionShapes/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ const RegionComponents = {
3131
/>
3232
</g>
3333
)),
34-
polygon: memo(({ region, iw, ih }) => {
34+
polygon: memo(({ region, iw, ih, fullImageSegmentationMode }) => {
3535
const Component = region.open ? "polyline" : "polygon"
36+
const alphaBase = fullImageSegmentationMode ? 0.5 : 1
3637
return (
3738
<Component
3839
points={region.points
@@ -112,12 +113,20 @@ const RegionComponents = {
112113
}
113114

114115
export const WrappedRegionList = memo(
115-
({ regions, iw, ih }) => {
116+
({ regions, iw, ih, fullImageSegmentationMode }) => {
116117
return regions
117118
.filter((r) => r.visible !== false)
118119
.map((r) => {
119120
const Component = RegionComponents[r.type]
120-
return <Component key={r.regionId} region={r} iw={iw} ih={ih} />
121+
return (
122+
<Component
123+
key={r.regionId}
124+
region={r}
125+
iw={iw}
126+
ih={ih}
127+
fullImageSegmentationMode={fullSegmentationMode}
128+
/>
129+
)
121130
})
122131
},
123132
(n, p) => n.regions === p.regions && n.iw === p.iw && n.ih === p.ih
@@ -126,6 +135,7 @@ export const WrappedRegionList = memo(
126135
export const RegionShapes = ({ mat, imagePosition, regions = [] }) => {
127136
const iw = imagePosition.bottomRight.x - imagePosition.topLeft.x
128137
const ih = imagePosition.bottomRight.y - imagePosition.topLeft.y
138+
if (isNaN(iw) || isNaN(ih)) return null
129139
return (
130140
<svg
131141
width={iw}

src/RegionTags/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export const RegionTags = ({
9797
<div
9898
style={{
9999
position: "absolute",
100+
zIndex: 20,
100101
left: 0,
101102
...(displayOnTop ? { bottom: 0 } : { top: 0 }),
102103
}}

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10841,10 +10841,10 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@
1084110841
dependencies:
1084210842
minimist "0.0.8"
1084310843

10844-
mmgc1-cpp@^1.0.35:
10845-
version "1.0.35"
10846-
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.35.tgz#654140c7e942dc32587cfe6cdc388a453ffa7d16"
10847-
integrity sha512-GVXtpvdQdzd6mNPJe/QqQEAuwUROA3jP5TUiHtbuK27ZfEAM77yyjp5LxzwtvfZ2vq0b0B07SkcpmphEGXsI8g==
10844+
mmgc1-cpp@^1.0.44:
10845+
version "1.0.44"
10846+
resolved "https://registry.yarnpkg.com/mmgc1-cpp/-/mmgc1-cpp-1.0.44.tgz#b3059e9df06d1dba5899b159c454e1d435fec317"
10847+
integrity sha512-7kJk5zggfAR05id6a6ltXih39y8Va+zir1wevORNXBr/Glc2gtrqyc0HciXrOqyAUZ6Y93aRLW11BODsnDSemQ==
1084810848

1084910849
moment@^2.23.0:
1085010850
version "2.24.0"

0 commit comments

Comments
 (0)