Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv和Python:PointPolyContest给出了明显错误的结果_Python_Opencv_Image Processing_Computer Vision_Contour - Fatal编程技术网

Opencv和Python:PointPolyContest给出了明显错误的结果

Opencv和Python:PointPolyContest给出了明显错误的结果,python,opencv,image-processing,computer-vision,contour,Python,Opencv,Image Processing,Computer Vision,Contour,我下面有一个轮廓。由于我想提取这个轮廓内的所有像素,并遮住其他所有像素以去除噪声,因此我使用了cv2.PointPolyContest 下面是我用来尝试创建掩码的代码 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.pointPol

我下面有一个轮廓。由于我想提取这个轮廓内的所有像素,并遮住其他所有像素以去除噪声,因此我使用了cv2.PointPolyContest

下面是我用来尝试创建掩码的代码

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)
只有2个点位于该点内。最重要的是,我希望轮廓上的点数,因此从cv2.PointPolyContest返回0应该与定义轮廓的像素数匹配。但是,当我运行sum(sum(inside_或_not==0))时,它与轮廓上的像素数不匹配。 我还用鼠标点击轮廓内的一个点,并将该点放入测试中;但是返回-1表示测试失败

我还使用了approxPolyDP函数来尝试用较少的顶点来近似轮廓。这次又多了一点积分。但是我不知道为什么

谢谢你的帮助


由于您有一个定义良好的轮廓,您只需使用
findContour
CV\u RETR\u EXTERNAL
来检测外部轮廓,并使用
drawContour(…,CV\u FILLED)
来绘制内部区域。有了遮罩后,可以使用
copyTo
根据遮罩仅复制原始图像的部分

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)

这里有一个例子,它是C++,但是移植到Python是很简单的。

#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;
}
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv)
{
//加载图像
Mat3b img=imread(“路径到图像形状”);
//只是为了有一个与你的图像大小相同的背景图像。
Mat3b bkg=imread(“路径到图像和背景”);
调整大小(bkg,bkg,img.size());
//变灰
Mat1b灰色;
CVT颜色(img,灰色,颜色为灰色);
//获取外部轮廓
矢量等值线;
findContours(gray.clone()、等高线、等高线、等高线外部、等高线链近似简单);
//等高线在这里的大小为1,您需要在实际应用中对此进行检查
//使用轮廓的内部部分创建遮罩
Mat1b掩模(img.size(),uchar(0));
绘制等高线(遮罩、等高线、0、标量(255)、CV_填充);
//创建黑色RGB图像
Mat3b res(img.size(),Vec3b(0,0,0));
//根据遮罩复制图像
bkg.copyTo(分辨率、掩码);
imshow(“结果”,res);
waitKey();
返回0;
}

关于:1)findContours,2)drawContours(…CV_FILLED…3)findNonZero(可选)?抱歉,但我不确定填充颜色的drawContours会有什么影响,因为绘图功能肯定不会返回或修改任何现有对象…?能否上载png图像?jpeg创建了不需要的工件。我已将其更改为png。我发布了一个答案,请告诉我它是否适合您的需要。非常感谢您的回答。也许我应该说得更清楚些。我有一个numpy数组img,表示原始图像文件。还有,我定义了这个轮廓。我希望有一个名为filtered_img的numpy数组,以便此轮廓外的每个像素都将具有RGB值0,而轮廓内的每个像素都将具有与img中定义的相同的RGB值object@wudanao检查更新的答案。一旦你有了遮罩(通过
drawContour
获得),使用
copyTo
只复制图像的遮罩部分。哇,这很漂亮,也很有希望。我忘了drawcontours实际上修改了原始numpy数组。今晚我会回去尝试实施它。谢谢