私はいくつかの凸包の画像処理を実行しようとしています。基本的に私がしたいのは、開いた輪郭を閉じることです。Python OpenCV - ConvexHullエラー "点数は配列がスカラーではなく、数値ではありません"?
私はthis answer over at the opencv forumを見つけました。これはまさに私がやりたいことです。私はコードをC++からPythonに少し前に変換し始めました。私は質問のコードを変換することに成功しましたが、答えのコードは私に予想よりも厳しい時間を与えています。
これは私がこれまで持っているものです: 輸入CV2 輸入numpyの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++からPythonに変換するのを助けることができれば、私は感謝しています。
はここで参照のための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の、簡単な変換を行うには。しかし、私はまだ私が期待した結果を得ていません。 – Razgriz
それは難しいですが、うまくいけばあなたの元の質問に答えました。新しい問題を反映するために質問を更新することができます。実際には、新しい質問をすることを検討してください。 – putonspectacles
私はそれを得ました、私はちょうど角括弧で 'ConvexHullPoints'を囲む必要があり、コードは意図したとおりに動作しています。 – Razgriz