The co-ordinates used in the array follow the format of img [y1: y2, x1: x2] Therefore, when copying to another part of the image, you need to ensure that (y2-y1) value remains the same, as well as (x2-x1) Here's another example to copy messi's head, where Top left coordinates is (200, 60) and bottom right is (270, 140) in x,y format messi_head = img[60:140, 200:270] img[260:340,100:170] = messi_head
your comment helped me to figure how to mwke this copy paste thing easy... i have used mouse click to select coordinates of the region which i want to copy and then i have selected by mouse click only the top left corner coordinate of region where i want to paste the ball and the bottom right coordinates i calculated using the formula i figured out from your comment. i hope my comment will be helpful for others and if anyone face any problem i can post the code
Great tutorial, sir, Thank you very much! I'd like to give a small contribution: I got an error even after resizing (12:12). According to the docs, add() oly works when both images have the same size and same number of channles. However, print(img.shape) says JPG file has 3 channels and print(img2.shape) says PGN file has 4, so we can't add them. But we can remove the 4th channel of the PGN file like this: if len(img2.shape) > 2 and img2.shape[2] == 4: img2 = cv2.cvtColor(img2, cv2.COLOR_BGRA2BGR) Now it worked. Again, thanks for your effort.
Nice video! Great demonstration of addWeighted. Best I have seen so far. Please keep up the great work. Can you show how to use addWeighted to slowly fade in an image, like maybe fade in another soccer ball?
Hi, I am facing this error ValueError: could not broadcast input array from shape (19,36,3) into shape (9,36,3) . While doing ROI . Could u plz guide me how to resolve this error
img.size = heights * width * channels (the size calculation ALSO includes the number of channels - 3 channels for color images and 1 for grayscale images)
I had the same issue... and found out the solution as below: 280:340 is y_upper_left_corner: y_lower_right_corner 330:390 is x_upper_left_corner: x_lower_right_corner ball = img[280:340, 330:390] # img[y,x]
Sir how you got ball co-ordinates in that image how to get that co-ordinate please clarify my doubt I tried but I'm not getting please help me thank you
sai kumar : Using the previous video get coordinates. for example First click on top left corner of the image (x1 =198,y1=131) and then click on right bottom corner of image(x2 = 282,y2 = 161). Now you have two coordinates, then your copy image , copy_img = img[131:161,198:282] in form of [y1:y2,x1:x2] and your img[47:77,80:164] = copy_img. mind here we use y first then x as y is row and x is column in matrix of image and size 161-131 == 47 -77 and 282 - 198 == 80-164. I hope this might helped you.
Hello Sir! i am facing an error " img[273:333 , 100:160 ,1 ] = ball ValueError: could not broadcast input array from shape (0,60,3) into shape (7,60) " please suggest me how to resolve this error?
i am getting error while i am adding two image of the same size: cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src esize.cpp:3720: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
What Michal says, but it's not just about fonts. Weight is (in this example) a comparisson between the two images. Opacity is transparency. If one of the two images is a completely transparant one, then the weight would behave like opacity, but thats only in that particular example.
You can mark the square containing the ball with img = cv2.rectangle(img, (338, 290), (388, 335), (0, 255, 255), 1) Note: the coordinates x, y are reversed compared to np indexing (I'm not sure why x-coordinates are ok, but y-coordinates are off by 5 pixels approx)
@@davidhermawan942 its simple. (1)Select arbritary point that will be ur x1, y1. (2) select another point that is x2, y2. now u need to be careful and modify x2 or y2 value such that y2-y1=x2-x1. Finally write the code which takes value as [y1:y2,x2:x1]
bal = img[280:340, 330:390] here you said (280,340) --> upper left corner & (330,390) --> bottom right corner. Then shouldn't y coordinate 390< 340. what's the logic here selecting the coordinate.
checkout his vid for lane detection using opencv, there is a concept of the numbering on the x-axis and y-axis which is a bit different from generally what we know and hence the change in coordinates.
My solution messi_head = img[54:130, 191:266] # *** location of the head img [175:251,17:92] = messi_head # *** where to copy it for the head it is img[y1:y2, x1:x2] and copy is = [y1:y2,x1:x2] with a remainder that the differences between x1 and x2 = 75 and y1 and y2 = 76 make the calculations
see this problem when import numpy as np import cv2 img = cv2.imread('messi5.jpg') img2 = cv2.imread('opencv-logo.jpg') print(img.shape)# return a tuple of no of rows,colum,&channel print(img.size)#return total no of pixels is accessed print(img.dtype)#return image datatype is obtained b,g,r = cv2.split(img) img = cv2.merge((b,g,r)) #-------add image ball------- #region of interest ROI # copy this cordinate like ball ball = img [280:340,330:390]#numpy indexing img[273:333, 100:160] = ball #place other position with numpy indexing #--------end other ball section------ #----same size image----both img = cv2.resize(img(512,512)) img2 = cv2.resize(img2,(512,512)) #end dst = cv2.add(img, img2)# marge both image #cv2.imshow('image', img) cv2.imshow('image', dst) cv2.waitKey(0) cv2.destroyAllWindows() error Traceback (most recent call last): File "c:/Users/ASUS/Desktop/openCV2/10_arthmatic_opration_on_image.py", line 18, in .py", line 18, in img = cv2.resize(img(512,512)) TypeError: 'numpy.ndarray' object is not callable
The co-ordinates used in the array follow the format of
img [y1: y2, x1: x2]
Therefore, when copying to another part of the image, you need to ensure that (y2-y1) value remains the same, as well as (x2-x1)
Here's another example to copy messi's head, where Top left coordinates is (200, 60) and bottom right is (270, 140) in x,y format
messi_head = img[60:140, 200:270]
img[260:340,100:170] = messi_head
your comment helped me to figure how to mwke this copy paste thing easy...
i have used mouse click to select coordinates of the region which i want to copy and then i have selected by mouse click only the top left corner coordinate of region where i want to paste the ball and the bottom right coordinates i calculated using the formula i figured out from your comment.
i hope my comment will be helpful for others and if anyone face any problem i can post the code
Gabriel, thnx man, your comment helped to get over on what I was stuck for over an hour
Thanks for your comment it help me a lot
Thanks
I at first thought it was x1: x2, y1: y2 and so my program didnot run... now i realised the mistake
Great tutorial, sir, Thank you very much! I'd like to give a small contribution:
I got an error even after resizing (12:12). According to the docs, add() oly works when both images have the same size and same number of channles. However, print(img.shape) says JPG file has 3 channels and print(img2.shape) says PGN file has 4, so we can't add them. But we can remove the 4th channel of the PGN file like this:
if len(img2.shape) > 2 and img2.shape[2] == 4:
img2 = cv2.cvtColor(img2, cv2.COLOR_BGRA2BGR)
Now it worked. Again, thanks for your effort.
My code error says
ValueError: could not broadcast input array from shape (0,0,3) into shape (1,0,3)
Sir please suggest a solution for this
@@arbaazali74 you can refer to the 1st comment
@4:39 in which video did you discuss about the region of interest???
He explained in this only. See carefully
thank you sir it was very useful for python developers
Nice video! Great demonstration of addWeighted. Best I have seen so far. Please keep up the great work. Can you show how to use addWeighted to slowly fade in an image, like maybe fade in another soccer ball?
I guess that would be done with a loop, where the weights are variables that keep updating during each loop.
How did you match the size of the "ball" array exactly to the place where you've put it in the image?
He taught about it in the previous videos.
@@Arjun147gtk in which video broo
Thanks for the tutorial sir.
Hi, I am facing this error ValueError: could not broadcast input array from shape (19,36,3) into shape (9,36,3) .
While doing ROI . Could u plz guide me how to resolve this error
please read gabriel sim comment
hi the same problem. is there anyone to help this erorr?
img.size = heights * width * channels (the size calculation ALSO includes the number of channels - 3 channels for color images and 1 for grayscale images)
Great Explanation
cv2.error: OpenCV(4.2.0) /io/opencv/modules/highgui/src/window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
My code error says
ValueError: could not broadcast input array from shape (0,0,3) into shape (1,0,3)
Sir please suggest a solution for this
I had the same issue... and found out the solution as below:
280:340 is y_upper_left_corner: y_lower_right_corner
330:390 is x_upper_left_corner: x_lower_right_corner
ball = img[280:340, 330:390] # img[y,x]
Sir how you got ball co-ordinates in that image how to get that co-ordinate please clarify my doubt I tried but I'm not getting please help me thank you
sai kumar : Using the previous video get coordinates. for example First click on top left corner of the image (x1 =198,y1=131) and then click on right bottom corner of image(x2 = 282,y2 = 161). Now you have two coordinates, then your copy image , copy_img = img[131:161,198:282] in form of [y1:y2,x1:x2] and your img[47:77,80:164] = copy_img. mind here we use y first then x as y is row and x is column in matrix of image and size 161-131 == 47 -77 and 282 - 198 == 80-164. I hope this might helped you.
Use this code.
mport numpy as np
import cv2
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img, (x,y), 3, (0, 0, 255), -1)
points.append((x,y))
if len(points) >=2:
cv2.rectangle(img, points[-1], points[-2], (255, 0, 0), 5)
cv2.imshow('image', img)
print (x, ', ', y)
font = cv2.FONT_HERSHEY_COMPLEX #define a fonte
strXY = str(x) + ' , ' + str(y)
cv2.putText(img, strXY, (x, y), font, 1, (0, 0, 0), 1 )
cv2.imshow('image', img)
#img = np.zeros((512,512,3), np.uint8)
img = cv2.imread('lena.jpg')
cv2.imshow('image', img)
points = []
cv2.setMouseCallback('image', click_event)
cv2.waitKey(0)
cv2.destroyAllWindows()
how can i get the pixel of particular portion of an image?
addWeighted with alpha = 0.5 and beta = 0.5 should have worked as normal add right???
Hello Sir! i am facing an error
" img[273:333 , 100:160 ,1 ] = ball
ValueError: could not broadcast input array from shape (0,60,3) into shape (7,60) "
please suggest me how to resolve this error?
me too
@@arjunraj6409 have you got your solution? if yes lemme know also.
me too. have you resolved for it?
can you anybody explain how to find the upper left and bottom right point of the ball?
@Zafar Faheem I found an error img[273:333, 100:160] = ball
ValueError: could not broadcast input array from shape (0,60,3) into shape (7,60,3)
@Zafar Faheem only One Pic is required
how i put the icon img on the window of cv2 ?
how you find the coordinates of the ball
i am getting error while i am adding two image of the same size: cv2.error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\imgproc\src
esize.cpp:3720: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
Same here, used two jpg format images and it worked.
Could weight be described in other words as opacity? Or are they two different things?
fonts weight is essentially thickness of font (e.q. normal text vs bold text). Opacity is just how transparent something is.
What Michal says, but it's not just about fonts. Weight is (in this example) a comparisson between the two images. Opacity is transparency. If one of the two images is a completely transparant one, then the weight would behave like opacity, but thats only in that particular example.
how can i exactly define location of ball?
thats what i m also searching for
@@dheerajchumble5602 bro any luck..Please tell if u found
@@forever-fz1hk Just print the cordinates with setMouseCallback().
where can I find the images utilized in the code? I can't seem to find them
They are available in the openCV github repository
when u run this code it says, merge requires only two arguments but provided 3- b,g,r. When u run second time, u wont get output
reply: I forgot to put (b,g,r) inside the bracket :p
# 280:340 is the xi, xf
# 330:390 is the yi yf
ball = img[280:340, 330:390]
how can i exactly define location of ball?
@@ThinhNguyen-hb8nj use the click_event function
How to find an object coordinate? the ball for example, Messi head .... etc ? can anyone help?
img=cv2.merge((b,g,r))
NameError: name 'b' is not defined
sir, its showing this, please help!
how can i find the coordinate of ball
Hello, thanks for the video, however, how do I obtain the Numpy Indexing of ROI?
You can mark the square containing the ball with img = cv2.rectangle(img, (338, 290), (388, 335), (0, 255, 255), 1) Note: the coordinates x, y are reversed compared to np indexing (I'm not sure why x-coordinates are ok, but y-coordinates are off by 5 pixels approx)
line 10, in
img[432:620,537:686]=ball
ValueError: could not broadcast input array from shape (70,94,3) into shape (188,53,3)
#Gabriel Sim cmnt couldnt help even
Sorry Gab, now I understood!!! thnkx man
@@pravuchaudhary3904 hello i got the error like this. let me know you found the solution
@@davidhermawan942 its simple. (1)Select arbritary point that will be ur x1, y1. (2) select another point that is x2, y2. now u need to be careful and modify x2 or y2 value such that y2-y1=x2-x1. Finally write the code which takes value as [y1:y2,x2:x1]
How to calculate image copy paste
Where can I get the image (messi) he used
from github
from google images.
thank you sir
Thanks bro........
bal = img[280:340, 330:390]
here you said (280,340) --> upper left corner &
(330,390) --> bottom right corner. Then shouldn't y coordinate 390< 340.
what's the logic here selecting the coordinate.
checkout his vid for lane detection using opencv, there is a concept of the numbering on the x-axis and y-axis which is a bit different from generally what we know and hence the change in coordinates.
top left is considered as 0,0
Consider that you are working in 4 quadrant with mod of y
this is showing error that neither araay something
my code didnot copy please help
i did the same
I cannot get code. Where is code uri?
My solution
messi_head = img[54:130, 191:266] # *** location of the head
img [175:251,17:92] = messi_head # *** where to copy it
for the head it is img[y1:y2, x1:x2]
and copy is = [y1:y2,x1:x2]
with a remainder that the differences between x1 and x2 = 75 and y1 and y2 = 76
make the calculations
it does not work I did like you, review your code
Which version of OpenCV are you using?
Easy to say "does not worked for me, fix it" Did you even try to fix it yourself?
@@JuanCarlos611 course he didnt' monkey programmers are a thing for a reason
see this problem when
import numpy as np
import cv2
img = cv2.imread('messi5.jpg')
img2 = cv2.imread('opencv-logo.jpg')
print(img.shape)# return a tuple of no of rows,colum,&channel
print(img.size)#return total no of pixels is accessed
print(img.dtype)#return image datatype is obtained
b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))
#-------add image ball-------
#region of interest ROI
# copy this cordinate like ball
ball = img [280:340,330:390]#numpy indexing
img[273:333, 100:160] = ball #place other position with numpy indexing
#--------end other ball section------
#----same size image----both
img = cv2.resize(img(512,512))
img2 = cv2.resize(img2,(512,512))
#end
dst = cv2.add(img, img2)# marge both image
#cv2.imshow('image', img)
cv2.imshow('image', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
error
Traceback (most recent call last):
File "c:/Users/ASUS/Desktop/openCV2/10_arthmatic_opration_on_image.py", line 18, in .py", line 18, in
img = cv2.resize(img(512,512))
TypeError: 'numpy.ndarray' object is not callable