OpenCV-Python中绘制缩放图像计数器面临的问题

2024-04-20 01:02:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在图像上绘制计数器时遇到问题。原始图像和复制图像的内容相同,只是大小不同。你知道吗

原始图像尺寸:1275x1650 调整图像大小:338 x478

从算法的角度来看,我是不是完全错了(比如说,我认为在调整大小的图像上绘制计数器也是通过调整计数器的大小来实现的)?你知道吗

代码如下:

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)

我成功地在原始图像上绘制计数器。 下面的代码是用于调整大小的图像

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)
#Making Float CounterNewValue to int
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)

下面是错误消息

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'

请帮我解决这个问题。你知道吗


Tags: in图像imageimgnp计数器绘制cv2