Skip to content

Commit b3ff8fd

Browse files
committed
Draw bounding box
1 parent 589dc1f commit b3ff8fd

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "emscripten-opencv",
3-
"version": "0.0.5",
3+
"version": "0.0.6",
44
"description": "Web app calling into C++ WebAssembly code that uses OpenCV",
55
"scripts": {
66
"build:wasm": "./build-wasm.sh",

src/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
<div class="container">
1414
<div class="row">
1515
<div class="col-md-6 col-xs-12">
16-
<img id="input-image" src="sudoku-puzzle.png" alt="A photo of a Sudoku puzzle from a newspaper" />
16+
<div id="input-image-wrapper">
17+
<img id="input-image" src="sudoku-puzzle.png" alt="A photo of a Sudoku puzzle from a newspaper" />
18+
<canvas id="input-image-overlay"></canvas>
19+
</div>
1720
</div>
1821
<div class="col-md-6 col-xs-12">
1922
<canvas id="output-image"></canvas>

src/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ const imageDataFrom1Channel = (data, width, height) => {
2323
const array = new Uint8ClampedArray(cb)
2424
data.forEach((pixelValue, index) => {
2525
const base = index * 4
26-
array[base] = pixelValue // R
27-
array[base + 1] = pixelValue // G
28-
array[base + 2] = pixelValue // B
29-
array[base + 3] = 255 // A
26+
const r = g = b = pixelValue
27+
const a = 255
28+
array[base] = r
29+
array[base + 1] = g
30+
array[base + 2] = b
31+
array[base + 3] = a
3032
})
3133
const imageData = new ImageData(array, width, height)
3234
return imageData
@@ -48,6 +50,15 @@ const drawOutputImage = imageData => {
4850
ctx.putImageData(imageData, 0, 0)
4951
}
5052

53+
const drawBoundingBox = (x, y, w, h) => {
54+
console.log('[drawBoundingBox]')
55+
const canvas = document.getElementById('input-image-overlay')
56+
ctx = canvas.getContext('2d')
57+
ctx.strokeStyle = 'red'
58+
ctx.lineWidth = 2
59+
ctx.strokeRect(x, y, w, h)
60+
}
61+
5162
const onProcessImage = (module, processImage) => () => {
5263
console.log('[onProcessImage]')
5364
const { data, width, height } = getImageData()
@@ -67,6 +78,7 @@ const onProcessImage = (module, processImage) => () => {
6778
const imageData = imageDataFrom4Channels(outImageData, outImageWidth, outImageHeight)
6879
drawOutputImage(imageData)
6980
}
81+
drawBoundingBox(bbx, bby, bbw, bbh)
7082
module._free(addr)
7183
}
7284

@@ -84,4 +96,8 @@ const init = module => {
8496
const processImage = wrapProcessImage(module)
8597
const processImageBtn = document.getElementById('process-image-btn')
8698
processImageBtn.addEventListener('click', onProcessImage(module, processImage))
99+
const inputImage = document.getElementById('input-image')
100+
const inputImageOverlay = document.getElementById('input-image-overlay')
101+
inputImageOverlay.width = inputImage.width
102+
inputImageOverlay.height = inputImage.height
87103
}

src/styles.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,13 @@
55
.row-gap {
66
margin-top: 5px;
77
}
8+
9+
#input-image-wrapper {
10+
position: relative;
11+
}
12+
13+
#input-image-overlay {
14+
position: absolute;
15+
top: 0;
16+
left: 0;
17+
}

0 commit comments

Comments
 (0)