OpenCV Python Tutorial For Beginners 10 - cv.split, cv.merge, cv.resize, cv.add, cv.addWeighted, ROI

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024

ความคิดเห็น • 98

  • @gabrielsim2709
    @gabrielsim2709 4 ปีที่แล้ว +63

    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

    • @codingmaniac6005
      @codingmaniac6005 4 ปีที่แล้ว +3

      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

    • @ayushjindal313
      @ayushjindal313 4 ปีที่แล้ว +1

      Gabriel, thnx man, your comment helped to get over on what I was stuck for over an hour

    • @himanshu5906
      @himanshu5906 4 ปีที่แล้ว +1

      Thanks for your comment it help me a lot

    • @dishagupta7446
      @dishagupta7446 4 ปีที่แล้ว

      Thanks

    • @kamalkrishnaroy8496
      @kamalkrishnaroy8496 4 ปีที่แล้ว

      I at first thought it was x1: x2, y1: y2 and so my program didnot run... now i realised the mistake

  • @JuanCarlos611
    @JuanCarlos611 4 ปีที่แล้ว +3

    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.

    • @arbaazali74
      @arbaazali74 4 ปีที่แล้ว

      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

    • @kamalkrishnaroy8496
      @kamalkrishnaroy8496 4 ปีที่แล้ว

      @@arbaazali74 you can refer to the 1st comment

  • @tanvirmd.sarwersabbir3931
    @tanvirmd.sarwersabbir3931 4 ปีที่แล้ว +1

    @4:39 in which video did you discuss about the region of interest???

    • @me-oniichan
      @me-oniichan 4 ปีที่แล้ว

      He explained in this only. See carefully

  • @CyberSaurabh
    @CyberSaurabh 3 ปีที่แล้ว

    thank you sir it was very useful for python developers

  • @joliver1981
    @joliver1981 5 ปีที่แล้ว

    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?

    • @TheQuickUplifts
      @TheQuickUplifts 5 ปีที่แล้ว

      I guess that would be done with a loop, where the weights are variables that keep updating during each loop.

  • @irtesamnasrat9866
    @irtesamnasrat9866 5 ปีที่แล้ว +2

    How did you match the size of the "ball" array exactly to the place where you've put it in the image?

    • @Arjun147gtk
      @Arjun147gtk 4 ปีที่แล้ว

      He taught about it in the previous videos.

    • @deekshithdogiparthi
      @deekshithdogiparthi 2 ปีที่แล้ว

      @@Arjun147gtk in which video broo

  • @abdurrazzak1612
    @abdurrazzak1612 4 ปีที่แล้ว +2

    Thanks for the tutorial sir.

  • @shailavijay1
    @shailavijay1 4 ปีที่แล้ว +4

    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

    • @codingmaniac6005
      @codingmaniac6005 4 ปีที่แล้ว

      please read gabriel sim comment

    • @davidhermawan942
      @davidhermawan942 4 ปีที่แล้ว

      hi the same problem. is there anyone to help this erorr?

  • @anthonygonsalvis121
    @anthonygonsalvis121 4 ปีที่แล้ว

    img.size = heights * width * channels (the size calculation ALSO includes the number of channels - 3 channels for color images and 1 for grayscale images)

  • @hussainmayouf5835
    @hussainmayouf5835 5 ปีที่แล้ว +1

    Great Explanation

  • @tittodominic933
    @tittodominic933 4 ปีที่แล้ว +1

    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'

  • @arbaazali74
    @arbaazali74 4 ปีที่แล้ว +3

    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

    • @KiranRoyworkroyal
      @KiranRoyworkroyal 4 ปีที่แล้ว +4

      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]

  • @saikumar-gc2yh
    @saikumar-gc2yh 5 ปีที่แล้ว +1

    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

    • @himanshushekhar26
      @himanshushekhar26 5 ปีที่แล้ว +4

      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.

    • @rzzengenhariamineracao334
      @rzzengenhariamineracao334 5 ปีที่แล้ว +2

      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()

  • @madhumitha-gr1gw
    @madhumitha-gr1gw 4 ปีที่แล้ว

    how can i get the pixel of particular portion of an image?

  • @ritapravadutta7939
    @ritapravadutta7939 4 ปีที่แล้ว

    addWeighted with alpha = 0.5 and beta = 0.5 should have worked as normal add right???

  • @razi_official
    @razi_official 4 ปีที่แล้ว +1

    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?

    • @arjunraj6409
      @arjunraj6409 4 ปีที่แล้ว

      me too

    • @razi_official
      @razi_official 4 ปีที่แล้ว

      @@arjunraj6409 have you got your solution? if yes lemme know also.

    • @davidhermawan942
      @davidhermawan942 4 ปีที่แล้ว

      me too. have you resolved for it?

  • @muhammadhasnain2238
    @muhammadhasnain2238 5 ปีที่แล้ว +1

    can you anybody explain how to find the upper left and bottom right point of the ball?

    • @namramumtaz9061
      @namramumtaz9061 4 ปีที่แล้ว

      @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)

    • @namramumtaz9061
      @namramumtaz9061 4 ปีที่แล้ว

      @Zafar Faheem only One Pic is required

  • @Eeriezonee
    @Eeriezonee 4 ปีที่แล้ว

    how i put the icon img on the window of cv2 ?

  • @satyamchauhan9775
    @satyamchauhan9775 3 ปีที่แล้ว

    how you find the coordinates of the ball

  • @keyurjoshi9685
    @keyurjoshi9685 5 ปีที่แล้ว

    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'

    • @brokelaowaiinchina
      @brokelaowaiinchina 5 ปีที่แล้ว

      Same here, used two jpg format images and it worked.

  • @megaredemption8051
    @megaredemption8051 5 ปีที่แล้ว

    Could weight be described in other words as opacity? Or are they two different things?

    • @michalpitr4474
      @michalpitr4474 5 ปีที่แล้ว

      fonts weight is essentially thickness of font (e.q. normal text vs bold text). Opacity is just how transparent something is.

    • @TheQuickUplifts
      @TheQuickUplifts 5 ปีที่แล้ว

      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.

  • @ThinhNguyen-hb8nj
    @ThinhNguyen-hb8nj 5 ปีที่แล้ว +1

    how can i exactly define location of ball?

    • @dheerajchumble5602
      @dheerajchumble5602 5 ปีที่แล้ว +2

      thats what i m also searching for

    • @forever-fz1hk
      @forever-fz1hk 4 ปีที่แล้ว

      @@dheerajchumble5602 bro any luck..Please tell if u found

    • @proturguenev1820
      @proturguenev1820 4 ปีที่แล้ว +2

      @@forever-fz1hk Just print the cordinates with setMouseCallback().

  • @nelsonalvarez1013
    @nelsonalvarez1013 4 ปีที่แล้ว

    where can I find the images utilized in the code? I can't seem to find them

    • @tanayraj2991
      @tanayraj2991 4 ปีที่แล้ว

      They are available in the openCV github repository

  • @pravuchaudhary3904
    @pravuchaudhary3904 4 ปีที่แล้ว

    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

    • @pravuchaudhary3904
      @pravuchaudhary3904 4 ปีที่แล้ว

      reply: I forgot to put (b,g,r) inside the bracket :p

  • @djameleddinechahrat4926
    @djameleddinechahrat4926 5 ปีที่แล้ว

    # 280:340 is the xi, xf
    # 330:390 is the yi yf
    ball = img[280:340, 330:390]

    • @ThinhNguyen-hb8nj
      @ThinhNguyen-hb8nj 5 ปีที่แล้ว +1

      how can i exactly define location of ball?

    • @muneeskhan5193
      @muneeskhan5193 4 ปีที่แล้ว

      @@ThinhNguyen-hb8nj use the click_event function

  • @ahmed.bhewary775
    @ahmed.bhewary775 3 ปีที่แล้ว

    How to find an object coordinate? the ball for example, Messi head .... etc ? can anyone help?

  • @apoorvgupta7060
    @apoorvgupta7060 3 ปีที่แล้ว

    img=cv2.merge((b,g,r))
    NameError: name 'b' is not defined
    sir, its showing this, please help!

  • @losten_chapter1190
    @losten_chapter1190 4 ปีที่แล้ว

    how can i find the coordinate of ball

  • @peanutsee
    @peanutsee 4 ปีที่แล้ว +1

    Hello, thanks for the video, however, how do I obtain the Numpy Indexing of ROI?

    • @JuanCarlos611
      @JuanCarlos611 4 ปีที่แล้ว

      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)

  • @pravuchaudhary3904
    @pravuchaudhary3904 4 ปีที่แล้ว

    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)

    • @pravuchaudhary3904
      @pravuchaudhary3904 4 ปีที่แล้ว

      #Gabriel Sim cmnt couldnt help even

    • @pravuchaudhary3904
      @pravuchaudhary3904 4 ปีที่แล้ว

      Sorry Gab, now I understood!!! thnkx man

    • @davidhermawan942
      @davidhermawan942 4 ปีที่แล้ว

      @@pravuchaudhary3904 hello i got the error like this. let me know you found the solution

    • @pravuchaudhary3904
      @pravuchaudhary3904 4 ปีที่แล้ว

      @@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]

  • @kalaipradeep2753
    @kalaipradeep2753 2 ปีที่แล้ว

    How to calculate image copy paste

  • @erinwolf1563
    @erinwolf1563 5 ปีที่แล้ว

    Where can I get the image (messi) he used

  • @sarthakshah6761
    @sarthakshah6761 4 ปีที่แล้ว

    thank you sir

  • @shashanksharmadon4295
    @shashanksharmadon4295 4 ปีที่แล้ว

    Thanks bro........

  • @mahmudulhasanudoy626
    @mahmudulhasanudoy626 4 ปีที่แล้ว

    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.

    • @muneeskhan5193
      @muneeskhan5193 4 ปีที่แล้ว +1

      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.

    • @Arjun147gtk
      @Arjun147gtk 4 ปีที่แล้ว

      top left is considered as 0,0
      Consider that you are working in 4 quadrant with mod of y

  • @NIHITGOAT
    @NIHITGOAT ปีที่แล้ว

    this is showing error that neither araay something

  • @HushedGamming
    @HushedGamming 4 ปีที่แล้ว

    my code didnot copy please help

  • @shuaishuai2009
    @shuaishuai2009 2 ปีที่แล้ว

    I cannot get code. Where is code uri?

  • @ahmed.bhewary775
    @ahmed.bhewary775 3 ปีที่แล้ว

    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

  • @professor002055
    @professor002055 5 ปีที่แล้ว

    it does not work I did like you, review your code

    • @SangNguyen-vt8jg
      @SangNguyen-vt8jg 5 ปีที่แล้ว

      Which version of OpenCV are you using?

    • @JuanCarlos611
      @JuanCarlos611 4 ปีที่แล้ว +2

      Easy to say "does not worked for me, fix it" Did you even try to fix it yourself?

    • @cloudboysmusic5223
      @cloudboysmusic5223 4 ปีที่แล้ว

      @@JuanCarlos611 course he didnt' monkey programmers are a thing for a reason

  • @saifu2write
    @saifu2write 4 ปีที่แล้ว

    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