如何用已知坐标绘制图像区域的轮廓?
How contour an image area with known coordenates?
我有一张超声图像和每张图像的肿瘤坐标(x 和 y 点数组)。如何使用 "cv.drawContours()" 使用已知坐标在肿瘤周围绘制轮廓?我希望删除肿瘤周围的所有信息,只留下肿瘤image/information。
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)
我收到这个错误:
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:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgproc\src\drawing.cpp:2515: error: (-215) npoints > 0 in function cv::drawContours
我该如何解决这个错误?因为我的轮廓是一个列表
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)
上述代码存在2个问题:
1:正如我在评论中提到的,绘制轮廓需要一个轮廓列表,因此将 temp
括在方括号中以使其成为一个列表:
cc=cv2.drawContours(imagem,[temp],-1,(0,255,0),3)
2:绘制轮廓似乎对 64 位浮点数(numpy 的默认设置)不满意。按照建议 here,给它一个 32 位 int 似乎对我有用。
cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(0,255,0),3)
我电脑上的完整工作代码:
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:\Users\username\Desktop\image.png', cv2.IMREAD_GRAYSCALE)
cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(255,0,0),3)
cv2.imshow('image',cc)
cv2.waitKey(0)
我有一张超声图像和每张图像的肿瘤坐标(x 和 y 点数组)。如何使用 "cv.drawContours()" 使用已知坐标在肿瘤周围绘制轮廓?我希望删除肿瘤周围的所有信息,只留下肿瘤image/information。
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)
我收到这个错误:
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:\Miniconda3\conda-bld\opencv-suite_1533128839831\work\modules\imgproc\src\drawing.cpp:2515: error: (-215) npoints > 0 in function cv::drawContours
我该如何解决这个错误?因为我的轮廓是一个列表
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)
上述代码存在2个问题:
1:正如我在评论中提到的,绘制轮廓需要一个轮廓列表,因此将 temp
括在方括号中以使其成为一个列表:
cc=cv2.drawContours(imagem,[temp],-1,(0,255,0),3)
2:绘制轮廓似乎对 64 位浮点数(numpy 的默认设置)不满意。按照建议 here,给它一个 32 位 int 似乎对我有用。
cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(0,255,0),3)
我电脑上的完整工作代码:
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:\Users\username\Desktop\image.png', cv2.IMREAD_GRAYSCALE)
cc=cv2.drawContours(imagem,[temp.astype('i4')],-1,(255,0,0),3)
cv2.imshow('image',cc)
cv2.waitKey(0)