gpt4 book ai didi

Python OpenCV - ConvexHull 错误 "Points is not a numpy array, neither a scalar"?

转载 作者:行者123 更新时间:2023-11-28 05:15:48 42 4
gpt4 key购买 nike

我正在尝试运行一些 Convex Hull 图像处理。基本上我想做的是关闭一个开放的轮廓。

我找到了 this answer over at the opencv forum这正是我想做的。一段时间前,我开始将代码从 C++ 转换为 Python。我成功地转换了问题的代码,但答案的代码给我带来了比预期更艰难的时间。

这是我目前所拥有的:

import cv2
import numpy as np

def contoursConvexHull(contours):
print("contours length = ", len(contours))
print("contours length of first item = ", len(contours[1]))
pts = []
for i in range(0, len(contours)):
for j in range(0, len(contours[i])):
pts.append(contours[i][j])

result = cv2.convexHull(pts)
return result

# Get our image in color mode (1)
src = cv2.imread("source.png", 1);

# Convert the color from BGR to Gray
srcGray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)

# Use Gaussian Blur
srcBlur = cv2.GaussianBlur(srcGray, (3, 3), 0)

# ret is the returned value, otsu is an image
ret, otsu = cv2.threshold(srcBlur, 0, 255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)

# Use canny
srcCanny = cv2.Canny(srcBlur, ret, ret*2, 3)

# im is the output image
# contours is the contour list
# I forgot what heirarchy was
im, contours, heirarchy = cv2.findContours(srcCanny,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(src, contours, -1, (0, 255, 0), 3)

ConvexHullPoints = contoursConvexHull(contours)
cv2.polylines(src, [ConvexHullPoints], True, (0, 255, 255), 2)


cv2.imshow("Test", src)

cv2.waitKey(0)

当我尝试运行它时,它给了我

    result = cv2.convexHull(pts)
TypeError: points is not a numpy array, neither a scalar

我猜我正在为 convexHull 提供一个输入,它需要其他东西。

我在 C++ 方面相当不错,但在 Python 方面我完全是个初学者。我想我可能在将轮廓元素附加到 pts 列表时做错了什么?

老实说,我不太清楚为什么我们需要将这些点追加回一个新数组,似乎没有任何值操纵或重新排列正在进行。

C++代码供引用:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdlib.h>
#include <stdio.h>

using namespace cv;
using namespace std;

vector<Point> contoursConvexHull( vector<vector<Point> > contours )
{
vector<Point> result;
vector<Point> pts;
for ( size_t i = 0; i< contours.size(); i++)
for ( size_t j = 0; j< contours[i].size(); j++)
pts.push_back(contours[i][j]);
convexHull( pts, result );
return result;
}

int main( int, char** argv )
{
Mat src, srcGray,srcBlur,srcCanny;

src = imread( argv[1], 1 );
cvtColor(src, srcGray, CV_BGR2GRAY);
blur(srcGray, srcBlur, Size(3, 3));

Canny(srcBlur, srcCanny, 0, 100, 3, true);

vector<vector<Point> > contours;

findContours( srcCanny, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

Mat drawing = Mat::zeros(srcCanny.size(), CV_8UC3);

for (int i = 0; i< contours.size(); i++)
{
Scalar color = Scalar( 255,255,255);
drawContours( drawing, contours, i, color, 2 );
}

vector<Point> ConvexHullPoints = contoursConvexHull(contours);

polylines( drawing, ConvexHullPoints, true, Scalar(0,0,255), 2 );
imshow("Contours", drawing);

polylines( src, ConvexHullPoints, true, Scalar(0,0,255), 2 );
imshow("contoursConvexHull", src);
waitKey();
return 0;
}

最佳答案

错误提示 pts 不是一个 numpy 数组;它是一个 python list

要将 pts 转换为数组,导入 numpy 并进行简单转换:

import numpy as np

# code ....

pts = np.array(pts)
result = cv2.convexHull(pts)

关于Python OpenCV - ConvexHull 错误 "Points is not a numpy array, neither a scalar"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42698584/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com