1 | initial version |
try this, it has a simpler way to do what your code does. there is still a dot on result image. i think you should use findContours() to get rid of it.
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
int main() {
Mat srcImg = imread("test.png", IMREAD_GRAYSCALE);
Mat dstImg = srcImg > 127;
rectangle(dstImg, Rect(0, 0, dstImg.cols, dstImg.rows), Scalar(255));
floodFill(dstImg, Point(0,0), Scalar(0));
imshow("srcImg", srcImg);
imshow("dstImg", dstImg);
waitKey();
return 0;
}
2 | No.2 Revision |
try this,
it has a simpler way to do what your code does. but there is still a dot on result image. i think image when using floodFill() method.
so you should use findContours() to get rid of it.
#include<opencv2/core.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat srcImg = imread("test.png", IMREAD_GRAYSCALE);
Mat dstImg = srcImg > 127;
rectangle(dstImg, Rect(0, 0, dstImg.cols, dstImg.rows), Scalar(255));
floodFill(dstImg, Point(0,0), Point(0, 0), Scalar(0));
imshow("srcImg", srcImg);
imshow("dstImg", dstImg);
srcImg = srcImg > 127;
vector<vector<Point> > contours;
findContours(srcImg, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++)
{
Rect bounding_rect = boundingRect(contours[i]);
Rect test_rect = bounding_rect & Rect(1, 1, dstImg.cols - 2, dstImg.rows - 2);
if (bounding_rect != test_rect)
{
drawContours(srcImg, contours, (int)i, Scalar(0),-1);
}
}
imshow("Result using findContours", srcImg);
waitKey();
return 0;
}