i am learning python so wrote this small code to add 2 images with ROI but it is showing me error












0















I have wrote a code which i copied from internet while learning open cv but when i run this code it is showing me some errors i don't know how to fix it



import cv2
import numpy as np

# Load two images
img1 = cv2.imread('car.png')
img2 = cv2.imread('coca cola.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


it is showing me this error



> Traceback (most recent call last):   File "D:/Python codes/put one
> image on other.py", line 18, in <module>
> img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) cv2.error: OpenCV(3.4.3)
> C:projectsopencv-pythonopencvmodulescoresrcarithm.cpp:241:
> error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) &&
> _mask.sameSize(*psrc1) in function 'cv::binary_op'









share|improve this question

























  • 8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • duplicate? stackoverflow.com/questions/44019023/…

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

    – Alexander Reynolds
    Nov 22 '18 at 7:52
















0















I have wrote a code which i copied from internet while learning open cv but when i run this code it is showing me some errors i don't know how to fix it



import cv2
import numpy as np

# Load two images
img1 = cv2.imread('car.png')
img2 = cv2.imread('coca cola.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


it is showing me this error



> Traceback (most recent call last):   File "D:/Python codes/put one
> image on other.py", line 18, in <module>
> img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) cv2.error: OpenCV(3.4.3)
> C:projectsopencv-pythonopencvmodulescoresrcarithm.cpp:241:
> error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) &&
> _mask.sameSize(*psrc1) in function 'cv::binary_op'









share|improve this question

























  • 8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • duplicate? stackoverflow.com/questions/44019023/…

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

    – Alexander Reynolds
    Nov 22 '18 at 7:52














0












0








0








I have wrote a code which i copied from internet while learning open cv but when i run this code it is showing me some errors i don't know how to fix it



import cv2
import numpy as np

# Load two images
img1 = cv2.imread('car.png')
img2 = cv2.imread('coca cola.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


it is showing me this error



> Traceback (most recent call last):   File "D:/Python codes/put one
> image on other.py", line 18, in <module>
> img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) cv2.error: OpenCV(3.4.3)
> C:projectsopencv-pythonopencvmodulescoresrcarithm.cpp:241:
> error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) &&
> _mask.sameSize(*psrc1) in function 'cv::binary_op'









share|improve this question
















I have wrote a code which i copied from internet while learning open cv but when i run this code it is showing me some errors i don't know how to fix it



import cv2
import numpy as np

# Load two images
img1 = cv2.imread('car.png')
img2 = cv2.imread('coca cola.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg)
img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()


it is showing me this error



> Traceback (most recent call last):   File "D:/Python codes/put one
> image on other.py", line 18, in <module>
> img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) cv2.error: OpenCV(3.4.3)
> C:projectsopencv-pythonopencvmodulescoresrcarithm.cpp:241:
> error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) &&
> _mask.sameSize(*psrc1) in function 'cv::binary_op'






python python-3.x opencv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:31









Jean-François Fabre

102k954111




102k954111










asked Nov 22 '18 at 6:26









Shubhanker TiwariShubhanker Tiwari

32




32













  • 8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • duplicate? stackoverflow.com/questions/44019023/…

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

    – Alexander Reynolds
    Nov 22 '18 at 7:52



















  • 8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • duplicate? stackoverflow.com/questions/44019023/…

    – Jean-François Fabre
    Nov 22 '18 at 6:33











  • The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

    – Alexander Reynolds
    Nov 22 '18 at 7:52

















8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

– Jean-François Fabre
Nov 22 '18 at 6:33





8U or 8S looks like 8 bit unsigned or signed. probably wrong/unsupported image type in input. Those libs aren't as good as standard ones when it comes to error messages ...

– Jean-François Fabre
Nov 22 '18 at 6:33













duplicate? stackoverflow.com/questions/44019023/…

– Jean-François Fabre
Nov 22 '18 at 6:33





duplicate? stackoverflow.com/questions/44019023/…

– Jean-François Fabre
Nov 22 '18 at 6:33













The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

– Alexander Reynolds
Nov 22 '18 at 7:52





The types here all look fine, I would check the size of your images and masks and be sure it's all good. Is it possible that your coca cola image is larger than your car image?

– Alexander Reynolds
Nov 22 '18 at 7:52












0






active

oldest

votes











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',
autoActivateHeartbeat: false,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425017%2fi-am-learning-python-so-wrote-this-small-code-to-add-2-images-with-roi-but-it-is%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425017%2fi-am-learning-python-so-wrote-this-small-code-to-add-2-images-with-roi-but-it-is%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno