Skip to content

Artifacts in image generated with cv::fillPoly #22012

@gmnl

Description

@gmnl
System information (version)
  • OpenCV => 3.4.1, 4.5.5
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2017
Detailed description

Result image of cv::fillPoly contains artifacts at the corner of the polygon. In the following image there are small black dots at the right top of the white area.
BadFilledPolygon
It seems like this mostly happenes if some of the polygon coordinates are outside of the image, but only in rare cases. And it seems that it doesn't happen with line mode cv::LineTypes::LINE_4

Steps to reproduce

UnitTest to reproduce and to generate the image shown above.

TEST(OpenCVTests, CvFillPolyCheckForArtifacts)
{
	unsigned imageWidth = 256;
	unsigned imageHeight = 256;
	for (int y = imageHeight/2; y > -50; y -= 1)
	{
                // define polygon
		std::vector<cv::Point> polygonPoints;
		polygonPoints.push_back(cv::Point(100, imageHeight - y));
		polygonPoints.push_back(cv::Point(imageWidth, imageHeight));
		polygonPoints.push_back(cv::Point(imageWidth, 0));

                // convert data
		std::vector<const cv::Point*> polygonPointPointers(polygonPoints.size());
		for (size_t i = 0; i < polygonPoints.size(); ++i)
		{
			polygonPointPointers[i] = &polygonPoints[i];
		}

		auto data = &polygonPointPointers.front();
		int size = (int)polygonPoints.size();
		const int* npts = &size;
		int ncontours = 1;
		cv::Mat img(imageWidth, imageHeight, CV_8UC1);
		img = 0;
		int lineType = cv::LineTypes::LINE_8;
		int shift = 0;
		cv::Point offset(0, 0);

                // generate image
		cv::fillPoly(img, data, npts, ncontours, 255, lineType, shift, offset);
	
                // check for artifacts
		cv::Mat binary = img < 128;
		cv::Mat labelImage(binary.size(), CV_32S);
		cv::Mat labelStats;
		cv::Mat labelCentroids;
		auto labels = cv::connectedComponents(binary, labelImage, 4);
		EXPECT_EQ(2, labels);
		if (labels > 2)
		{
			cv::imwrite("C:\\temp\\BadFilledPolygon.png", img);
		}
	}
	for (int x = imageWidth / 2; x > -50; x -= 1)
	{
		// define polygon
		std::vector<cv::Point> polygonPoints;
		polygonPoints.push_back(cv::Point(imageWidth-x, 100));
		polygonPoints.push_back(cv::Point(imageWidth, imageHeight));
		polygonPoints.push_back(cv::Point(0, imageHeight));
		
		// convert data
		std::vector<const cv::Point*> polygonPointPointers(polygonPoints.size());
		for (size_t i = 0; i < polygonPoints.size(); ++i)
		{
			polygonPointPointers[i] = &polygonPoints[i];
		}

		auto data = &polygonPointPointers.front();
		int size = (int)polygonPoints.size();
		const int* npts = &size;
		int ncontours = 1;
		cv::Mat img(imageWidth, imageHeight, CV_8UC1);
		img = 0;
		int lineType = cv::LineTypes::LINE_4;
		int shift = 0;
		cv::Point offset(0, 0);
		
		// generate image
		cv::fillPoly(img, data, npts, ncontours, 255, lineType, shift, offset);

		// check for artifacts
		cv::Mat binary = img < 128;

		cv::Mat labelImage(binary.size(), CV_32S);
		cv::Mat labelStats;
		cv::Mat labelCentroids;
		auto labels = cv::connectedComponents(binary, labelImage, 4);
		EXPECT_EQ(2, labels);
		if (labels > 2)
		{
			cv::imwrite("C:\\temp\\BadFilledPolygon.png", img);
		}
	}

}
Issue submission checklist
  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues,
    forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files: videos, images, onnx, etc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions