cv2 drawMatches draws on blank screen?
up vote
1
down vote
favorite
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
gives expected output in jupyter notebook, but when I save the file using python script, it is drawing matches and flags=4
is drawing keypoints just fine, except everything is happening on a black image (of right size: left + right combined).
Possible backend selection issues like we have with matplotlib
?
The example code works just fine:
import numpy as np
import cv2
def getCorrespondence(imageLeft, imageRight):
orb = cv2.ORB_create()
kpLeft, desLeft = orb.detectAndCompute(imageLeft, None)
kpRight, desRight = orb.detectAndCompute(imageRight, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desLeft, desRight)
goodMatches =
for m in matches:
if m.distance < 100:
goodMatches.append(m)
print('Matches', len(goodMatches))
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
return imageCorrespondence
if __name__ == '__main__':
imageLeft = cv2.imread('image_001.png')
imageRight = cv2.imread('image_002.png')
imageCorrespondence = getCorrespondence(imageLeft, imageRight)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
print('Image Saved')
But as soon as I start using the function with other images loaded from elsewhere, it is breaking something. I made sure if those images have content and cv2.imwrite('imageLeft', imageLeft)
works just fine and the image gets saved fine.
python opencv
add a comment |
up vote
1
down vote
favorite
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
gives expected output in jupyter notebook, but when I save the file using python script, it is drawing matches and flags=4
is drawing keypoints just fine, except everything is happening on a black image (of right size: left + right combined).
Possible backend selection issues like we have with matplotlib
?
The example code works just fine:
import numpy as np
import cv2
def getCorrespondence(imageLeft, imageRight):
orb = cv2.ORB_create()
kpLeft, desLeft = orb.detectAndCompute(imageLeft, None)
kpRight, desRight = orb.detectAndCompute(imageRight, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desLeft, desRight)
goodMatches =
for m in matches:
if m.distance < 100:
goodMatches.append(m)
print('Matches', len(goodMatches))
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
return imageCorrespondence
if __name__ == '__main__':
imageLeft = cv2.imread('image_001.png')
imageRight = cv2.imread('image_002.png')
imageCorrespondence = getCorrespondence(imageLeft, imageRight)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
print('Image Saved')
But as soon as I start using the function with other images loaded from elsewhere, it is breaking something. I made sure if those images have content and cv2.imwrite('imageLeft', imageLeft)
works just fine and the image gets saved fine.
python opencv
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
gives expected output in jupyter notebook, but when I save the file using python script, it is drawing matches and flags=4
is drawing keypoints just fine, except everything is happening on a black image (of right size: left + right combined).
Possible backend selection issues like we have with matplotlib
?
The example code works just fine:
import numpy as np
import cv2
def getCorrespondence(imageLeft, imageRight):
orb = cv2.ORB_create()
kpLeft, desLeft = orb.detectAndCompute(imageLeft, None)
kpRight, desRight = orb.detectAndCompute(imageRight, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desLeft, desRight)
goodMatches =
for m in matches:
if m.distance < 100:
goodMatches.append(m)
print('Matches', len(goodMatches))
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
return imageCorrespondence
if __name__ == '__main__':
imageLeft = cv2.imread('image_001.png')
imageRight = cv2.imread('image_002.png')
imageCorrespondence = getCorrespondence(imageLeft, imageRight)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
print('Image Saved')
But as soon as I start using the function with other images loaded from elsewhere, it is breaking something. I made sure if those images have content and cv2.imwrite('imageLeft', imageLeft)
works just fine and the image gets saved fine.
python opencv
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
gives expected output in jupyter notebook, but when I save the file using python script, it is drawing matches and flags=4
is drawing keypoints just fine, except everything is happening on a black image (of right size: left + right combined).
Possible backend selection issues like we have with matplotlib
?
The example code works just fine:
import numpy as np
import cv2
def getCorrespondence(imageLeft, imageRight):
orb = cv2.ORB_create()
kpLeft, desLeft = orb.detectAndCompute(imageLeft, None)
kpRight, desRight = orb.detectAndCompute(imageRight, None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desLeft, desRight)
goodMatches =
for m in matches:
if m.distance < 100:
goodMatches.append(m)
print('Matches', len(goodMatches))
imageCorrespondence = cv2.drawMatches(imageLeft, kpLeft, imageRight, kpRight, [goodMatches[0]], None, flags=2)
return imageCorrespondence
if __name__ == '__main__':
imageLeft = cv2.imread('image_001.png')
imageRight = cv2.imread('image_002.png')
imageCorrespondence = getCorrespondence(imageLeft, imageRight)
cv2.imwrite('imageCorrespondence.png', imageCorrespondence)
print('Image Saved')
But as soon as I start using the function with other images loaded from elsewhere, it is breaking something. I made sure if those images have content and cv2.imwrite('imageLeft', imageLeft)
works just fine and the image gets saved fine.
python opencv
python opencv
edited Nov 20 at 11:26
asked Nov 19 at 22:28
Saravanabalagi Ramachandran
2,75621951
2,75621951
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38
add a comment |
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I initially thought the sixth param None
was causing this, but that is not causing any trouble.
cv2.drawMatches()
takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:
outImg = cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]] )
Parameters
img1
First source image.
keypoints1
Keypoints from the first source image.
img2
Second source image.
keypoints2
Keypoints from the second source image. ...
However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib
handles alpha layer differs from how cv2.imwrite
handles the same, that it seemed to work in Jupyter notebook but not using the Python script.
I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383569%2fcv2-drawmatches-draws-on-blank-screen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I initially thought the sixth param None
was causing this, but that is not causing any trouble.
cv2.drawMatches()
takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:
outImg = cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]] )
Parameters
img1
First source image.
keypoints1
Keypoints from the first source image.
img2
Second source image.
keypoints2
Keypoints from the second source image. ...
However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib
handles alpha layer differs from how cv2.imwrite
handles the same, that it seemed to work in Jupyter notebook but not using the Python script.
I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
add a comment |
up vote
1
down vote
accepted
I initially thought the sixth param None
was causing this, but that is not causing any trouble.
cv2.drawMatches()
takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:
outImg = cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]] )
Parameters
img1
First source image.
keypoints1
Keypoints from the first source image.
img2
Second source image.
keypoints2
Keypoints from the second source image. ...
However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib
handles alpha layer differs from how cv2.imwrite
handles the same, that it seemed to work in Jupyter notebook but not using the Python script.
I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I initially thought the sixth param None
was causing this, but that is not causing any trouble.
cv2.drawMatches()
takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:
outImg = cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]] )
Parameters
img1
First source image.
keypoints1
Keypoints from the first source image.
img2
Second source image.
keypoints2
Keypoints from the second source image. ...
However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib
handles alpha layer differs from how cv2.imwrite
handles the same, that it seemed to work in Jupyter notebook but not using the Python script.
I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!
I initially thought the sixth param None
was causing this, but that is not causing any trouble.
cv2.drawMatches()
takes in imageLeft and imageRight as numpy arrays as mentioned in the docs:
outImg = cv.drawMatches( img1, keypoints1, img2, keypoints2, matches1to2, outImg[, matchColor[, singlePointColor[, matchesMask[,flags]]]] )
Parameters
img1
First source image.
keypoints1
Keypoints from the first source image.
img2
Second source image.
keypoints2
Keypoints from the second source image. ...
However something that breaks this is the alpha layer, if you happen to load the alpha layer in the numpy array, it would draw on a black image. When I removed the alpha layer in the numpy array manually and had only three channels, it started working fine. This could be because of the way matplotlib
handles alpha layer differs from how cv2.imwrite
handles the same, that it seemed to work in Jupyter notebook but not using the Python script.
I initially thought I needed to switch from BGRA to ABGR, but that's not the case BGRA is just fine, I was getting a black screen if the input images had a fourth alpha layer. Opencv usually strips the alpha layer when reading an image...!
edited Nov 20 at 12:09
answered Nov 20 at 11:33
Saravanabalagi Ramachandran
2,75621951
2,75621951
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
add a comment |
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
1
1
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
Good catch. Looking at the source code, here is the culprit and here's another. The random colour generation leaves the alpha channel at default value of 0, so whatever it draws ends up transparent. I'll file a bug report, this is a trivial fix.
– Dan Mašek
Nov 20 at 13:07
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
github.com/opencv/opencv/issues/13219
– Dan Mašek
Nov 20 at 13:15
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
And one more: github.com/opencv/opencv/issues/13227 | Fix for the previous issue is already merged, should be fine in 3.4.5 and the next release of 4.x. I guess this is really what you ran into.
– Dan Mašek
Nov 20 at 20:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383569%2fcv2-drawmatches-draws-on-blank-screen%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Provide a Minimal, Complete, and Verifiable example. Shouldn't have to remind a 5 year user with reputation in thousands about that....
– Dan Mašek
Nov 20 at 0:16
Sorry, I thought I missed something in those two lines I posted initially (esp because I set the output image param to None) which I assumed would be obvious to opencv users, hence didn't add the full code. Added it now :)
– Saravanabalagi Ramachandran
Nov 20 at 11:38