justadudewhohacks / justadudewhohacks/opencv4nodejs

Process finished with exit code -1073741819 (0xC0000005) when matching features

Open
#608 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
5.1k
Forks
826
PR merge metrics
No merged PRs in 30d

Description

Hi,
I am trying the to make feature matching by means of opencv4nodejs. I followed the example [matchFeatures.js](https://github.com/justadudewhohacks/opencv4nodejs/blob/master/examples/matchFeatures.js) but had to slightly adjust the source code for my purposes. The implemented functions look as follows:
First I read 14301 images from the given image directory. Then I call the findSimilaritiesOfArtObjects function in order to compare two images (i1, i2) with each other in order to get the 10 most similar images.
```
function similarityRanking() {
const dirs = fs.readdirSync("./images/")
const arts = {}
for(let d of dirs) {
let curr = undefined
if (curr === undefined) {
curr = d
arts[curr] = {}
}
for (let c of dirs) {
if (curr !== c) {
const i1 = curr
const i2 = c
findSimilaritiesOfArtObjects(i1, i2, arts, false)
}
}
}

for(let art in arts) {
const result = sortByValueNumber(arts[art])
console.log(result)
let top10 = ``
for(let i = 0; i < 10; i++) {
const imdas = result[i][0]
const dist = result[i][1]
top10 += `IMDAS-ID: ${imdas}, Distance: ${dist}\n`
const im1 = art
const im2 = imdas
findSimilaritiesOfArtObjects(im1, im2, undefined, true)
}
fs.writeFileSync(`./images/${art}/${art}_similar_images.txt`, top10)
}
}

```
The both images are read and then the different feature matching algorithms (sift, orb, bfmatcher) are executed.
```
function findSimilaritiesOfArtObjects(i1, i2, arts, store) {
if(fs.existsSync(`./images/${i1}/${i1}.jpg`) && fs.existsSync(`./images/${i2}/${i2}.jpg`)) {
const img1 = cv.imread(`./images/${i1}/${i1}.jpg`)
const img2 = cv.imread(`./images/${i2}/${i2}.jpg`)

if (cv.modules.xfeatures2d) {
const siftMatchesImg = matchFeatures({
img1,
img2,
detector: new cv.SIFTDetector({nFeatures: 2000}),
matchFunc: cv.matchFlannBased
}, i1 + "_" + i2, store);
if (arts !== undefined) {
if (!(i2 in arts[i1])) {
// arts[i1] = {}
arts[i1][i2] = 0
}
arts[i1][i2] += siftMatchesImg
}
} else {
console.log('skipping SIFT matches');
}

const orbMatchesImg = matchFeatures({
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming
}, i1 + "_" + i2, store);
if (arts !== undefined) {
arts[i1][i2] += orbMatchesImg
}

// Match using the BFMatcher with crossCheck true
const bf = new cv.BFMatcher(cv.NORM_L2, true);
const orbBFMatchIMG = matchFeatures({
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: (desc1, desc2) => bf.match(desc1, desc2)
}, i1 + "_" + i2, store);
if (arts !== undefined) {
arts[i1][i2] += orbBFMatchIMG
}
}

}

```
```
function matchFeatures({ img1, img2, detector, matchFunc }, name, store) {
// detect keypoints
const keyPoints1 = detector.detect(img1);
const keyPoints2 = detector.detect(img2);

// compute feature descriptors
const descriptors1 = detector.compute(img1, keyPoints1);
const descriptors2 = detector.compute(img2, keyPoints2);

// match the feature descriptors
const matches = matchFunc(descriptors1, descriptors2);

// only keep good matches
const bestN = 40;
const bestMatches = matches.sort(
(match1, match2) => match1.distance - match2.distance
).slice(0, bestN);

let summed = bestMatches.reduce(
(acc, curr) => {
return acc + curr.distance
},0)

if(store) {
const matchedImage = cv.drawMatches(
img1,
img2,
keyPoints1,
keyPoints2,
bestMatches
);
const dir = name.substring(0, name.indexOf("_"))
cv.imwrite(`./images/${dir}/${name}_matched_Features_${matchFunc.name}.jpg`, matchedImage)
}

return summed
}
```

```
async function main() {
similarityRanking()
}

main()
```
After some time of execution, I get the following error:

> Process finished with exit code -1073741819 (0xC0000005)

There is no further debug information regarding this error. It seems there is a memory allocation problem and I have no idea how to fix this. I also got this message by node inspect

> CustomMatAllocator::allocate - exception adjusting memory

It seems that a C++ class of opencv4nodejs is printing this error message. Any ideas?

Contributor guide

Open the contributing guide

Research direction

Start with examples/matchFeatures.js and the similarityRanking, findSimilaritiesOfArtObjects, and matchFeatures entry points shown in the report. Reproduce the run with the stated image volume and inspect the CustomMatAllocator allocation message and native process termination. Done means identifying a minimal reproducible failure and documenting or correcting the relevant allocation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, javascript, node.js
Domain
computer-vision
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.