find contourを使用して矩形を検出しようとしていますが、次の画像から輪郭が得られません。find contourを使用して矩形を検出しようとしています
画像内の任意の輪郭を検出できません。次の画像で輪郭が見つからないか、またはハフ変換を使用する必要がありますか?
更新:近似されたポリゴンを使用するようにソースコードを更新しました。
しかし、私はまだ外れ値の境界を得ていますが、スクリーンショットにある最小の長方形を見つけることはできません。
私は浸食や拡張を追加するとき、現在のソリューションは、それも仕事をdoesntの別のケースを持っています。
、ここでコード
using namespace cm;
using namespace cv;
using namespace std;
cv::Mat input = cv::imread("heightmap.png");
RNG rng(12345);
// convert to grayscale (you could load as grayscale instead)
cv::Mat gray;
cv::cvtColor(input,gray, CV_BGR2GRAY);
// compute mask (you could use a simple threshold if the image is always as good as the one you provided)
cv::Mat mask;
cv::threshold(gray, mask, 0, 255,CV_THRESH_OTSU);
cv::namedWindow("threshold");
cv::imshow("threshold",mask);
// find contours (if always so easy to segment as your image, you could just add the black/rect pixels to a vector)
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(mask,contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
cv::Mat drawing = cv::Mat::zeros(mask.size(), CV_8UC3);
vector<vector<cv::Point> > contours_poly(contours.size());
vector<vector<cv::Point> > (contours.size());
vector<cv::Rect> boundRect(contours.size());
for(int i = 0; i < contours.size(); i++)
{
approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);
boundRect[i] = boundingRect(cv::Mat(contours_poly[i]));
}
for(int i = 0; i< contours.size(); i++)
{
cv::Scalar color = cv::Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255));
rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
}
// display
cv::imshow("input", input);
cv::imshow("drawing", drawing);
cv::waitKey(0);
私は輪郭を見つける代わりにハフ変換を使うべきだと思いますか?私に不規則な形があるので? – andre
に依存します。より安定した 'findContours'でイメージスティックをクリーンアップすることができれば。 –
イメージスティックをクリーンアップすることはどういう意味ですか? I begin初心者とIḿ学習。もっと説明して助けてください。ありがとうございました – andre