Facing Problem in drawing counters for resized Image

asked 2018-12-07 10:56:14 -0600

sreevathsabr gravatar image

updated 2020-10-18 13:58:17 -0600

img=r"C:\5_cube.tif"
img = cv2.imread(img, cv2.IMREAD_COLOR)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Code to Find edges of Square using Canny edge detection method and finding contours and drawing in Back Line

edges = cv2.Canny(img_gray, 0, 100)
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img, contours, -1, (0, 0, 0), 5)

#Save contours

cv2.imwrite(r"C:\Users\burli\Desktop\1.tif",img)

Now we will read Resized image, Resize the contours and apply the contours on the Resized Image

image_resize=r"C:\ImageResize.tif"
image_resize = cv2.imread(image_resize, cv2.IMREAD_COLOR)
height = np.size(image_resize, 0)
width = np.size(image_resize, 1)
ratio = width/height
CounterNewValue = np.multiply(ratio, contours)
NewconterList = []
for i in CounterNewValue:
     for j in range(len(i)):
         CounterNewValue[j] = CounterNewValue[j].astype(int)

cv2.drawContours(image_resize, CounterNewValue, -1, (0, 0, 0), 5)

cv2.imwrite(r"C:\3.tif",image_resize)

Is there Some thing i am doing wrong ? Error Message

Traceback (most recent call last):
  File "C:/EffectMainFunction.py", line 17, in <module>
    ObjConturesCreation.ExtractColorFromImage()
  File "C:\conturesCreation.py", line 34, in ExtractColorFromImage
    cv2.drawContours(image_resize, CounterNewValue, -1, (0, 0, 0), 5)
cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\imgproc\src\drawing.cpp:2511: error: (-215:Assertion failed) npoints > 0 in function 'cv::drawContours'
edit retag flag offensive close merge delete

Comments

contours is not defined. CounterNewValue = np.multiply(ratio, contours)

supra56 gravatar imagesupra56 ( 2018-12-08 08:12:14 -0600 )edit

This line of code will provide contours

 im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
sreevathsabr gravatar imagesreevathsabr ( 2018-12-09 21:05:33 -0600 )edit