How to plot histogram of an area inside a contour [closed]

asked 2019-12-20 11:40:51 -0600

hernancrespo gravatar image

updated 2019-12-26 15:10:38 -0600

Hi, i am using OpenCV and python. i can find the biggest contour biggest contour i need histogram of this area. i tried this

 for c in cntrs:
        area = cv2.contourArea(c)
        if area > area_thresh:
            area = area_thresh
            big_contour = c
        if cntrs != None:
            results = fg.copy() #draw the contour on a copy of the input image
            cv2.drawContours(results,[big_contour],0,(0,0,255),2) # 2 is thickness
            cv2.imshow('results', results)

       hist_item = cv2.calcHist([results],[cntrs],big_contour,[256],[0,255])

i see this error message: <built-in function calcHist> returned NULL without setting an error

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-26 14:11:34.437931

Comments

what's your problem, exactly ? getting the area inside the contour ?

https://docs.opencv.org/master/de/db2...

berak gravatar imageberak ( 2019-12-20 12:27:11 -0600 )edit

yes getting the area inside the contour, and plot the histogram of that area

hernancrespo gravatar imagehernancrespo ( 2019-12-20 13:25:11 -0600 )edit

ton of things wrong here:

  1. anything below big_contour = c has to go out of the for - loop
  2. don't draw into an image you want to take an histogram from later
  3. contours do not belong into histogram code (that's your error) please look at the tutoro ials again

you can try to fill the outside of the contour with e.g. black, and then take the histogram

berak gravatar imageberak ( 2019-12-27 02:24:04 -0600 )edit