Skip to content
Advertisement

How contour an image area with known coordinates?

I have an ultrasound image and the coordinates (array of x and y points) of the tumor of each image. How can I make a contour around the tumor with those known coordinates using cv.drawContours()? I wish to delete all the information around the tumor and stay only with the tumor image/information.

Ultrasound image with the tumor ROI

temp=np.array([[284.,  60.],[276.,  59.],[269.,  58.],[260.,  58.],[247.,  60.],[241.,  65.],[237.,  68.],[233.,  72.],[228.,  80.],[225.,  87.],[225.,  96.],[229., 107.],[233., 109.],[238., 110.],[244., 111.],[253., 112.],[260., 113.],[267., 115.],[273., 116.],[290., 115.],[298., 113.],[306., 110.],[313., 109.],[323., 108.],[330., 102.],[330., 100.],[335.,  96.],[338.,  94.],[344.,  91.],[346.,  86.],[346.,  82.],[346.,  77.],[346.,  73.],[341.,  70.],[337.,  68.],[327.,  64.],[322.,  63.],[314.,  62.],[305.,  62.],[300.,  61.],[293.,  60.],[289.,  60.],[284.,  60.]])

imagem = cv2.imread('thyroid/2_1.jpg', cv2.IMREAD_GRAYSCALE)

cc=cv2.drawContours(imagem,temp,3,(0,255,0),3)

I am getting this error: error Traceback (most recent call last) <ipython-input-25-59da73cb5b29> in <module> 2 imagem = cv2.imread('thyroid/2_1.jpg', cv2.IMREAD_GRAYSCALE) 3 ----> 4 cc=cv2.drawContours(imagem,temp,3,(0,255,0),3) error: OpenCV(3.4.1) C:Miniconda3conda-bldopencv-suite_1533128839831workmodulesimgprocsrcdrawing.cpp:2515: error: (-215) npoints > 0 in function cv::drawContours How can I fix this error? Because my contours are a list

img = cv2.imread('thyroid/2_1.jpg', cv2.IMREAD_GRAYSCALE) temp=temp.astype(np.int32) cc=cv2.drawContours(img,[temp.astype('i4')],-1,(0,255,0),3)

Advertisement

Answer

2 problems in the above code:

1: As I mentioned in my comment, draw contours expects a list of contours so wrap temp in brackets to make it a list:

cc=cv2.drawContours(imagem,[temp],-1,(0,255,0),3)

2: Draw contours seems to be unhappy with 64 bit floats (the default for numpy). Giving it a 32 bit int seemed to work for me as suggested here.

cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(0,255,0),3)

Entire working code on my PC:

import cv2
import numpy as np

temp=np.array([[284.,  60.], [276.,  59.], [269.,  58.], [260.,  58.], [247.,  60.], [241.,  65.], [237.,  68.], [233.,  72.], [228.,  80.], [225.,  87.], [225.,  96.], [229., 107.], [233., 109.], [238., 110.], [244., 111.], [253., 112.], [260., 113.], [267., 115.], [273., 116.], [290., 115.], [298., 113.], [306., 110.], [313., 109.], [323., 108.], [330., 102.], [330., 100.], [335.,  96.], [338.,  94.], [344.,  91.], [346.,  86.], [346.,  82.], [346.,  77.], [346.,  73.], [341.,  70.], [337.,  68.], [327.,  64.], [322.,  63.], [314.,  62.], [305.,  62.], [300.,  61.], [293.,  60.], [289.,  60.]])

imagem = cv2.imread(r'c:UsersusernameDesktopimage.png', cv2.IMREAD_GRAYSCALE)

cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(255,0,0),3)
cv2.imshow('image',cc)
cv2.waitKey(0)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement