Opencv与Python:pointPolygonTest给出明显错误的结果 [英] Opencv with Python: pointPolygonTest gives obviously wrong result
问题描述
我有一个轮廓下面。因为我想提取这个轮廓内的所有像素和黑色的一切去除噪声,我使用cv2.pointPolygonTest为目的。
I have a contour below. As I wanted to extract all the pixels inside this contour and black out everything else to remove noise, I used cv2.pointPolygonTest for the purpose.
下面是我使用的代码尝试创建掩码。
Below are the code I used to attempt to create the mask.
inside_or_not = np.zeros(img.shape[:2],dtype = np.int32)
for i in range(0,img.shape[0]):
for j in range(0,img.shape[1]):
inside_or_not[i,j] = cv2.pointPolygonTest(body_line,(i,j),False)
里面的点。除此之外,我期望在轮廓上的点的数量,因此从cv2.pointPolygonTest返回0应当匹配定义轮廓的像素的数量。然而,当我运行sum(sum(inside_or_not == 0)),它不匹配轮廓上的像素数。
我也用鼠标点击点击一个点显然在轮廓内,并把这一点进入测试;但是返回-1表示测试失败。
Only 2 points were found to be inside the point. On top of that, I expect the number of points lying on the contour, hence returning 0 from the cv2.pointPolygonTest should match the number of pixels defining the contour. However when I run sum(sum(inside_or_not == 0)), it does not match the No. of pixels on the contour. I also used mouse click to click a point obviously inside the contour and put that point into the test; but -1 is returned indicating the test failed.
我也使用了一个approximPolyDP函数来尝试用更少的顶点逼近轮廓。这一次有点多返回。但我不知道为什么!
I also used a approxPolyDP function to attempt to approximate the contour with less vertices. This time a bit more points were returned. However I have no idea why!
任何帮助,感谢,谢谢。
Any help is appreciated, thanks.
推荐答案
由于你有一个定义良好的轮廓,你可以使用 findContour c>使用
CV_RETR_EXTERNAL
检测外部轮廓,并用 drawContour(...,CV_FILLED) / code>。一旦你有了面具,你可以使用
copyTo
根据面具仅复制原始图像的部分。
Since you have a well defined contour, you can simply use findContour
with CV_RETR_EXTERNAL
to detect the external contour, and draw the region inside it with drawContour(..., CV_FILLED)
. Once you have the mask, you can use copyTo
to copy only the parts of your original image according to the mask.
这里是一个例子,它是C ++,但是移植到python很简单。
Here an example, it's C++, but the porting to python is straightforward.
#include <opencv2\opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
// Load image
Mat3b img = imread("path_to_image_shape");
// Just to have a background image the same size of your image.
Mat3b bkg = imread("path_to_image_background");
resize(bkg, bkg, img.size());
// Convert to gray
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
// Get external contour
vector<vector<Point>> contours;
findContours(gray.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// contours has size 1 here, you need to check this in a real application
// Create a mask with the inner part of the contour
Mat1b mask(img.size(), uchar(0));
drawContours(mask, contours, 0, Scalar(255), CV_FILLED);
// Create a black RGB image
Mat3b res(img.size(), Vec3b(0,0,0));
// Copy the image according to the mask
bkg.copyTo(res, mask);
imshow("result", res);
waitKey();
return 0;
}
这篇关于Opencv与Python:pointPolygonTest给出明显错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!